diff options
author | Michael Lumish <mlumish@google.com> | 2015-08-25 14:08:43 -0700 |
---|---|---|
committer | Michael Lumish <mlumish@google.com> | 2015-08-25 14:08:43 -0700 |
commit | 04babecd7254afa11c3dc7f737924ee7af5e3b09 (patch) | |
tree | fe243232745d0c7f2b4bc32da02547837f8df06a /src | |
parent | 4087ea6f9c543ec79436c45281475fefe4e33a8f (diff) | |
parent | 2e49a355b64e2fec5f00995b8ba2dba1afef9700 (diff) |
Merge pull request #3043 from murgatroid99/objc_kvo_compliance
Test KVO compliance for call.responseHeaders
Diffstat (limited to 'src')
-rw-r--r-- | src/objective-c/tests/GRPCClientTests.m | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/objective-c/tests/GRPCClientTests.m b/src/objective-c/tests/GRPCClientTests.m index 06581e7599..09a55e0704 100644 --- a/src/objective-c/tests/GRPCClientTests.m +++ b/src/objective-c/tests/GRPCClientTests.m @@ -53,6 +53,37 @@ static ProtoMethod *kInexistentMethod; static ProtoMethod *kEmptyCallMethod; static ProtoMethod *kUnaryCallMethod; +// This is an observer class for testing that responseMetadata is KVO-compliant + +@interface PassthroughObserver : NSObject + +- (instancetype) initWithCallback:(void (^)(NSString*, id, NSDictionary*))callback; + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change + context:(void *)context; +@end + +@implementation PassthroughObserver { + void (^_callback)(NSString*, id, NSDictionary*); +} + +- (instancetype)initWithCallback:(void (^)(NSString *, id, NSDictionary *))callback { + self = [super init]; + if (self) { + _callback = callback; + } + return self; + +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + _callback(keyPath, object, change); + [object removeObserver:self forKeyPath:keyPath]; +} + +@end + @interface GRPCClientTests : XCTestCase @end @@ -183,4 +214,35 @@ static ProtoMethod *kUnaryCallMethod; [self waitForExpectationsWithTimeout:4 handler:nil]; } +- (void)testResponseMetadataKVO { + __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; + __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; + __weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."]; + + GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress + path:kEmptyCallMethod.HTTPPath + requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; + + PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) { + if ([keypath isEqual: @"responseHeaders"]) { + [metadata fulfill]; + } + }]; + + [call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL]; + + id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { + XCTAssertNotNil(value, @"nil value received as response."); + XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); + [response fulfill]; + } completionHandler:^(NSError *errorOrNil) { + XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); + [completion fulfill]; + }]; + + [call startWithWriteable:responsesWriteable]; + + [self waitForExpectationsWithTimeout:8 handler:nil]; +} + @end |