Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RSC15e for 0.9 #514

Merged
merged 1 commit into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Source/ARTRest+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ARTHTTPExecutor> httpExecutor;
Expand Down
10 changes: 9 additions & 1 deletion Source/ARTRest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
}
Expand Down
41 changes: 38 additions & 3 deletions Spec/RestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down