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 | |
| 45 | // These are a few tests similar to InteropTests, but which use the generic gRPC client (GRPCCall) |
| 46 | // rather than a generated proto library on top of it. |
| 47 | |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 48 | static NSString * const kHostAddress = @"localhost:5050"; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 49 | static NSString * const kPackage = @"grpc.testing"; |
| 50 | static NSString * const kService = @"TestService"; |
| 51 | |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 52 | static ProtoMethod *kInexistentMethod; |
| 53 | static ProtoMethod *kEmptyCallMethod; |
| 54 | static ProtoMethod *kUnaryCallMethod; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 55 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 56 | @interface GRPCClientTests : XCTestCase |
| 57 | @end |
| 58 | |
| 59 | @implementation GRPCClientTests |
| 60 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 61 | - (void)setUp { |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 62 | // Register test server as non-SSL. |
| 63 | [GRPCCall useInsecureConnectionsForHost:kHostAddress]; |
| 64 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 65 | // This method isn't implemented by the remote server. |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 66 | kInexistentMethod = [[ProtoMethod alloc] initWithPackage:kPackage |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 67 | service:kService |
| 68 | method:@"Inexistent"]; |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 69 | kEmptyCallMethod = [[ProtoMethod alloc] initWithPackage:kPackage |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 70 | service:kService |
| 71 | method:@"EmptyCall"]; |
Jorge Canizales | 469d4b6 | 2015-07-03 12:02:38 -0700 | [diff] [blame] | 72 | kUnaryCallMethod = [[ProtoMethod alloc] initWithPackage:kPackage |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 73 | service:kService |
| 74 | method:@"UnaryCall"]; |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 77 | - (void)testConnectionToRemoteServer { |
| 78 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."]; |
| 79 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 80 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 81 | path:kInexistentMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 82 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 83 | |
| 84 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 85 | XCTFail(@"Received unexpected response: %@", value); |
| 86 | } completionHandler:^(NSError *errorOrNil) { |
| 87 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
| 88 | // TODO(jcanizales): The server should return code 12 UNIMPLEMENTED, not 5 NOT FOUND. |
| 89 | XCTAssertEqual(errorOrNil.code, 5, @"Finished with unexpected error: %@", errorOrNil); |
| 90 | [expectation fulfill]; |
| 91 | }]; |
| 92 | |
| 93 | [call startWithWriteable:responsesWriteable]; |
| 94 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 95 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | - (void)testEmptyRPC { |
| 99 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; |
| 100 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; |
| 101 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 102 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 103 | path:kEmptyCallMethod.HTTPPath |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 104 | requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 105 | |
| 106 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 107 | XCTAssertNotNil(value, @"nil value received as response."); |
| 108 | XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); |
| 109 | [response fulfill]; |
| 110 | } completionHandler:^(NSError *errorOrNil) { |
| 111 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 112 | [completion fulfill]; |
| 113 | }]; |
| 114 | |
| 115 | [call startWithWriteable:responsesWriteable]; |
| 116 | |
Jorge Canizales | 83c57cb | 2015-08-09 16:36:49 -0700 | [diff] [blame^] | 117 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | - (void)testSimpleProtoRPC { |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 121 | __weak XCTestExpectation *response = [self expectationWithDescription:@"Expected response."]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 122 | __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."]; |
| 123 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 124 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 125 | request.responseSize = 100; |
| 126 | request.fillUsername = YES; |
| 127 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 128 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 129 | |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 130 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 131 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 132 | requestsWriter:requestsWriter]; |
| 133 | |
| 134 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 135 | XCTAssertNotNil(value, @"nil value received as response."); |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 136 | XCTAssertGreaterThan(value.length, 0, @"Empty response received."); |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 137 | RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 138 | // We expect empty strings, not nil: |
Jorge Canizales | d666fd0 | 2015-06-12 18:59:11 -0700 | [diff] [blame] | 139 | XCTAssertNotNil(responseProto.username, @"Response's username is nil."); |
| 140 | XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil."); |
| 141 | [response fulfill]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 142 | } completionHandler:^(NSError *errorOrNil) { |
| 143 | XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); |
| 144 | [completion fulfill]; |
| 145 | }]; |
| 146 | |
| 147 | [call startWithWriteable:responsesWriteable]; |
| 148 | |
Jorge Canizales | 83c57cb | 2015-08-09 16:36:49 -0700 | [diff] [blame^] | 149 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 152 | - (void)testMetadata { |
| 153 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"RPC unauthorized."]; |
| 154 | |
| 155 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
| 156 | request.fillUsername = YES; |
| 157 | request.fillOauthScope = YES; |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 158 | GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 159 | |
| 160 | GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 161 | path:kUnaryCallMethod.HTTPPath |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 162 | requestsWriter:requestsWriter]; |
| 163 | |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 164 | call.oauth2AccessToken = @"bogusToken"; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 165 | |
| 166 | id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
| 167 | XCTFail(@"Received unexpected response: %@", value); |
| 168 | } completionHandler:^(NSError *errorOrNil) { |
| 169 | XCTAssertNotNil(errorOrNil, @"Finished without error!"); |
| 170 | XCTAssertEqual(errorOrNil.code, 16, @"Finished with unexpected error: %@", errorOrNil); |
Jorge Canizales | 8c6bc6e | 2015-06-12 22:19:23 -0700 | [diff] [blame] | 171 | XCTAssertEqualObjects(call.responseMetadata, errorOrNil.userInfo[kGRPCStatusMetadataKey], |
| 172 | @"Metadata in the NSError object and call object differ."); |
Jorge Canizales | 721b7a3 | 2015-08-07 10:11:16 -0700 | [diff] [blame] | 173 | NSString *challengeHeader = call.oauth2ChallengeHeader; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 174 | XCTAssertGreaterThan(challengeHeader.length, 0, |
| 175 | @"No challenge in response headers %@", call.responseMetadata); |
| 176 | [expectation fulfill]; |
| 177 | }]; |
| 178 | |
| 179 | [call startWithWriteable:responsesWriteable]; |
| 180 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 181 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | d798125 | 2015-06-12 19:15:18 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jorge Canizales | b3be229 | 2015-05-31 19:11:20 -0700 | [diff] [blame] | 184 | @end |