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; }]; } diff --git a/PostHog/Internal/PHGHTTPClient.m b/PostHog/Internal/PHGHTTPClient.m index 26d0dfa9b6..10ced1ffcf 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,17 @@ - (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]; + success(json); + } + NSError *error = [NSError errorWithDomain:NSURLErrorDomain + code:code + userInfo:nil]; + PHGLog(@"Server responded with unexpected HTTP code.", error); + failure(error); + } }]; 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()) + } }