diff --git a/Source/ARTRest+Private.h b/Source/ARTRest+Private.h index be2f284ab..8f2014ff6 100644 --- a/Source/ARTRest+Private.h +++ b/Source/ARTRest+Private.h @@ -21,6 +21,8 @@ ART_ASSUME_NONNULL_BEGIN @property (readonly, strong, nonatomic) __GENERIC(id, ARTEncoder) defaultEncoder; @property (readonly, strong, nonatomic) NSString *defaultEncoding; //Content-Type @property (readonly, strong, nonatomic) NSDictionary *encoders; + +// Private prioritized host for testing only (overrides the current `restHost`) @property (readwrite, strong, nonatomic, art_nullable) NSString *prioritizedHost; @property (nonatomic, strong) id httpExecutor; diff --git a/Source/ARTRest.m b/Source/ARTRest.m index 099a56f60..9a65dfb22 100644 --- a/Source/ARTRest.m +++ b/Source/ARTRest.m @@ -153,7 +153,7 @@ - (void)executeRequest:(NSMutableURLRequest *)request completion:(void (^)(NSHTT } } if (retries < _options.httpMaxRetryCount && [self shouldRetryWithFallback:request response:response error:error]) { - if (!blockFallbacks && [request.URL.host isEqualToString:(_prioritizedHost ? _prioritizedHost : [ARTDefault restHost])]) { + if (!blockFallbacks && [request.URL.host isEqualToString:[self currentHost]]) { blockFallbacks = [[ARTFallback alloc] initWithOptions:_options]; } if (blockFallbacks) { @@ -189,6 +189,14 @@ - (BOOL)shouldRetryWithFallback:(NSMutableURLRequest *)request response:(NSHTTPU return NO; } +- (NSString *)currentHost { + if (_prioritizedHost) { + // Test purpose only + return _prioritizedHost; + } + return self.options.restHost; +} + - (void)prepareAuthorisationHeader:(ARTAuthMethod)method completion:(void (^)(NSString *authorization, NSError *error))callback { [self prepareAuthorisationHeader:method force:NO completion:callback]; } diff --git a/Spec/RestClient.swift b/Spec/RestClient.swift index 487115620..e65faee50 100644 --- a/Spec/RestClient.swift +++ b/Spec/RestClient.swift @@ -786,7 +786,7 @@ class RestClient: QuickSpec { } // RSC15e - it("every new HTTP request is first attempted to the primary host rest.ably.io") { + it("every new HTTP request is first attempted to the default primary host rest.ably.io") { let options = ARTClientOptions(key: "xxxx:xxxx") options.httpMaxRetryCount = 1 let client = ARTRest(options: options) @@ -812,9 +812,44 @@ class RestClient: QuickSpec { if testHTTPExecutor.requests.count != 3 { return } - expect(NSRegularExpression.match(testHTTPExecutor.requests[0].URL!.absoluteString, pattern: "//rest.ably.io")).to(beTrue()) + + expect(NSRegularExpression.match(testHTTPExecutor.requests[0].URL!.absoluteString, pattern: "//\(ARTDefault.restHost())")).to(beTrue()) + expect(NSRegularExpression.match(testHTTPExecutor.requests[1].URL!.absoluteString, pattern: "//[a-e].ably-realtime.com")).to(beTrue()) + expect(NSRegularExpression.match(testHTTPExecutor.requests[2].URL!.absoluteString, pattern: "//\(ARTDefault.restHost())")).to(beTrue()) + } + + // RSC15e + it("if ClientOptions#restHost is set then every new HTTP request should first attempt ClientOptions#restHost") { + let options = ARTClientOptions(key: "xxxx:xxxx") + options.httpMaxRetryCount = 1 + options.restHost = "fake.ably.io" + let client = ARTRest(options: options) + client.httpExecutor = testHTTPExecutor + testHTTPExecutor.http = MockHTTP(network: .HostUnreachable) + let channel = client.channels.get("test") + + waitUntil(timeout: testTimeout) { done in + channel.publish(nil, data: "nil") { _ in + done() + } + } + + testHTTPExecutor.http = ARTHttp() + + waitUntil(timeout: testTimeout) { done in + channel.publish(nil, data: "nil") { _ in + done() + } + } + + expect(testHTTPExecutor.requests).to(haveCount(3)) + if testHTTPExecutor.requests.count != 3 { + return + } + + expect(NSRegularExpression.match(testHTTPExecutor.requests[0].URL!.absoluteString, pattern: "//\(client.options.restHost)")).to(beTrue()) expect(NSRegularExpression.match(testHTTPExecutor.requests[1].URL!.absoluteString, pattern: "//[a-e].ably-realtime.com")).to(beTrue()) - expect(NSRegularExpression.match(testHTTPExecutor.requests[2].URL!.absoluteString, pattern: "//rest.ably.io")).to(beTrue()) + expect(NSRegularExpression.match(testHTTPExecutor.requests[2].URL!.absoluteString, pattern: "//\(client.options.restHost)")).to(beTrue()) } // RSC15a