3636import static junit .framework .TestCase .fail ;
3737
3838import com .google .api .client .http .GenericUrl ;
39+ import com .google .api .client .http .HttpHeaders ;
3940import com .google .api .client .http .HttpMethods ;
41+ import com .google .api .client .http .HttpRequest ;
4042import com .google .api .client .http .HttpRequestFactory ;
43+ import com .google .api .client .http .HttpRequestInitializer ;
4144import com .google .api .client .http .HttpResponse ;
4245import com .google .api .client .http .HttpResponseException ;
4346import 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