Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 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> |
Makarand Dharmapurikar | 1c2890d | 2016-02-24 14:35:46 -0800 | [diff] [blame] | 38 | #import <GRPCClient/GRPCCall+ChannelArg.h> |
Jorge Canizales | 631dc00 | 2015-08-06 23:08:41 -0700 | [diff] [blame] | 39 | #import <GRPCClient/GRPCCall+OAuth2.h> |
Muxi Yan | 8e6aec5 | 2017-02-10 09:40:18 -0800 | [diff] [blame] | 40 | #import <GRPCClient/GRPCCall+Tests.h> |
| 41 | #import <GRPCClient/internal_testing/GRPCCall+InternalTests.h> |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 42 | #import <ProtoRPC/ProtoMethod.h> |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 43 | #import <RemoteTest/Messages.pbobjc.h> |
Jorge Canizales | c42a38e | 2015-06-21 14:44:25 -0700 | [diff] [blame] | 44 | #import <RxLibrary/GRXWriteable.h> |
| 45 | #import <RxLibrary/GRXWriter+Immediate.h> |
Makarand Dharmapurikar | a0414ee | 2016-02-23 16:58:46 -0800 | [diff] [blame] | 46 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 47 | #define TEST_TIMEOUT 16 |
| 48 | |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 49 | static NSString * const kHostAddress = @"localhost:5050"; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 50 | static NSString * const kPackage = @"grpc.testing"; |
| 51 | static NSString * const kService = @"TestService"; |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 52 | static NSString * const kRemoteSSLHost = @"grpc-test.sandbox.googleapis.com"; |
Makarand Dharmapurikar | a0414ee | 2016-02-23 16:58:46 -0800 | [diff] [blame] | 53 | |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 54 | static GRPCProtoMethod *kInexistentMethod; |
| 55 | static GRPCProtoMethod *kEmptyCallMethod; |
| 56 | static GRPCProtoMethod *kUnaryCallMethod; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 57 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 58 | /** Observer class for testing that responseMetadata is KVO-compliant */ |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 59 | @interface PassthroughObserver : NSObject |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 60 | - (instancetype) initWithCallback:(void (^)(NSString*, id, NSDictionary*))callback |
| 61 | NS_DESIGNATED_INITIALIZER; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 62 | |
| 63 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change |
| 64 | context:(void *)context; |
| 65 | @end |
| 66 | |
| 67 | @implementation PassthroughObserver { |
| 68 | void (^_callback)(NSString*, id, NSDictionary*); |
| 69 | } |
| 70 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 71 | - (instancetype)init { |
| 72 | return [self initWithCallback:nil]; |
| 73 | } |
| 74 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 75 | - (instancetype)initWithCallback:(void (^)(NSString *, id, NSDictionary *))callback { |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 76 | if (!callback) { |
| 77 | return nil; |
| 78 | } |
| 79 | if ((self = [super init])) { |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 80 | _callback = callback; |
| 81 | } |
| 82 | return self; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 85 | - (void)observeValueForKeyPath:(NSString *)keyPath |
| 86 | ofObject:(id)object |
| 87 | change:(NSDictionary *)change |
| 88 | context:(void *)context { |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 89 | _callback(keyPath, object, change); |
murgatroid99 | 2e49a35 | 2015-08-24 14:40:45 -0700 | [diff] [blame] | 90 | [object removeObserver:self forKeyPath:keyPath]; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @end |
| 94 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 95 | # pragma mark Tests |
| 96 | |
| 97 | /** |
| 98 | * A few tests similar to InteropTests, but which use the generic gRPC client (GRPCCall) rather than |
| 99 | * a generated proto library on top of it. Its RPCs are sent to a local cleartext server. |
| 100 | * |
| 101 | * TODO(jcanizales): Run them also against a local SSL server and against a remote server. |
| 102 | */ |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 103 | @interface GRPCClientTests : XCTestCase |
| 104 | @end |
| 105 | |
| 106 | @implementation GRPCClientTests |
| 107 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 108 | - (void)setUp { |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 109 | // Add a custom user agent prefix that will be used in test |
| 110 | [GRPCCall setUserAgentPrefix:@"Foo" forHost:kHostAddress]; |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 111 | // Register test server as non-SSL. |
| 112 | [GRPCCall useInsecureConnectionsForHost:kHostAddress]; |
| 113 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 114 | // This method isn't implemented by the remote server. |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 115 | kInexistentMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 116 | service:kService |
| 117 | method:@"Inexistent"]; |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 118 | kEmptyCallMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 119 | service:kService |
| 120 | method:@"EmptyCall"]; |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 121 | kUnaryCallMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 122 | service:kService |
| 123 | method:@"UnaryCall"]; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 126 | - (void)testConnectionToRemoteServer { |
| 127 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; |
| 128 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 129 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 130 | path:kInexistentMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 131 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 132 | |
| 133 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 134 | XCTFail(@"Received unexpected response: %@", value); |
| 135 | } completionHandler:^(NSError *errorOrNil) { |
| 136 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 137 | XCTAssertEqual(errorOrNil.code, 12, @"Finished with unexpected error: %@", errorOrNil); |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 138 | [expectation fulfill]; |
| 139 | }]; |
| 140 | |
| 141 | [call startWithWriteable:responsesWriteable]; |
| 142 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 143 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | - (void)testEmptyRPC { |
| 147 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 148 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 149 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 150 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 151 | path:kEmptyCallMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 152 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 153 | |
| 154 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 155 | XCTAssertNotNil(value, @"nil value received as response."); |
| 156 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 157 | [response fulfill]; |
| 158 | } completionHandler:^(NSError *errorOrNil) { |
| 159 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 160 | [completion fulfill]; |
| 161 | }]; |
| 162 | |
| 163 | [call startWithWriteable:responsesWriteable]; |
| 164 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 165 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | - (void)testSimpleProtoRPC { |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 169 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Expected response."]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 170 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."]; |
| 171 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 172 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 173 | request.responseSize = 100; |
| 174 | request.fillUsername = YES; |
| 175 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 176 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 177 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 178 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 179 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 180 | requestsWriter:requestsWriter]; |
| 181 | |
| 182 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 183 | XCTAssertNotNil(value, @"nil value received as response."); |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 184 | XCTAssertGreaterThan(value.length, 0, @"Empty response received."); |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 185 | RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 186 | // We expect empty strings, not nil: |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 187 | XCTAssertNotNil(responseProto.username, @"Response's username is nil."); |
| 188 | XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil."); |
| 189 | [response fulfill]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 190 | } completionHandler:^(NSError *errorOrNil) { |
| 191 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 192 | [completion fulfill]; |
| 193 | }]; |
| 194 | |
| 195 | [call startWithWriteable:responsesWriteable]; |
| 196 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 197 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 200 | - (void)testMetadata { |
| 201 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"RPC unauthorized."]; |
| 202 | |
| 203 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
| 204 | request.fillUsername = YES; |
| 205 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 206 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 207 | |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 208 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kRemoteSSLHost |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 209 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 210 | requestsWriter:requestsWriter]; |
| 211 | |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 212 | call.oauth2AccessToken = @"bogusToken"; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 213 | |
| 214 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 215 | XCTFail(@"Received unexpected response: %@", value); |
| 216 | } completionHandler:^(NSError *errorOrNil) { |
| 217 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
| 218 | XCTAssertEqual(errorOrNil.code, 16, @"Finished with unexpected error: %@", errorOrNil); |
Jorge Canizales | 1ab2a71 | 2015-08-12 20:32:11 -0700 | [diff] [blame] | 219 | XCTAssertEqualObjects(call.responseHeaders, errorOrNil.userInfo[kGRPCHeadersKey], |
| 220 | @"Headers in the NSError object and call object differ."); |
| 221 | XCTAssertEqualObjects(call.responseTrailers, errorOrNil.userInfo[kGRPCTrailersKey], |
| 222 | @"Trailers in the NSError object and call object differ."); |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 223 | NSString *challengeHeader = call.oauth2ChallengeHeader; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 224 | XCTAssertGreaterThan(challengeHeader.length, 0, |
Jorge Canizales | 1ab2a71 | 2015-08-12 20:32:11 -0700 | [diff] [blame] | 225 | @"No challenge in response headers %@", call.responseHeaders); |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 226 | [expectation fulfill]; |
| 227 | }]; |
| 228 | |
| 229 | [call startWithWriteable:responsesWriteable]; |
| 230 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 231 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 232 | } |
| 233 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 234 | - (void)testResponseMetadataKVO { |
| 235 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 236 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 237 | __weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."]; |
| 238 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 239 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
| 240 | path:kEmptyCallMethod.HTTPPath |
| 241 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
| 242 | |
murgatroid99 | 2e49a35 | 2015-08-24 14:40:45 -0700 | [diff] [blame] | 243 | PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) { |
| 244 | if ([keypath isEqual: @"responseHeaders"]) { |
| 245 | [metadata fulfill]; |
| 246 | } |
| 247 | }]; |
| 248 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 249 | [call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL]; |
| 250 | |
| 251 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 252 | XCTAssertNotNil(value, @"nil value received as response."); |
| 253 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 254 | [response fulfill]; |
| 255 | } completionHandler:^(NSError *errorOrNil) { |
| 256 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 257 | [completion fulfill]; |
| 258 | }]; |
| 259 | |
| 260 | [call startWithWriteable:responsesWriteable]; |
| 261 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 262 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 265 | - (void)testUserAgentPrefix { |
| 266 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 267 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 268 | |
| 269 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
| 270 | path:kEmptyCallMethod.HTTPPath |
| 271 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
| 272 | // Setting this special key in the header will cause the interop server to echo back the |
| 273 | // user-agent value, which we confirm. |
| 274 | call.requestHeaders[@"x-grpc-test-echo-useragent"] = @""; |
| 275 | |
| 276 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 277 | XCTAssertNotNil(value, @"nil value received as response."); |
| 278 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
Nicolas "Pixel" Noble | aad4156 | 2016-03-11 22:21:11 +0100 | [diff] [blame] | 279 | /* This test needs to be more clever in regards to changing the version of the core. |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 280 | XCTAssertEqualObjects(call.responseHeaders[@"x-grpc-test-echo-useragent"], |
Makarand Dharmapurikar | f1588b5 | 2016-02-26 17:05:47 -0800 | [diff] [blame] | 281 | @"Foo grpc-objc/0.13.0 grpc-c/0.14.0-dev (ios)", |
| 282 | @"Did not receive expected user agent %@", |
| 283 | call.responseHeaders[@"x-grpc-test-echo-useragent"]); |
Nicolas "Pixel" Noble | aad4156 | 2016-03-11 22:21:11 +0100 | [diff] [blame] | 284 | */ |
Makarand Dharmapurikar | c9dbf64 | 2016-02-26 16:59:08 -0800 | [diff] [blame] | 285 | [response fulfill]; |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 286 | } completionHandler:^(NSError *errorOrNil) { |
| 287 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 288 | [completion fulfill]; |
| 289 | }]; |
| 290 | |
| 291 | [call startWithWriteable:responsesWriteable]; |
| 292 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 293 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Makarand Dharmapurikar | 716c7c5 | 2017-05-09 13:04:41 -0700 | [diff] [blame] | 296 | - (void)testTrailers { |
| 297 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 298 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 299 | |
| 300 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
| 301 | path:kEmptyCallMethod.HTTPPath |
| 302 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
| 303 | // Setting this special key in the header will cause the interop server to echo back the |
| 304 | // trailer data. |
| 305 | const unsigned char raw_bytes[] = {1,2,3,4}; |
| 306 | NSData *trailer_data = [NSData dataWithBytes:raw_bytes length:sizeof(raw_bytes)]; |
| 307 | call.requestHeaders[@"x-grpc-test-echo-trailing-bin"] = trailer_data; |
| 308 | |
| 309 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 310 | XCTAssertNotNil(value, @"nil value received as response."); |
| 311 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 312 | [response fulfill]; |
| 313 | } completionHandler:^(NSError *errorOrNil) { |
| 314 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 315 | XCTAssertEqualObjects((NSData *)call.responseTrailers[@"x-grpc-test-echo-trailing-bin"], |
| 316 | trailer_data, |
| 317 | @"Did not receive expected trailer"); |
| 318 | [completion fulfill]; |
| 319 | }]; |
| 320 | |
| 321 | [call startWithWriteable:responsesWriteable]; |
| 322 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
| 323 | } |
| 324 | |
Makarand Dharmapurikar | 63f2b5b | 2016-02-24 13:54:48 -0800 | [diff] [blame] | 325 | // TODO(makarandd): Move to a different file that contains only unit tests |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 326 | - (void)testExceptions { |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 327 | // Try to set parameters to nil for GRPCCall. This should cause an exception |
| 328 | @try { |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 329 | (void)[[GRPCCall alloc] initWithHost:nil |
| 330 | path:nil |
| 331 | requestsWriter:nil]; |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 332 | XCTFail(@"Did not receive an exception when parameters are nil"); |
| 333 | } @catch(NSException *theException) { |
| 334 | NSLog(@"Received exception as expected: %@", theException.name); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | // Set state to Finished by force |
| 339 | GRXWriter *requestsWriter = [GRXWriter emptyWriter]; |
| 340 | [requestsWriter finishWithError:nil]; |
| 341 | @try { |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 342 | (void)[[GRPCCall alloc] initWithHost:kHostAddress |
| 343 | path:kUnaryCallMethod.HTTPPath |
| 344 | requestsWriter:requestsWriter]; |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 345 | XCTFail(@"Did not receive an exception when GRXWriter has incorrect state."); |
| 346 | } @catch(NSException *theException) { |
| 347 | NSLog(@"Received exception as expected: %@", theException.name); |
| 348 | } |
| 349 | |
| 350 | } |
| 351 | |
Muxi Yan | 1bd5c77 | 2016-09-30 15:13:58 -0700 | [diff] [blame] | 352 | - (void)testIdempotentProtoRPC { |
| 353 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Expected response."]; |
| 354 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."]; |
| 355 | |
| 356 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
| 357 | request.responseSize = 100; |
| 358 | request.fillUsername = YES; |
| 359 | request.fillOauthScope = YES; |
| 360 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
| 361 | |
| 362 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
| 363 | path:kUnaryCallMethod.HTTPPath |
| 364 | requestsWriter:requestsWriter]; |
Muxi Yan | 8739e80 | 2016-10-03 09:41:31 -0700 | [diff] [blame] | 365 | [GRPCCall setCallSafety:GRPCCallSafetyIdempotentRequest host:kHostAddress path:kUnaryCallMethod.HTTPPath]; |
Muxi Yan | 1bd5c77 | 2016-09-30 15:13:58 -0700 | [diff] [blame] | 366 | |
| 367 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 368 | XCTAssertNotNil(value, @"nil value received as response."); |
| 369 | XCTAssertGreaterThan(value.length, 0, @"Empty response received."); |
| 370 | RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL]; |
| 371 | // We expect empty strings, not nil: |
| 372 | XCTAssertNotNil(responseProto.username, @"Response's username is nil."); |
| 373 | XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil."); |
| 374 | [response fulfill]; |
| 375 | } completionHandler:^(NSError *errorOrNil) { |
| 376 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 377 | [completion fulfill]; |
| 378 | }]; |
| 379 | |
| 380 | [call startWithWriteable:responsesWriteable]; |
| 381 | |
Muxi Yan | b1341a3 | 2016-12-05 16:38:02 -0800 | [diff] [blame] | 382 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
Muxi Yan | 1bd5c77 | 2016-09-30 15:13:58 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Muxi Yan | b965ff6 | 2017-04-19 14:27:47 -0700 | [diff] [blame] | 385 | - (void)testAlternateDispatchQueue { |
| 386 | const int32_t kPayloadSize = 100; |
| 387 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
| 388 | request.responseSize = kPayloadSize; |
| 389 | |
| 390 | __weak XCTestExpectation *expectation1 = [self expectationWithDescription:@"AlternateDispatchQueue1"]; |
| 391 | |
| 392 | // Use default (main) dispatch queue |
| 393 | NSString *main_queue_label = [NSString stringWithUTF8String:dispatch_queue_get_label(dispatch_get_main_queue())]; |
| 394 | |
| 395 | GRXWriter *requestsWriter1 = [GRXWriter writerWithValue:[request data]]; |
| 396 | |
| 397 | GRPCCall *call1 = [[GRPCCall alloc] initWithHost:kHostAddress |
Muxi Yan | fd00263 | 2017-04-20 16:16:49 -0700 | [diff] [blame] | 398 | path:kUnaryCallMethod.HTTPPath |
| 399 | requestsWriter:requestsWriter1]; |
Muxi Yan | b965ff6 | 2017-04-19 14:27:47 -0700 | [diff] [blame] | 400 | |
| 401 | id<GRXWriteable> responsesWriteable1 = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 402 | NSString *label = [NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)]; |
| 403 | XCTAssert([label isEqualToString:main_queue_label]); |
| 404 | |
| 405 | [expectation1 fulfill]; |
| 406 | } completionHandler:^(NSError *errorOrNil) { |
| 407 | }]; |
| 408 | |
| 409 | [call1 startWithWriteable:responsesWriteable1]; |
| 410 | |
| 411 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
| 412 | |
| 413 | // Use a custom queue |
| 414 | __weak XCTestExpectation *expectation2 = [self expectationWithDescription:@"AlternateDispatchQueue2"]; |
| 415 | |
| 416 | NSString *queue_label = @"test.queue1"; |
| 417 | dispatch_queue_t queue = dispatch_queue_create([queue_label UTF8String], DISPATCH_QUEUE_SERIAL); |
| 418 | |
| 419 | GRXWriter *requestsWriter2 = [GRXWriter writerWithValue:[request data]]; |
| 420 | |
| 421 | GRPCCall *call2 = [[GRPCCall alloc] initWithHost:kHostAddress |
| 422 | path:kUnaryCallMethod.HTTPPath |
| 423 | requestsWriter:requestsWriter2]; |
| 424 | |
| 425 | [call2 setResponseDispatchQueue:queue]; |
| 426 | |
| 427 | id<GRXWriteable> responsesWriteable2 = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 428 | NSString *label = [NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)]; |
| 429 | XCTAssert([label isEqualToString:queue_label]); |
| 430 | |
| 431 | [expectation2 fulfill]; |
| 432 | } completionHandler:^(NSError *errorOrNil) { |
| 433 | }]; |
| 434 | |
| 435 | [call2 startWithWriteable:responsesWriteable2]; |
| 436 | |
| 437 | [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
| 438 | } |
| 439 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 440 | @end |