This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
/
MockHttpServiceTest.java
262 lines (226 loc) · 9.46 KB
/
MockHttpServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright 2018 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.httpjson;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.gax.httpjson.testing.FakeApiMessage;
import com.google.api.gax.httpjson.testing.MockHttpService;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.google.protobuf.TypeRegistry;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
public class MockHttpServiceTest {
private static PetMessage gerbilMessage =
new PetMessage(
ImmutableMap.<String, Object>of("type", Lists.newArrayList("rodent")), null, null);
private static PetMessage ospreyMessage =
new PetMessage(
ImmutableMap.<String, Object>of("type", Lists.newArrayList("raptor")), null, null);
private static HumanMessage humanMessage =
new HumanMessage(
ImmutableMap.<String, Object>of("type", Lists.newArrayList("toddler")), null, null);
private static final String RESPONSE_EXCEPTION_STRING = "[Expected exception]";
private static final ApiException PARSE_EXCEPTION =
new ApiException(
"Unknown object type.", null, HttpJsonStatusCode.of(Code.INVALID_ARGUMENT), false);
private static class PetMessage extends FakeApiMessage {
public PetMessage(
Map<String, Object> fieldValues, ApiMessage requestBodyMessage, List<String> fieldMask) {
super(fieldValues, requestBodyMessage, fieldMask);
}
}
private static class HumanMessage extends FakeApiMessage {
public HumanMessage(
Map<String, Object> fieldValues, ApiMessage requestBodyMessage, List<String> fieldMask) {
super(fieldValues, requestBodyMessage, fieldMask);
}
}
@SuppressWarnings("unchecked")
private static final HttpResponseParser<PetMessage> PET_RESPONSE_PARSER =
new HttpResponseParser<PetMessage>() {
@Override
public PetMessage parse(InputStream httpContent) {
return null;
}
@Override
public PetMessage parse(InputStream httpContent, TypeRegistry registry) {
return parse(httpContent);
}
@Override
public String serialize(PetMessage response) {
return ((List<String>) response.getFieldValue("type")).get(0);
}
};
private static final String BASE_ENDPOINT = "http://google.com/";
private static final HttpRequestFormatter<PetMessage> PET_REQUEST_FORMATTER =
new HttpRequestFormatter<PetMessage>() {
@Override
public Map<String, List<String>> getQueryParamNames(PetMessage apiMessage) {
return null;
}
@Override
public String getRequestBody(PetMessage apiMessage) {
return null;
}
@Override
public String getPath(PetMessage apiMessage) {
return null;
}
@Override
public PathTemplate getPathTemplate() {
return PathTemplate.create("pet/{name}");
}
};
private static final ApiMethodDescriptor methodDescriptor =
ApiMethodDescriptor.<PetMessage, PetMessage>newBuilder()
.setFullMethodName("getPetName")
.setHttpMethod(HttpMethods.GET)
.setRequestFormatter(PET_REQUEST_FORMATTER)
.setResponseParser(PET_RESPONSE_PARSER)
.build();
private static final List<ApiMethodDescriptor> SERVER_METHOD_DESCRIPTORS =
Lists.newArrayList(methodDescriptor);
private static MockHttpService testService =
new MockHttpService(SERVER_METHOD_DESCRIPTORS, BASE_ENDPOINT);
private static final HttpRequestFactory HTTP_REQUEST_FACTORY = testService.createRequestFactory();
@Before
public void cleanUp() {
testService.reset();
}
@Test
public void testMessageResponse() throws IOException {
// Queue up return objects.
testService.addResponse(gerbilMessage);
testService.addResponse(ospreyMessage);
// First HTTP call returns gerbil.
HttpResponse httpResponse =
HTTP_REQUEST_FACTORY
.buildGetRequest(new GenericUrl("http://google.com/pet/rodent"))
.execute();
assertEquals("rodent", getHttpResponseString(httpResponse));
// Second HTTP call returns raptor.
httpResponse =
HTTP_REQUEST_FACTORY
.buildGetRequest(
new GenericUrl("http://google.com/pet/raptor?species=birb&name=G%C3%BCnter"))
.execute();
assertEquals("raptor", getHttpResponseString(httpResponse));
}
@Test
public void testNullResponse() throws IOException {
testService.addNullResponse();
HttpResponse httpResponse =
HTTP_REQUEST_FACTORY
.buildGetRequest(new GenericUrl("http://google.com/pet/raptor?species=birb"))
.execute();
assertNull(httpResponse.getContent());
}
@Test
public void testBadFormatter() throws IOException {
testService.addResponse(humanMessage);
// Human message type is not parsable by PET_RESPONSE_PARSER and should fail.
try {
HTTP_REQUEST_FACTORY
.buildGetRequest(new GenericUrl("http://google.com/pet/raptor?species=birb"))
.execute();
fail();
} catch (ClassCastException e) {
// Expected parsing exception.
}
}
@Test
public void testUnknownMethodPath() throws IOException {
testService.addResponse(gerbilMessage);
try {
// This url does not match any path template in SERVER_METHOD_DESCRIPTORS.
GenericUrl url = new GenericUrl("http://google.com/car/");
HttpResponse httpResponse = HTTP_REQUEST_FACTORY.buildGetRequest(url).execute();
fail();
} catch (HttpResponseException e) {
// Expected parsing exception.
assertFalse(e.isSuccessStatusCode());
}
}
@Test
public void testReturnException() throws IOException {
testService.addException(new Exception(RESPONSE_EXCEPTION_STRING));
try {
HTTP_REQUEST_FACTORY
.buildGetRequest(new GenericUrl("http://google.com/pet/rodent"))
.execute();
fail();
} catch (HttpResponseException e) {
assertFalse(e.isSuccessStatusCode());
assertTrue(e.getContent().contains(RESPONSE_EXCEPTION_STRING));
}
}
@Test
public void testHeaderSent() throws IOException {
testService.addNullResponse();
final String headerValue1 = "3005";
final String headerValue2 = "d.?g";
HttpRequestInitializer httpRequestInitializer =
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setHeaders(
new HttpHeaders().set("headerkey1", headerValue1).set("headerkey2", headerValue2));
}
};
HttpRequestFactory requestFactory = testService.createRequestFactory(httpRequestInitializer);
requestFactory.buildGetRequest(new GenericUrl("http://google.com/")).execute();
assertEquals(headerValue1, testService.getRequestHeaders().get("headerkey1").iterator().next());
assertEquals(headerValue2, testService.getRequestHeaders().get("headerkey2").iterator().next());
}
private String getHttpResponseString(HttpResponse httpResponse) throws IOException {
return CharStreams.toString(new InputStreamReader(httpResponse.getContent()));
}
}