blob: 2e03ad88a82d0adb91cda5f6422a63f7b42f8aff [file] [log] [blame]
Jorge Canizalesb3be2292015-05-31 19:11:20 -07001/*
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 Canizalesc42a38e2015-06-21 14:44:25 -070037#import <GRPCClient/GRPCCall.h>
Jorge Canizalesbe808e32015-07-04 14:37:58 -070038#import <ProtoRPC/ProtoMethod.h>
Jorge Canizalesb3be2292015-05-31 19:11:20 -070039#import <RemoteTest/Messages.pbobjc.h>
Jorge Canizalesc42a38e2015-06-21 14:44:25 -070040#import <RxLibrary/GRXWriteable.h>
41#import <RxLibrary/GRXWriter+Immediate.h>
Jorge Canizalesb3be2292015-05-31 19:11:20 -070042
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 Canizalesd666fd02015-06-12 18:59:11 -070046static NSString * const kHostAddress = @"grpc-test.sandbox.google.com";
47static NSString * const kPackage = @"grpc.testing";
48static NSString * const kService = @"TestService";
49
Jorge Canizales469d4b62015-07-03 12:02:38 -070050static ProtoMethod *kInexistentMethod;
51static ProtoMethod *kEmptyCallMethod;
52static ProtoMethod *kUnaryCallMethod;
Jorge Canizalesd666fd02015-06-12 18:59:11 -070053
Jorge Canizalesb3be2292015-05-31 19:11:20 -070054@interface GRPCClientTests : XCTestCase
55@end
56
57@implementation GRPCClientTests
58
Jorge Canizalesd666fd02015-06-12 18:59:11 -070059- (void)setUp {
60 // This method isn't implemented by the remote server.
Jorge Canizales469d4b62015-07-03 12:02:38 -070061 kInexistentMethod = [[ProtoMethod alloc] initWithPackage:kPackage
Jorge Canizalesbe808e32015-07-04 14:37:58 -070062 service:kService
63 method:@"Inexistent"];
Jorge Canizales469d4b62015-07-03 12:02:38 -070064 kEmptyCallMethod = [[ProtoMethod alloc] initWithPackage:kPackage
Jorge Canizalesbe808e32015-07-04 14:37:58 -070065 service:kService
66 method:@"EmptyCall"];
Jorge Canizales469d4b62015-07-03 12:02:38 -070067 kUnaryCallMethod = [[ProtoMethod alloc] initWithPackage:kPackage
Jorge Canizalesbe808e32015-07-04 14:37:58 -070068 service:kService
69 method:@"UnaryCall"];
Jorge Canizalesd666fd02015-06-12 18:59:11 -070070}
71
Jorge Canizalesb3be2292015-05-31 19:11:20 -070072- (void)testConnectionToRemoteServer {
73 __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Server reachable."];
74
Jorge Canizalesd666fd02015-06-12 18:59:11 -070075 GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
Jorge Canizales5260f532015-07-04 14:47:41 -070076 path:kInexistentMethod.HTTPPath
Jorge Canizalesd666fd02015-06-12 18:59:11 -070077 requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
Jorge Canizalesb3be2292015-05-31 19:11:20 -070078
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 Canizales5580e5d2015-07-15 23:35:48 -070090 [self waitForExpectationsWithTimeout:4 handler:nil];
Jorge Canizalesb3be2292015-05-31 19:11:20 -070091}
92
93- (void)testEmptyRPC {
94 __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."];
95 __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
96
Jorge Canizalesd666fd02015-06-12 18:59:11 -070097 GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
Jorge Canizales5260f532015-07-04 14:47:41 -070098 path:kEmptyCallMethod.HTTPPath
Jorge Canizalesd666fd02015-06-12 18:59:11 -070099 requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700100
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 Canizales5580e5d2015-07-15 23:35:48 -0700112 [self waitForExpectationsWithTimeout:4 handler:nil];
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700113}
114
115- (void)testSimpleProtoRPC {
Jorge Canizalesd666fd02015-06-12 18:59:11 -0700116 __weak XCTestExpectation *response = [self expectationWithDescription:@"Expected response."];
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700117 __weak XCTestExpectation *completion = [self expectationWithDescription:@"RPC completed."];
118
Jorge Canizalesd666fd02015-06-12 18:59:11 -0700119 RMTSimpleRequest *request = [RMTSimpleRequest message];
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700120 request.responseSize = 100;
121 request.fillUsername = YES;
122 request.fillOauthScope = YES;
123 id<GRXWriter> requestsWriter = [GRXWriter writerWithValue:[request data]];
124
Jorge Canizalesd666fd02015-06-12 18:59:11 -0700125 GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
Jorge Canizales5260f532015-07-04 14:47:41 -0700126 path:kUnaryCallMethod.HTTPPath
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700127 requestsWriter:requestsWriter];
128
129 id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
130 XCTAssertNotNil(value, @"nil value received as response.");
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700131 XCTAssertGreaterThan(value.length, 0, @"Empty response received.");
Jorge Canizalesd666fd02015-06-12 18:59:11 -0700132 RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL];
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700133 // We expect empty strings, not nil:
Jorge Canizalesd666fd02015-06-12 18:59:11 -0700134 XCTAssertNotNil(responseProto.username, @"Response's username is nil.");
135 XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil.");
136 [response fulfill];
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700137 } completionHandler:^(NSError *errorOrNil) {
138 XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
139 [completion fulfill];
140 }];
141
142 [call startWithWriteable:responsesWriteable];
143
Jorge Canizales5580e5d2015-07-15 23:35:48 -0700144 [self waitForExpectationsWithTimeout:4 handler:nil];
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700145}
146
Jorge Canizalesd7981252015-06-12 19:15:18 -0700147- (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 Canizales5260f532015-07-04 14:47:41 -0700156 path:kUnaryCallMethod.HTTPPath
Jorge Canizalesd7981252015-06-12 19:15:18 -0700157 requestsWriter:requestsWriter];
158
Jorge Canizales544963e2015-06-12 19:46:27 -0700159 call.requestMetadata[@"Authorization"] = @"Bearer bogusToken";
Jorge Canizalesd7981252015-06-12 19:15:18 -0700160
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 Canizales8c6bc6e2015-06-12 22:19:23 -0700166 XCTAssertEqualObjects(call.responseMetadata, errorOrNil.userInfo[kGRPCStatusMetadataKey],
167 @"Metadata in the NSError object and call object differ.");
Jorge Canizales1a384752015-06-12 22:40:37 -0700168 NSString *challengeHeader = call.responseMetadata[@"www-authenticate"];
Jorge Canizalesd7981252015-06-12 19:15:18 -0700169 XCTAssertGreaterThan(challengeHeader.length, 0,
170 @"No challenge in response headers %@", call.responseMetadata);
171 [expectation fulfill];
172 }];
173
174 [call startWithWriteable:responsesWriteable];
175
Jorge Canizales5580e5d2015-07-15 23:35:48 -0700176 [self waitForExpectationsWithTimeout:4 handler:nil];
Jorge Canizalesd7981252015-06-12 19:15:18 -0700177}
178
Jorge Canizalesb3be2292015-05-31 19:11:20 -0700179@end