use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class BlobPullerTest method testHandleResponse_unexpectedDigest.
@Test
public void testHandleResponse_unexpectedDigest() throws IOException {
Blob testBlob = Blobs.from("some BLOB content");
DescriptorDigest testBlobDigest = testBlob.writeTo(ByteStreams.nullOutputStream()).getDigest();
Response mockResponse = Mockito.mock(Response.class);
Mockito.when(mockResponse.getBody()).thenReturn(testBlob);
try {
testBlobPuller.handleResponse(mockResponse);
Assert.fail("Receiving an unexpected digest should fail");
} catch (UnexpectedBlobDigestException ex) {
Assert.assertEquals("The pulled BLOB has digest '" + testBlobDigest + "', but the request digest was '" + fakeDigest + "'", ex.getMessage());
}
}
use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class BlobPullerTest method testHandleResponse.
@Test
public void testHandleResponse() throws IOException, UnexpectedBlobDigestException {
Blob testBlob = Blobs.from("some BLOB content");
DescriptorDigest testBlobDigest = testBlob.writeTo(ByteStreams.nullOutputStream()).getDigest();
Response mockResponse = Mockito.mock(Response.class);
Mockito.when(mockResponse.getBody()).thenReturn(testBlob);
BlobPuller blobPuller = new BlobPuller(fakeRegistryEndpointProperties, testBlobDigest, layerOutputStream);
blobPuller.handleResponse(mockResponse);
Assert.assertEquals("some BLOB content", new String(layerContentOutputStream.toByteArray(), StandardCharsets.UTF_8));
Assert.assertEquals(testBlobDigest, layerOutputStream.toBlobDescriptor().getDigest());
}
use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class BlobPusherTest method testInitializer_handleResponse_accepted_multipleLocations.
@Test
public void testInitializer_handleResponse_accepted_multipleLocations() throws IOException, RegistryException {
Response mockResponse = Mockito.mock(Response.class);
// Accepted
Mockito.when(mockResponse.getStatusCode()).thenReturn(202);
Mockito.when(mockResponse.getHeader("Location")).thenReturn(Arrays.asList("location1", "location2"));
try {
testBlobPusher.initializer().handleResponse(mockResponse);
Assert.fail("Multiple 'Location' headers should be a registry error");
} catch (RegistryErrorException ex) {
Assert.assertThat(ex.getMessage(), CoreMatchers.containsString("Expected 1 'Location' header, but found 2"));
}
}
use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class BlobPusherTest method testInitializer_handleResponse_unrecognized.
@Test
public void testInitializer_handleResponse_unrecognized() throws IOException, RegistryException {
Response mockResponse = Mockito.mock(Response.class);
// Unrecognized
Mockito.when(mockResponse.getStatusCode()).thenReturn(-1);
try {
testBlobPusher.initializer().handleResponse(mockResponse);
Assert.fail("Multiple 'Location' headers should be a registry error");
} catch (RegistryErrorException ex) {
Assert.assertThat(ex.getMessage(), CoreMatchers.containsString("Received unrecognized status code -1"));
}
}
use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class RegistryClient method callRegistryEndpoint.
/**
* Calls the registry endpoint with an override URL.
*
* @param url the endpoint URL to call, or {@code null} to use default from {@code
* registryEndpointProvider}
* @param registryEndpointProvider the {@link RegistryEndpointProvider} to the endpoint
*/
@Nullable
private <T> T callRegistryEndpoint(@Nullable URL url, RegistryEndpointProvider<T> registryEndpointProvider) throws IOException, RegistryException {
if (url == null) {
url = registryEndpointProvider.getApiRoute(getApiRouteBase());
}
try (Connection connection = new Connection(url)) {
Request request = Request.builder().setAuthorization(authorization).setUserAgent(getUserAgent()).setAccept(registryEndpointProvider.getAccept()).setBody(registryEndpointProvider.getContent()).build();
Response response = connection.send(registryEndpointProvider.getHttpMethod(), request);
return registryEndpointProvider.handleResponse(response);
} catch (HttpResponseException ex) {
// First, see if the endpoint provider handles an exception as an expected response.
try {
return registryEndpointProvider.handleHttpResponseException(ex);
} catch (HttpResponseException httpResponseException) {
if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_METHOD_NOT_ALLOWED) {
// The name or reference was invalid.
ErrorResponseTemplate errorResponse = JsonTemplateMapper.readJson(httpResponseException.getContent(), ErrorResponseTemplate.class);
RegistryErrorExceptionBuilder registryErrorExceptionBuilder = new RegistryErrorExceptionBuilder(registryEndpointProvider.getActionDescription(), httpResponseException);
for (ErrorEntryTemplate errorEntry : errorResponse.getErrors()) {
registryErrorExceptionBuilder.addReason(errorEntry);
}
throw registryErrorExceptionBuilder.build();
} else if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
throw new RegistryUnauthorizedException(registryEndpointProperties.getServerUrl(), registryEndpointProperties.getImageName(), httpResponseException);
} else if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_TEMPORARY_REDIRECT) {
return callRegistryEndpoint(new URL(httpResponseException.getHeaders().getLocation()), registryEndpointProvider);
} else {
// Unknown
throw httpResponseException;
}
}
} catch (NoHttpResponseException ex) {
throw new RegistryNoResponseException(ex);
} catch (SSLPeerUnverifiedException ex) {
// Fall-back to HTTP
GenericUrl httpUrl = new GenericUrl(url);
httpUrl.setScheme("http");
return callRegistryEndpoint(httpUrl.toURL(), registryEndpointProvider);
}
}
Aggregations