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