Skip to content

Commit 5822bf3

Browse files
authored
Handles 301 and 308 redirects for registry responses. (GoogleContainerTools#408)
1 parent f623991 commit 5822bf3

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

‎jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryEndpointCaller.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
*/
4343
class RegistryEndpointCaller<T> {
4444

45+
/**
46+
* @see <a
47+
* href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308">https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308</a>
48+
*/
49+
@VisibleForTesting static final int STATUS_CODE_PERMANENT_REDIRECT = 308;
50+
4551
private static final String DEFAULT_PROTOCOL = "https";
4652

4753
/** Maintains the state of a request. This is used to retry requests with different parameters. */
@@ -52,8 +58,7 @@ private static class RequestState {
5258

5359
/**
5460
* @param authorization authentication credentials
55-
* @param url the endpoint URL to call, or {@code null} to use default from {@code
56-
* registryEndpointProvider}
61+
* @param url the endpoint URL to call
5762
*/
5863
private RequestState(@Nullable Authorization authorization, URL url) {
5964
this.authorization = authorization;
@@ -180,7 +185,10 @@ private T call(RequestState requestState) throws IOException, RegistryException
180185
httpResponseException);
181186

182187
} else if (httpResponseException.getStatusCode()
183-
== HttpStatusCodes.STATUS_CODE_TEMPORARY_REDIRECT) {
188+
== HttpStatusCodes.STATUS_CODE_TEMPORARY_REDIRECT
189+
|| httpResponseException.getStatusCode()
190+
== HttpStatusCodes.STATUS_CODE_MOVED_PERMANENTLY
191+
|| httpResponseException.getStatusCode() == STATUS_CODE_PERMANENT_REDIRECT) {
184192
// 'Location' header can be relative or absolute.
185193
URL redirectLocation =
186194
new URL(requestState.url, httpResponseException.getHeaders().getLocation());

‎jib-core/src/test/java/com/google/cloud/tools/jib/registry/RegistryEndpointCallerTest.java‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)