use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class BlobPusherTest method testInitializer_handleResponse_created.
@Test
public void testInitializer_handleResponse_created() throws IOException, RegistryException {
Response mockResponse = Mockito.mock(Response.class);
// Created
Mockito.when(mockResponse.getStatusCode()).thenReturn(201);
Assert.assertNull(testBlobPusher.initializer().handleResponse(mockResponse));
}
use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class BlobPusherTest method testInitializer_handleResponse_accepted.
@Test
public void testInitializer_handleResponse_accepted() throws IOException, RegistryException {
Response mockResponse = Mockito.mock(Response.class);
// Accepted
Mockito.when(mockResponse.getStatusCode()).thenReturn(202);
Mockito.when(mockResponse.getHeader("Location")).thenReturn(Collections.singletonList("location"));
Assert.assertEquals("location", testBlobPusher.initializer().handleResponse(mockResponse));
}
use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class BlobPusherTest method testWriter_handleResponse.
@Test
public void testWriter_handleResponse() throws IOException, RegistryException {
Response mockResponse = Mockito.mock(Response.class);
Mockito.when(mockResponse.getHeader("Location")).thenReturn(Collections.singletonList("location"));
Assert.assertEquals("location", testBlobPusher.writer(mockURL).handleResponse(mockResponse));
}
use of com.google.cloud.tools.jib.http.Response in project jib by google.
the class RegistryAuthenticator method authenticate.
/**
* Sends the authentication request and retrieves the Bearer authorization token.
*
* @param scope the scope of permissions to authenticate for
* @see <a
* href="https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate">https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate</a>
*/
private Authorization authenticate(String scope) throws RegistryAuthenticationFailedException {
try {
URL authenticationUrl = getAuthenticationUrl(scope);
try (Connection connection = new Connection(authenticationUrl)) {
Request.Builder requestBuilder = Request.builder();
if (authorization != null) {
requestBuilder.setAuthorization(authorization);
}
Response response = connection.get(requestBuilder.build());
String responseString = Blobs.writeToString(response.getBody());
AuthenticationResponseTemplate responseJson = JsonTemplateMapper.readJson(responseString, AuthenticationResponseTemplate.class);
if (responseJson.token == null) {
throw new RegistryAuthenticationFailedException("Did not get token in authentication response from " + authenticationUrl);
}
return Authorizations.withBearerToken(responseJson.token);
}
} catch (IOException ex) {
throw new RegistryAuthenticationFailedException(ex);
}
}
Aggregations