Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 03a0c49

Browse files
authored
Add header sent test to mockhttpservicetest (#514)
Test that the headers sent through MockHttpService are indeed sent.
1 parent d0196bb commit 03a0c49

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

gax-httpjson/src/test/java/com/google/api/gax/httpjson/MockHttpServiceTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
import static junit.framework.TestCase.fail;
3737

3838
import com.google.api.client.http.GenericUrl;
39+
import com.google.api.client.http.HttpHeaders;
3940
import com.google.api.client.http.HttpMethods;
41+
import com.google.api.client.http.HttpRequest;
4042
import com.google.api.client.http.HttpRequestFactory;
43+
import com.google.api.client.http.HttpRequestInitializer;
4144
import com.google.api.client.http.HttpResponse;
4245
import com.google.api.client.http.HttpResponseException;
4346
import com.google.api.gax.httpjson.testing.FakeApiMessage;
@@ -210,7 +213,6 @@ public void testUnknownMethodPath() throws IOException {
210213
public void testReturnException() throws IOException {
211214
testService.addException(new Exception(RESPONSE_EXCEPTION_STRING));
212215

213-
// Fifth HTTP call throws exception.
214216
try {
215217
HTTP_REQUEST_FACTORY
216218
.buildGetRequest(new GenericUrl("http://google.com/pet/rodent"))
@@ -222,6 +224,29 @@ public void testReturnException() throws IOException {
222224
}
223225
}
224226

227+
@Test
228+
public void testHeaderSent() throws IOException {
229+
testService.addNullResponse();
230+
231+
final String headerValue1 = "3005";
232+
final String headerValue2 = "d.?g";
233+
234+
HttpRequestInitializer httpRequestInitializer =
235+
new HttpRequestInitializer() {
236+
@Override
237+
public void initialize(HttpRequest request) throws IOException {
238+
request.setHeaders(
239+
new HttpHeaders().set("headerkey1", headerValue1).set("headerkey2", headerValue2));
240+
}
241+
};
242+
243+
HttpRequestFactory requestFactory = testService.createRequestFactory(httpRequestInitializer);
244+
requestFactory.buildGetRequest(new GenericUrl("http://google.com/")).execute();
245+
246+
assertEquals(headerValue1, testService.getRequestHeaders().get("headerkey1").iterator().next());
247+
assertEquals(headerValue2, testService.getRequestHeaders().get("headerkey2").iterator().next());
248+
}
249+
225250
private String getHttpResponseString(HttpResponse httpResponse) throws IOException {
226251
return CharStreams.toString(new InputStreamReader(httpResponse.getContent()));
227252
}

0 commit comments

Comments
 (0)