From 85ad8850fc6853e2f359f1e1a853ee8e1d59ac60 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 6 Feb 2023 21:46:13 -0500 Subject: [PATCH 1/3] add timeout and error handling --- PostHog/Internal/PHGHTTPClient.m | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/PostHog/Internal/PHGHTTPClient.m b/PostHog/Internal/PHGHTTPClient.m index 26d0dfa9b6..ee26dad049 100644 --- a/PostHog/Internal/PHGHTTPClient.m +++ b/PostHog/Internal/PHGHTTPClient.m @@ -21,6 +21,8 @@ - (instancetype)initWithRequestFactory:(PHGRequestFactory)requestFactory self.requestFactory = requestFactory; } NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; + config.timeoutIntervalForRequest = 10; + config.HTTPAdditionalHeaders = @{ @"Accept-Encoding" : @"gzip", @"Content-Encoding" : @"gzip", @@ -140,9 +142,18 @@ - (NSURLSessionDataTask *)sharedSessionUpload:(NSDictionary *)payload host:(NSUR PHGLog(@"Error uploading request %@.", error); failure(error); } else { - NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; - NSLog(@"%@",json); - success(json); + NSInteger code = ((NSHTTPURLResponse *)response).statusCode; + if (code < 300) { + NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + NSLog(@"%@",json); + success(json); + } + NSError *error = [NSError errorWithDomain:NSURLErrorDomain + code:code + userInfo:nil]; + PHGLog(@"Server responded with unexpected HTTP code.", error); + failure(error); + } }]; From ed38ad5292891eecc42206911d03aa52e0ffef35 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 6 Feb 2023 21:47:51 -0500 Subject: [PATCH 2/3] handle error --- PostHog/Classes/PHGPayloadManager.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PostHog/Classes/PHGPayloadManager.m b/PostHog/Classes/PHGPayloadManager.m index 94d4e2c4bb..f478dfbd2c 100644 --- a/PostHog/Classes/PHGPayloadManager.m +++ b/PostHog/Classes/PHGPayloadManager.m @@ -196,7 +196,7 @@ - (void)reloadFeatureFlags NSDictionary *flagPayloads = [responseDict objectForKey:@"featureFlagPayloads"]; [self receivedFeatureFlags:flags payloads:flagPayloads]; } failure:^(NSError * _Nonnull error) { -// TODO: handle error + return; }]; } From 5737df318b63eb9f61fbd90172f88939d54e0c91 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 13 Feb 2023 20:46:58 -0500 Subject: [PATCH 3/3] add test --- PostHog/Internal/PHGHTTPClient.m | 1 - PostHogTests/FeatureFlagTests.swift | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/PostHog/Internal/PHGHTTPClient.m b/PostHog/Internal/PHGHTTPClient.m index ee26dad049..10ced1ffcf 100644 --- a/PostHog/Internal/PHGHTTPClient.m +++ b/PostHog/Internal/PHGHTTPClient.m @@ -145,7 +145,6 @@ - (NSURLSessionDataTask *)sharedSessionUpload:(NSDictionary *)payload host:(NSUR NSInteger code = ((NSHTTPURLResponse *)response).statusCode; if (code < 300) { NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; - NSLog(@"%@",json); success(json); } NSError *error = [NSError errorWithDomain:NSURLErrorDomain diff --git a/PostHogTests/FeatureFlagTests.swift b/PostHogTests/FeatureFlagTests.swift index 38c18257e6..95ade4f73f 100644 --- a/PostHogTests/FeatureFlagTests.swift +++ b/PostHogTests/FeatureFlagTests.swift @@ -203,6 +203,25 @@ class FeatureFlagTests: QuickSpec { let flagValue = posthog.getFeatureFlag("some-flag") expect(flagValue).to(be("variant-1")) } + + it("bad request does not override current flags") { + _ = stubRequest("POST", "https://app.posthog.test/decide/?v=3" as LSMatcheable) + .andReturn(200)? + .withBody("{\"featureFlags\":{\"some-flag\":\"true\"}}" as LSHTTPBody); + posthog.reloadFeatureFlags() + // Hacky: Need to buffer for async request to happen without stub being cleaned up + sleep(1) + let isEnabled = posthog.isFeatureEnabled("some-flag") + expect(isEnabled).to(beTrue()) + + _ = stubRequest("POST", "https://app.posthog.test/decide/?v=3" as LSMatcheable) + .andReturn(400); + posthog.reloadFeatureFlags() + // Hacky: Need to buffer for async request to happen without stub being cleaned up + sleep(1) + let secondIsEnabled = posthog.isFeatureEnabled("some-flag") + expect(secondIsEnabled).to(beTrue()) + } }