Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #import <UIKit/UIKit.h> |
| 35 | #import <XCTest/XCTest.h> |
| 36 | |
Jorge Canizales | c42a38e | 2015-06-21 14:44:25 -0700 | [diff] [blame] | 37 | #import <GRPCClient/GRPCCall.h> |
Jorge Canizales | 631dc00 | 2015-08-06 23:08:41 -0700 | [diff] [blame] | 38 | #import <GRPCClient/GRPCCall+OAuth2.h> |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 39 | #import <GRPCClient/GRPCCall+Tests.h> |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 40 | #import <ProtoRPC/ProtoMethod.h> |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 41 | #import <RemoteTest/Messages.pbobjc.h> |
Jorge Canizales | c42a38e | 2015-06-21 14:44:25 -0700 | [diff] [blame] | 42 | #import <RxLibrary/GRXWriteable.h> |
| 43 | #import <RxLibrary/GRXWriter+Immediate.h> |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 44 | |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 45 | static NSString * const kHostAddress = @"localhost:5050"; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 46 | static NSString * const kPackage = @"grpc.testing"; |
| 47 | static NSString * const kService = @"TestService"; |
| 48 | |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 49 | static ProtoMethod *kInexistentMethod; |
| 50 | static ProtoMethod *kEmptyCallMethod; |
| 51 | static ProtoMethod *kUnaryCallMethod; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 52 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 53 | /** Observer class for testing that responseMetadata is KVO-compliant */ |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 54 | @interface PassthroughObserver : NSObject |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 55 | - (instancetype) initWithCallback:(void (^)(NSString*, id, NSDictionary*))callback |
| 56 | NS_DESIGNATED_INITIALIZER; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 57 | |
| 58 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change |
| 59 | context:(void *)context; |
| 60 | @end |
| 61 | |
| 62 | @implementation PassthroughObserver { |
| 63 | void (^_callback)(NSString*, id, NSDictionary*); |
| 64 | } |
| 65 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 66 | - (instancetype)init { |
| 67 | return [self initWithCallback:nil]; |
| 68 | } |
| 69 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 70 | - (instancetype)initWithCallback:(void (^)(NSString *, id, NSDictionary *))callback { |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 71 | if (!callback) { |
| 72 | return nil; |
| 73 | } |
| 74 | if ((self = [super init])) { |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 75 | _callback = callback; |
| 76 | } |
| 77 | return self; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 80 | - (void)observeValueForKeyPath:(NSString *)keyPath |
| 81 | ofObject:(id)object |
| 82 | change:(NSDictionary *)change |
| 83 | context:(void *)context { |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 84 | _callback(keyPath, object, change); |
murgatroid99 | 2e49a35 | 2015-08-24 14:40:45 -0700 | [diff] [blame] | 85 | [object removeObserver:self forKeyPath:keyPath]; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @end |
| 89 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 90 | # pragma mark Tests |
| 91 | |
| 92 | /** |
| 93 | * A few tests similar to InteropTests, but which use the generic gRPC client (GRPCCall) rather than |
| 94 | * a generated proto library on top of it. Its RPCs are sent to a local cleartext server. |
| 95 | * |
| 96 | * TODO(jcanizales): Run them also against a local SSL server and against a remote server. |
| 97 | */ |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 98 | @interface GRPCClientTests : XCTestCase |
| 99 | @end |
| 100 | |
| 101 | @implementation GRPCClientTests |
| 102 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 103 | - (void)setUp { |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 104 | // Register test server as non-SSL. |
| 105 | [GRPCCall useInsecureConnectionsForHost:kHostAddress]; |
| 106 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 107 | // This method isn't implemented by the remote server. |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 108 | kInexistentMethod = [[ProtoMethod alloc] initWithPackage:kPackage |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 109 | service:kService |
| 110 | method:@"Inexistent"]; |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 111 | kEmptyCallMethod = [[ProtoMethod alloc] initWithPackage:kPackage |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 112 | service:kService |
| 113 | method:@"EmptyCall"]; |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 114 | kUnaryCallMethod = [[ProtoMethod alloc] initWithPackage:kPackage |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 115 | service:kService |
| 116 | method:@"UnaryCall"]; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 119 | - (void)testConnectionToRemoteServer { |
| 120 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; |
| 121 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 122 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 123 | path:kInexistentMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 124 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 125 | |
| 126 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 127 | XCTFail(@"Received unexpected response: %@", value); |
| 128 | } completionHandler:^(NSError *errorOrNil) { |
| 129 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
| 130 | // TODO(jcanizales): The server should return code 12 UNIMPLEMENTED, not 5 NOT FOUND. |
| 131 | XCTAssertEqual(errorOrNil.code, 5, @"Finished with unexpected error: %@", errorOrNil); |
| 132 | [expectation fulfill]; |
| 133 | }]; |
| 134 | |
| 135 | [call startWithWriteable:responsesWriteable]; |
| 136 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 137 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | - (void)testEmptyRPC { |
| 141 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 142 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 143 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 144 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 145 | path:kEmptyCallMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 146 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 147 | |
| 148 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 149 | XCTAssertNotNil(value, @"nil value received as response."); |
| 150 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 151 | [response fulfill]; |
| 152 | } completionHandler:^(NSError *errorOrNil) { |
| 153 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 154 | [completion fulfill]; |
| 155 | }]; |
| 156 | |
| 157 | [call startWithWriteable:responsesWriteable]; |
| 158 | |
Jorge Canizales | 83c57cb | 2015-08-09 16:36:49 -0700 | [diff] [blame] | 159 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | - (void)testSimpleProtoRPC { |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 163 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Expected response."]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 164 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."]; |
| 165 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 166 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 167 | request.responseSize = 100; |
| 168 | request.fillUsername = YES; |
| 169 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 170 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 171 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 172 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 173 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 174 | requestsWriter:requestsWriter]; |
| 175 | |
| 176 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 177 | XCTAssertNotNil(value, @"nil value received as response."); |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 178 | XCTAssertGreaterThan(value.length, 0, @"Empty response received."); |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 179 | RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 180 | // We expect empty strings, not nil: |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 181 | XCTAssertNotNil(responseProto.username, @"Response's username is nil."); |
| 182 | XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil."); |
| 183 | [response fulfill]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 184 | } completionHandler:^(NSError *errorOrNil) { |
| 185 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 186 | [completion fulfill]; |
| 187 | }]; |
| 188 | |
| 189 | [call startWithWriteable:responsesWriteable]; |
| 190 | |
Jorge Canizales | 83c57cb | 2015-08-09 16:36:49 -0700 | [diff] [blame] | 191 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 194 | // TODO(jcanizales): Activate this test against the remote server. |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 195 | - (void)testMetadata { |
| 196 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"RPC unauthorized."]; |
| 197 | |
| 198 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
| 199 | request.fillUsername = YES; |
| 200 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 201 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 202 | |
| 203 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 204 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 205 | requestsWriter:requestsWriter]; |
| 206 | |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 207 | call.oauth2AccessToken = @"bogusToken"; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 208 | |
| 209 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 210 | XCTFail(@"Received unexpected response: %@", value); |
| 211 | } completionHandler:^(NSError *errorOrNil) { |
| 212 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
| 213 | XCTAssertEqual(errorOrNil.code, 16, @"Finished with unexpected error: %@", errorOrNil); |
Jorge Canizales | 1ab2a71 | 2015-08-12 20:32:11 -0700 | [diff] [blame] | 214 | XCTAssertEqualObjects(call.responseHeaders, errorOrNil.userInfo[kGRPCHeadersKey], |
| 215 | @"Headers in the NSError object and call object differ."); |
| 216 | XCTAssertEqualObjects(call.responseTrailers, errorOrNil.userInfo[kGRPCTrailersKey], |
| 217 | @"Trailers in the NSError object and call object differ."); |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 218 | NSString *challengeHeader = call.oauth2ChallengeHeader; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 219 | XCTAssertGreaterThan(challengeHeader.length, 0, |
Jorge Canizales | 1ab2a71 | 2015-08-12 20:32:11 -0700 | [diff] [blame] | 220 | @"No challenge in response headers %@", call.responseHeaders); |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 221 | [expectation fulfill]; |
| 222 | }]; |
| 223 | |
| 224 | [call startWithWriteable:responsesWriteable]; |
| 225 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 226 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 227 | } |
| 228 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 229 | - (void)testResponseMetadataKVO { |
| 230 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 231 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 232 | __weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."]; |
| 233 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 234 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
| 235 | path:kEmptyCallMethod.HTTPPath |
| 236 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
| 237 | |
murgatroid99 | 2e49a35 | 2015-08-24 14:40:45 -0700 | [diff] [blame] | 238 | PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) { |
| 239 | if ([keypath isEqual: @"responseHeaders"]) { |
| 240 | [metadata fulfill]; |
| 241 | } |
| 242 | }]; |
| 243 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 244 | [call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL]; |
| 245 | |
| 246 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 247 | XCTAssertNotNil(value, @"nil value received as response."); |
| 248 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 249 | [response fulfill]; |
| 250 | } completionHandler:^(NSError *errorOrNil) { |
| 251 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 252 | [completion fulfill]; |
| 253 | }]; |
| 254 | |
| 255 | [call startWithWriteable:responsesWriteable]; |
| 256 | |
| 257 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
| 258 | } |
| 259 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 260 | @end |