@@ -159,7 +159,7 @@ public void testCall_methodNotAllowed() throws IOException, RegistryException {
159159 @ Test
160160 public void testCall_unknown () throws IOException , RegistryException {
161161 Mockito .when (mockHttpResponse .getStatusCode ())
162- .thenReturn (HttpStatusCodes .STATUS_CODE_MOVED_PERMANENTLY );
162+ .thenReturn (HttpStatusCodes .STATUS_CODE_SERVER_ERROR );
163163 HttpResponseException httpResponseException = new HttpResponseException (mockHttpResponse );
164164
165165 Mockito .when (mockConnection .send (Mockito .eq ("httpMethod" ), Mockito .any ()))
@@ -176,27 +176,17 @@ public void testCall_unknown() throws IOException, RegistryException {
176176
177177 @ Test
178178 public void testCall_temporaryRedirect () throws IOException , RegistryException {
179- // Mocks a response for temporary redirect to a new location.
180- Mockito .when (mockHttpResponse .getStatusCode ())
181- .thenReturn (HttpStatusCodes .STATUS_CODE_TEMPORARY_REDIRECT );
182- Mockito .when (mockHttpResponse .getHeaders ())
183- .thenReturn (new HttpHeaders ().setLocation ("https://newlocation" ));
184-
185- // Has mockConnection.send throw first, then succeed.
186- HttpResponseException httpResponseException = new HttpResponseException (mockHttpResponse );
187- Mockito .when (mockConnection .send (Mockito .eq ("httpMethod" ), Mockito .any ()))
188- .thenThrow (httpResponseException )
189- .thenReturn (mockResponse );
190- Mockito .when (mockResponse .getBody ()).thenReturn (Blobs .from ("body" ));
179+ verifyRetriesWithNewLocation (HttpStatusCodes .STATUS_CODE_TEMPORARY_REDIRECT );
180+ }
191181
192- Assert .assertEquals ("body" , testRegistryEndpointCaller .call ());
182+ @ Test
183+ public void testCall_movedPermanently () throws IOException , RegistryException {
184+ verifyRetriesWithNewLocation (HttpStatusCodes .STATUS_CODE_MOVED_PERMANENTLY );
185+ }
193186
194- // Checks that the URL was changed to the new location.
195- ArgumentCaptor <URL > urlArgumentCaptor = ArgumentCaptor .forClass (URL .class );
196- Mockito .verify (mockConnectionFactory , Mockito .times (2 )).apply (urlArgumentCaptor .capture ());
197- Assert .assertEquals (
198- new URL ("https://apiRouteBase/api" ), urlArgumentCaptor .getAllValues ().get (0 ));
199- Assert .assertEquals (new URL ("https://newlocation" ), urlArgumentCaptor .getAllValues ().get (1 ));
187+ @ Test
188+ public void testCall_permanentRedirect () throws IOException , RegistryException {
189+ verifyRetriesWithNewLocation (RegistryEndpointCaller .STATUS_CODE_PERMANENT_REDIRECT );
200190 }
201191
202192 /** Verifies a request is retried with HTTP protocol if {@code exceptionClass} is thrown. */
@@ -268,4 +258,32 @@ private void verifyThrowsRegistryErrorException(int httpStatusCode)
268258 "Tried to actionDescription but failed because: unknown: message" ));
269259 }
270260 }
261+
262+ /**
263+ * Verifies that a response with {@code httpStatusCode} retries the request with the {@code
264+ * Location} header.
265+ */
266+ private void verifyRetriesWithNewLocation (int httpStatusCode )
267+ throws IOException , RegistryException {
268+ // Mocks a response for temporary redirect to a new location.
269+ Mockito .when (mockHttpResponse .getStatusCode ()).thenReturn (httpStatusCode );
270+ Mockito .when (mockHttpResponse .getHeaders ())
271+ .thenReturn (new HttpHeaders ().setLocation ("https://newlocation" ));
272+
273+ // Has mockConnection.send throw first, then succeed.
274+ HttpResponseException httpResponseException = new HttpResponseException (mockHttpResponse );
275+ Mockito .when (mockConnection .send (Mockito .eq ("httpMethod" ), Mockito .any ()))
276+ .thenThrow (httpResponseException )
277+ .thenReturn (mockResponse );
278+ Mockito .when (mockResponse .getBody ()).thenReturn (Blobs .from ("body" ));
279+
280+ Assert .assertEquals ("body" , testRegistryEndpointCaller .call ());
281+
282+ // Checks that the URL was changed to the new location.
283+ ArgumentCaptor <URL > urlArgumentCaptor = ArgumentCaptor .forClass (URL .class );
284+ Mockito .verify (mockConnectionFactory , Mockito .times (2 )).apply (urlArgumentCaptor .capture ());
285+ Assert .assertEquals (
286+ new URL ("https://apiRouteBase/api" ), urlArgumentCaptor .getAllValues ().get (0 ));
287+ Assert .assertEquals (new URL ("https://newlocation" ), urlArgumentCaptor .getAllValues ().get (1 ));
288+ }
271289}
0 commit comments