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> |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 40 | #import <GRPCClient/GRPCCall+Tests.h> |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 41 | #import <ProtoRPC/ProtoMethod.h> |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 42 | #import <RemoteTest/Messages.pbobjc.h> |
Jorge Canizales | c42a38e | 2015-06-21 14:44:25 -0700 | [diff] [blame] | 43 | #import <RxLibrary/GRXWriteable.h> |
| 44 | #import <RxLibrary/GRXWriter+Immediate.h> |
Makarand Dharmapurikar | a0414ee | 2016-02-23 16:58:46 -0800 | [diff] [blame] | 45 | |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 46 | static NSString * const kHostAddress = @"localhost:5050"; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 47 | static NSString * const kPackage = @"grpc.testing"; |
| 48 | static NSString * const kService = @"TestService"; |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 49 | static NSString * const kRemoteSSLHost = @"grpc-test.sandbox.googleapis.com"; |
Makarand Dharmapurikar | a0414ee | 2016-02-23 16:58:46 -0800 | [diff] [blame] | 50 | |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 51 | static GRPCProtoMethod *kInexistentMethod; |
| 52 | static GRPCProtoMethod *kEmptyCallMethod; |
| 53 | static GRPCProtoMethod *kUnaryCallMethod; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 54 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 55 | /** Observer class for testing that responseMetadata is KVO-compliant */ |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 56 | @interface PassthroughObserver : NSObject |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 57 | - (instancetype) initWithCallback:(void (^)(NSString*, id, NSDictionary*))callback |
| 58 | NS_DESIGNATED_INITIALIZER; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 59 | |
| 60 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change |
| 61 | context:(void *)context; |
| 62 | @end |
| 63 | |
| 64 | @implementation PassthroughObserver { |
| 65 | void (^_callback)(NSString*, id, NSDictionary*); |
| 66 | } |
| 67 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 68 | - (instancetype)init { |
| 69 | return [self initWithCallback:nil]; |
| 70 | } |
| 71 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 72 | - (instancetype)initWithCallback:(void (^)(NSString *, id, NSDictionary *))callback { |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 73 | if (!callback) { |
| 74 | return nil; |
| 75 | } |
| 76 | if ((self = [super init])) { |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 77 | _callback = callback; |
| 78 | } |
| 79 | return self; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 82 | - (void)observeValueForKeyPath:(NSString *)keyPath |
| 83 | ofObject:(id)object |
| 84 | change:(NSDictionary *)change |
| 85 | context:(void *)context { |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 86 | _callback(keyPath, object, change); |
murgatroid99 | 2e49a35 | 2015-08-24 14:40:45 -0700 | [diff] [blame] | 87 | [object removeObserver:self forKeyPath:keyPath]; |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @end |
| 91 | |
Jorge Canizales | 2beb88c | 2015-10-28 15:13:40 -0700 | [diff] [blame] | 92 | # pragma mark Tests |
| 93 | |
| 94 | /** |
| 95 | * A few tests similar to InteropTests, but which use the generic gRPC client (GRPCCall) rather than |
| 96 | * a generated proto library on top of it. Its RPCs are sent to a local cleartext server. |
| 97 | * |
| 98 | * TODO(jcanizales): Run them also against a local SSL server and against a remote server. |
| 99 | */ |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 100 | @interface GRPCClientTests : XCTestCase |
| 101 | @end |
| 102 | |
| 103 | @implementation GRPCClientTests |
| 104 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 105 | - (void)setUp { |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 106 | // Add a custom user agent prefix that will be used in test |
| 107 | [GRPCCall setUserAgentPrefix:@"Foo" forHost:kHostAddress]; |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 108 | // Register test server as non-SSL. |
| 109 | [GRPCCall useInsecureConnectionsForHost:kHostAddress]; |
| 110 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 111 | // This method isn't implemented by the remote server. |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 112 | kInexistentMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 113 | service:kService |
| 114 | method:@"Inexistent"]; |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 115 | kEmptyCallMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 116 | service:kService |
| 117 | method:@"EmptyCall"]; |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 118 | kUnaryCallMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 119 | service:kService |
| 120 | method:@"UnaryCall"]; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 123 | - (void)testConnectionToRemoteServer { |
| 124 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; |
| 125 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 126 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 127 | path:kInexistentMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 128 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 129 | |
| 130 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 131 | XCTFail(@"Received unexpected response: %@", value); |
| 132 | } completionHandler:^(NSError *errorOrNil) { |
| 133 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 134 | XCTAssertEqual(errorOrNil.code, 12, @"Finished with unexpected error: %@", errorOrNil); |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 135 | [expectation fulfill]; |
| 136 | }]; |
| 137 | |
| 138 | [call startWithWriteable:responsesWriteable]; |
| 139 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 140 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | - (void)testEmptyRPC { |
| 144 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 145 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 146 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 147 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 148 | path:kEmptyCallMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 149 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 150 | |
| 151 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 152 | XCTAssertNotNil(value, @"nil value received as response."); |
| 153 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 154 | [response fulfill]; |
| 155 | } completionHandler:^(NSError *errorOrNil) { |
| 156 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 157 | [completion fulfill]; |
| 158 | }]; |
| 159 | |
| 160 | [call startWithWriteable:responsesWriteable]; |
| 161 | |
Jorge Canizales | 83c57cb | 2015-08-09 16:36:49 -0700 | [diff] [blame] | 162 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | - (void)testSimpleProtoRPC { |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 166 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Expected response."]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 167 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."]; |
| 168 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 169 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 170 | request.responseSize = 100; |
| 171 | request.fillUsername = YES; |
| 172 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 173 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 174 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 175 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 176 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 177 | requestsWriter:requestsWriter]; |
| 178 | |
| 179 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 180 | XCTAssertNotNil(value, @"nil value received as response."); |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 181 | XCTAssertGreaterThan(value.length, 0, @"Empty response received."); |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 182 | RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 183 | // We expect empty strings, not nil: |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 184 | XCTAssertNotNil(responseProto.username, @"Response's username is nil."); |
| 185 | XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil."); |
| 186 | [response fulfill]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 187 | } completionHandler:^(NSError *errorOrNil) { |
| 188 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 189 | [completion fulfill]; |
| 190 | }]; |
| 191 | |
| 192 | [call startWithWriteable:responsesWriteable]; |
| 193 | |
Jorge Canizales | 83c57cb | 2015-08-09 16:36:49 -0700 | [diff] [blame] | 194 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 197 | - (void)testMetadata { |
| 198 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"RPC unauthorized."]; |
| 199 | |
| 200 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
| 201 | request.fillUsername = YES; |
| 202 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 203 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 204 | |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 205 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kRemoteSSLHost |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 206 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 207 | requestsWriter:requestsWriter]; |
| 208 | |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 209 | call.oauth2AccessToken = @"bogusToken"; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 210 | |
| 211 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 212 | XCTFail(@"Received unexpected response: %@", value); |
| 213 | } completionHandler:^(NSError *errorOrNil) { |
| 214 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
| 215 | XCTAssertEqual(errorOrNil.code, 16, @"Finished with unexpected error: %@", errorOrNil); |
Jorge Canizales | 1ab2a71 | 2015-08-12 20:32:11 -0700 | [diff] [blame] | 216 | XCTAssertEqualObjects(call.responseHeaders, errorOrNil.userInfo[kGRPCHeadersKey], |
| 217 | @"Headers in the NSError object and call object differ."); |
| 218 | XCTAssertEqualObjects(call.responseTrailers, errorOrNil.userInfo[kGRPCTrailersKey], |
| 219 | @"Trailers in the NSError object and call object differ."); |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 220 | NSString *challengeHeader = call.oauth2ChallengeHeader; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 221 | XCTAssertGreaterThan(challengeHeader.length, 0, |
Jorge Canizales | 1ab2a71 | 2015-08-12 20:32:11 -0700 | [diff] [blame] | 222 | @"No challenge in response headers %@", call.responseHeaders); |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 223 | [expectation fulfill]; |
| 224 | }]; |
| 225 | |
| 226 | [call startWithWriteable:responsesWriteable]; |
| 227 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 228 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 229 | } |
| 230 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 231 | - (void)testResponseMetadataKVO { |
| 232 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 233 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 234 | __weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."]; |
| 235 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 236 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
| 237 | path:kEmptyCallMethod.HTTPPath |
| 238 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
| 239 | |
murgatroid99 | 2e49a35 | 2015-08-24 14:40:45 -0700 | [diff] [blame] | 240 | PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) { |
| 241 | if ([keypath isEqual: @"responseHeaders"]) { |
| 242 | [metadata fulfill]; |
| 243 | } |
| 244 | }]; |
| 245 | |
murgatroid99 | 81b4fcb | 2015-08-24 10:56:44 -0700 | [diff] [blame] | 246 | [call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL]; |
| 247 | |
| 248 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 249 | XCTAssertNotNil(value, @"nil value received as response."); |
| 250 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 251 | [response fulfill]; |
| 252 | } completionHandler:^(NSError *errorOrNil) { |
| 253 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 254 | [completion fulfill]; |
| 255 | }]; |
| 256 | |
| 257 | [call startWithWriteable:responsesWriteable]; |
| 258 | |
| 259 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
| 260 | } |
| 261 | |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 262 | - (void)testUserAgentPrefix { |
| 263 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 264 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 265 | |
| 266 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
| 267 | path:kEmptyCallMethod.HTTPPath |
| 268 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
| 269 | // Setting this special key in the header will cause the interop server to echo back the |
| 270 | // user-agent value, which we confirm. |
| 271 | call.requestHeaders[@"x-grpc-test-echo-useragent"] = @""; |
| 272 | |
| 273 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 274 | XCTAssertNotNil(value, @"nil value received as response."); |
| 275 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
Nicolas "Pixel" Noble | aad4156 | 2016-03-11 22:21:11 +0100 | [diff] [blame] | 276 | /* 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] | 277 | XCTAssertEqualObjects(call.responseHeaders[@"x-grpc-test-echo-useragent"], |
Makarand Dharmapurikar | f1588b5 | 2016-02-26 17:05:47 -0800 | [diff] [blame] | 278 | @"Foo grpc-objc/0.13.0 grpc-c/0.14.0-dev (ios)", |
| 279 | @"Did not receive expected user agent %@", |
| 280 | call.responseHeaders[@"x-grpc-test-echo-useragent"]); |
Nicolas "Pixel" Noble | aad4156 | 2016-03-11 22:21:11 +0100 | [diff] [blame] | 281 | */ |
Makarand Dharmapurikar | c9dbf64 | 2016-02-26 16:59:08 -0800 | [diff] [blame] | 282 | [response fulfill]; |
Makarand Dharmapurikar | c2b3490 | 2016-02-26 16:38:17 -0800 | [diff] [blame] | 283 | } completionHandler:^(NSError *errorOrNil) { |
| 284 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 285 | [completion fulfill]; |
| 286 | }]; |
| 287 | |
| 288 | [call startWithWriteable:responsesWriteable]; |
| 289 | |
| 290 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
| 291 | } |
| 292 | |
Makarand Dharmapurikar | 63f2b5b | 2016-02-24 13:54:48 -0800 | [diff] [blame] | 293 | // TODO(makarandd): Move to a different file that contains only unit tests |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 294 | - (void)testExceptions { |
| 295 | // Try to set userAgentPrefix for host that is nil. This should cause |
| 296 | // an exception. |
| 297 | @try { |
| 298 | [GRPCCall setUserAgentPrefix:@"Foo" forHost:nil]; |
| 299 | XCTFail(@"Did not receive an exception when host is nil"); |
| 300 | } @catch(NSException *theException) { |
| 301 | NSLog(@"Received exception as expected: %@", theException.name); |
| 302 | } |
| 303 | |
| 304 | // Try to set parameters to nil for GRPCCall. This should cause an exception |
| 305 | @try { |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 306 | (void)[[GRPCCall alloc] initWithHost:nil |
| 307 | path:nil |
| 308 | requestsWriter:nil]; |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 309 | XCTFail(@"Did not receive an exception when parameters are nil"); |
| 310 | } @catch(NSException *theException) { |
| 311 | NSLog(@"Received exception as expected: %@", theException.name); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | // Set state to Finished by force |
| 316 | GRXWriter *requestsWriter = [GRXWriter emptyWriter]; |
| 317 | [requestsWriter finishWithError:nil]; |
| 318 | @try { |
Yuchen Zeng | d5fd7dd | 2016-06-21 11:13:23 -0700 | [diff] [blame] | 319 | (void)[[GRPCCall alloc] initWithHost:kHostAddress |
| 320 | path:kUnaryCallMethod.HTTPPath |
| 321 | requestsWriter:requestsWriter]; |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 322 | XCTFail(@"Did not receive an exception when GRXWriter has incorrect state."); |
| 323 | } @catch(NSException *theException) { |
| 324 | NSLog(@"Received exception as expected: %@", theException.name); |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 329 | @end |