2424import com .google .api .client .util .Value ;
2525import com .google .common .collect .ImmutableList ;
2626import com .google .common .collect .ImmutableSet ;
27+ import com .google .common .collect .Lists ;
2728
2829import junit .framework .Assert ;
2930import junit .framework .TestCase ;
@@ -314,17 +315,20 @@ static private class FailThenSuccessConnectionErrorTransport extends MockHttpTra
314315
315316 public int lowLevelExecCalls ;
316317 int callsBeforeSuccess ;
318+ List <String > userAgentHeader = Lists .newArrayList ();
317319
318320 protected FailThenSuccessConnectionErrorTransport (int callsBeforeSuccess ) {
319321 this .callsBeforeSuccess = callsBeforeSuccess ;
320322 }
321323
322- public LowLevelHttpRequest retryableGetRequest = new MockLowLevelHttpRequest () {
324+ public MockLowLevelHttpRequest retryableGetRequest = new MockLowLevelHttpRequest () {
323325
324326 @ Override
325327 public LowLevelHttpResponse execute () throws IOException {
326328 lowLevelExecCalls ++;
327329
330+ userAgentHeader = getHeaders ().get ("User-Agent" );
331+
328332 if (lowLevelExecCalls <= callsBeforeSuccess ) {
329333 throw new IOException ();
330334 }
@@ -337,6 +341,7 @@ public LowLevelHttpResponse execute() throws IOException {
337341
338342 @ Override
339343 public LowLevelHttpRequest buildGetRequest (String url ) {
344+ retryableGetRequest .getHeaders ().clear ();
340345 return retryableGetRequest ;
341346 }
342347 }
@@ -389,6 +394,22 @@ public void testExecuteErrorWithRetryDisabled() throws IOException {
389394 Assert .assertEquals (1 , fakeTransport .lowLevelExecCalls );
390395 }
391396
397+ public void testUserAgentWithExecuteErrorAndRetryEnabled () throws IOException {
398+ int callsBeforeSuccess = 3 ;
399+ FailThenSuccessConnectionErrorTransport fakeTransport =
400+ new FailThenSuccessConnectionErrorTransport (callsBeforeSuccess );
401+ HttpRequest req =
402+ fakeTransport .createRequestFactory ().buildGetRequest (new GenericUrl ("http://not/used" ));
403+ req .setRetryOnExecuteIOException (true );
404+ req .setNumberOfRetries (callsBeforeSuccess + 1 );
405+ HttpResponse resp = req .execute ();
406+
407+ Assert .assertEquals (1 , fakeTransport .userAgentHeader .size ());
408+ Assert .assertEquals (HttpRequest .USER_AGENT_SUFFIX , fakeTransport .userAgentHeader .get (0 ));
409+ Assert .assertEquals (200 , resp .getStatusCode ());
410+ Assert .assertEquals (4 , fakeTransport .lowLevelExecCalls );
411+ }
412+
392413 public void testAbnormalResponseHandlerWithNoBackOff () throws IOException {
393414 FailThenSuccessBackoffTransport fakeTransport =
394415 new FailThenSuccessBackoffTransport (HttpStatusCodes .STATUS_CODE_UNAUTHORIZED , 1 );
0 commit comments