Skip to content

Commit

Permalink
Merge pull request #363 from ably/rsl6b
Browse files Browse the repository at this point in the history
RSL6b
  • Loading branch information
tcard committed Apr 5, 2016
2 parents 5a22247 + e91194f commit 9337a67
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Source/ARTRestChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ - (BOOL)history:(ARTDataQuery *)query callback:(void(^)(__GENERIC(ARTPaginatedRe
ARTPaginatedResultResponseProcessor responseProcessor = ^NSArray *(NSHTTPURLResponse *response, NSData *data) {
id<ARTEncoder> encoder = [_rest.encoders objectForKey:response.MIMEType];
return [[encoder decodeMessages:data] artMap:^(ARTMessage *message) {
return [message decodeWithEncoder:self.dataEncoder error:nil];
NSError *error;
message = [message decodeWithEncoder:self.dataEncoder error:&error];
if (error != nil) {
ARTErrorInfo *errorInfo = [ARTErrorInfo wrap:(ARTErrorInfo *)error.userInfo[NSLocalizedFailureReasonErrorKey] prepend:@"Failed to decode data: "];
[self.logger error:@"%@", errorInfo.message];
}
return message;
}];
};

Expand Down
3 changes: 0 additions & 3 deletions Spec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,6 @@ class RealtimeClientChannel: QuickSpec {
}

waitUntil(timeout: testTimeout) { done in
let logTime = NSDate()
var callbacks = 2
channel.subscribe(testMessage.encoded.name) { message in
expect(message.data as? String).to(equal(testMessage.encrypted.data))
Expand Down Expand Up @@ -1289,8 +1288,6 @@ class RealtimeClientChannel: QuickSpec {
}

waitUntil(timeout: testTimeout) { done in
let logTime = NSDate()

channel.subscribe { message in
// Last decoding failed: NSData -> JSON object, so...
expect(message.data as? NSData).to(equal(expectedData))
Expand Down
73 changes: 73 additions & 0 deletions Spec/RestClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class RestClientChannel: QuickSpec {
}
}

// RSL4
describe("message encoding") {

struct TestCase {
Expand Down Expand Up @@ -457,5 +458,77 @@ class RestClientChannel: QuickSpec {
}
}
}

// RSL6
describe("message decoding") {

// RSL6b
it("should deliver with a binary payload when the payload was successfully decoded but it could not be decrypted") {
let options = AblyTests.commonAppSetup()
let clientEncrypted = ARTRest(options: options)

let channelOptions = ARTChannelOptions(cipher: ["key":ARTCrypto.generateRandomKey()])
let channelEncrypted = clientEncrypted.channels.get("test", options: channelOptions)

let expectedMessage = ["something":1]

waitUntil(timeout: testTimeout) { done in
channelEncrypted.publish(nil, data: expectedMessage) { error in
done()
}
}

let client = ARTRest(options: options)
let channel = client.channels.get("test")

waitUntil(timeout: testTimeout) { done in
channel.history { result, error in
let message = (result!.items as! [ARTMessage])[0]
expect(message.data is NSData).to(beTrue())
expect(message.encoding).to(equal("json/utf-8/cipher+aes-256-cbc"))
done()
}
}
}

// RSL6b
it("should deliver with encoding attribute set indicating the residual encoding and error should be emitted") {
let options = AblyTests.commonAppSetup()
options.logHandler = ARTLog(capturingOutput: true)
let client = ARTRest(options: options)
let channelOptions = ARTChannelOptions(cipher: ["key":ARTCrypto.generateRandomKey()])
let channel = client.channels.get("test", options: channelOptions)
client.httpExecutor = mockExecutor

let expectedMessage = ["something":1]
let expectedData = try! NSJSONSerialization.dataWithJSONObject(expectedMessage, options: NSJSONWritingOptions(rawValue: 0))

mockExecutor.beforeProcessingDataResponse = { data in
let dataStr = String(data: data!, encoding: NSUTF8StringEncoding)!
return dataStr.replace("json/utf-8", withString: "invalid").dataUsingEncoding(NSUTF8StringEncoding)!
}

waitUntil(timeout: testTimeout) { done in
channel.publish(nil, data: expectedMessage) { error in
done()
}
}

waitUntil(timeout: testTimeout) { done in
channel.history { result, error in
let message = (result!.items as! [ARTMessage])[0]
expect(message.data as? NSData).to(equal(expectedData))
expect(message.encoding).to(equal("invalid"))

let logs = options.logHandler.captured
let line = logs.reduce("") { $0 + "; " + $1.toString() } //Reduce in one line
expect(line).to(contain("ERROR: Failed to decode data: unknown encoding: 'invalid'"))
done()
}
}
}

}

}
}
17 changes: 16 additions & 1 deletion Spec/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,20 @@ class MockHTTPExecutor: NSObject, ARTHTTPExecutor {
var requests: [NSMutableURLRequest] = []
var responses: [NSHTTPURLResponse] = []

var beforeProcessingDataResponse: Optional<(NSData?)->(NSData)> = nil

func executeRequest(request: NSMutableURLRequest, completion callback: ((NSHTTPURLResponse?, NSData?, NSError?) -> Void)?) {
self.requests.append(request)
self.executor.executeRequest(request, completion: { response, data, error in
if let httpResponse = response {
self.responses.append(httpResponse)
}
callback?(response, data, error)
if let performEvent = self.beforeProcessingDataResponse {
callback?(response, performEvent(data), error)
}
else {
callback?(response, data, error)
}
})
}
}
Expand Down Expand Up @@ -653,6 +660,14 @@ extension NSRegularExpression {

}

extension String {

func replace(value: String, withString string: String) -> String {
return self.stringByReplacingOccurrencesOfString(value, withString: string, options: NSStringCompareOptions.LiteralSearch, range: nil)
}

}

extension ARTRealtime {

func simulateLostConnectionAndState() {
Expand Down

0 comments on commit 9337a67

Please sign in to comment.