|
27 | 27 | import java.util.Map; |
28 | 28 |
|
29 | 29 | import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 30 | +import com.google.api.client.http.GenericUrl; |
| 31 | +import com.google.api.client.http.HttpIOExceptionHandler; |
| 32 | +import com.google.api.client.http.HttpRequest; |
| 33 | +import com.google.api.client.http.HttpRequestFactory; |
| 34 | +import com.google.api.client.http.HttpRequestInitializer; |
| 35 | +import com.google.api.client.http.HttpResponse; |
| 36 | +import com.google.api.client.http.HttpUnsuccessfulResponseHandler; |
| 37 | +import com.google.api.client.testing.http.MockHttpTransport; |
30 | 38 | import org.elasticsearch.common.settings.Settings; |
| 39 | +import org.elasticsearch.common.unit.TimeValue; |
31 | 40 | import org.elasticsearch.env.Environment; |
32 | 41 | import org.elasticsearch.repositories.gcs.GoogleCloudStorageService.InternalGoogleCloudStorageService; |
33 | 42 | import org.elasticsearch.test.ESTestCase; |
34 | 43 |
|
| 44 | +import static java.util.Collections.emptyMap; |
35 | 45 | import static org.hamcrest.Matchers.containsString; |
| 46 | +import static org.mockito.Matchers.any; |
| 47 | +import static org.mockito.Matchers.anyBoolean; |
| 48 | +import static org.mockito.Mockito.mock; |
| 49 | +import static org.mockito.Mockito.times; |
| 50 | +import static org.mockito.Mockito.verify; |
| 51 | +import static org.mockito.Mockito.when; |
36 | 52 |
|
37 | 53 | public class GoogleCloudStorageServiceTests extends ESTestCase { |
38 | 54 |
|
@@ -82,4 +98,45 @@ public void testClientCredential() throws Exception { |
82 | 98 | InternalGoogleCloudStorageService service = new InternalGoogleCloudStorageService(env, credentials); |
83 | 99 | assertSame(cred, service.getCredential("_default_", "clientname")); |
84 | 100 | } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test that the {@link InternalGoogleCloudStorageService.DefaultHttpRequestInitializer} attaches new instances |
| 104 | + * of {@link HttpIOExceptionHandler} and {@link HttpUnsuccessfulResponseHandler} for every HTTP requests. |
| 105 | + */ |
| 106 | + public void testDefaultHttpRequestInitializer() throws IOException { |
| 107 | + final Environment environment = mock(Environment.class); |
| 108 | + when(environment.settings()).thenReturn(Settings.EMPTY); |
| 109 | + |
| 110 | + final GoogleCredential credential = mock(GoogleCredential.class); |
| 111 | + when(credential.handleResponse(any(HttpRequest.class), any(HttpResponse.class), anyBoolean())).thenReturn(false); |
| 112 | + |
| 113 | + final TimeValue readTimeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); |
| 114 | + final TimeValue connectTimeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); |
| 115 | + |
| 116 | + final InternalGoogleCloudStorageService service = new InternalGoogleCloudStorageService(environment, emptyMap()); |
| 117 | + final HttpRequestInitializer initializer = service.new DefaultHttpRequestInitializer(credential, connectTimeout, readTimeout); |
| 118 | + final HttpRequestFactory requestFactory = new MockHttpTransport().createRequestFactory(initializer); |
| 119 | + |
| 120 | + final HttpRequest request1 = requestFactory.buildGetRequest(new GenericUrl()); |
| 121 | + assertEquals((int) connectTimeout.millis(), request1.getConnectTimeout()); |
| 122 | + assertEquals((int) readTimeout.millis(), request1.getReadTimeout()); |
| 123 | + assertSame(credential, request1.getInterceptor()); |
| 124 | + assertNotNull(request1.getIOExceptionHandler()); |
| 125 | + assertNotNull(request1.getUnsuccessfulResponseHandler()); |
| 126 | + |
| 127 | + final HttpRequest request2 = requestFactory.buildGetRequest(new GenericUrl()); |
| 128 | + assertEquals((int) connectTimeout.millis(), request2.getConnectTimeout()); |
| 129 | + assertEquals((int) readTimeout.millis(), request2.getReadTimeout()); |
| 130 | + assertSame(request1.getInterceptor(), request2.getInterceptor()); |
| 131 | + assertNotNull(request2.getIOExceptionHandler()); |
| 132 | + assertNotSame(request1.getIOExceptionHandler(), request2.getIOExceptionHandler()); |
| 133 | + assertNotNull(request2.getUnsuccessfulResponseHandler()); |
| 134 | + assertNotSame(request1.getUnsuccessfulResponseHandler(), request2.getUnsuccessfulResponseHandler()); |
| 135 | + |
| 136 | + request1.getUnsuccessfulResponseHandler().handleResponse(null, null, false); |
| 137 | + verify(credential, times(1)).handleResponse(any(HttpRequest.class), any(HttpResponse.class), anyBoolean()); |
| 138 | + |
| 139 | + request2.getUnsuccessfulResponseHandler().handleResponse(null, null, false); |
| 140 | + verify(credential, times(2)).handleResponse(any(HttpRequest.class), any(HttpResponse.class), anyBoolean()); |
| 141 | + } |
85 | 142 | } |
0 commit comments