Search in sources :

Example 6 with Response

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));
}
Also used : Response(com.google.cloud.tools.jib.http.Response) Test(org.junit.Test)

Example 7 with Response

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));
}
Also used : Response(com.google.cloud.tools.jib.http.Response) Test(org.junit.Test)

Example 8 with Response

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));
}
Also used : Response(com.google.cloud.tools.jib.http.Response) Test(org.junit.Test)

Example 9 with Response

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);
    }
}
Also used : Response(com.google.cloud.tools.jib.http.Response) Connection(com.google.cloud.tools.jib.http.Connection) Request(com.google.cloud.tools.jib.http.Request) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

Response (com.google.cloud.tools.jib.http.Response)9 Test (org.junit.Test)7 Blob (com.google.cloud.tools.jib.blob.Blob)2 Connection (com.google.cloud.tools.jib.http.Connection)2 Request (com.google.cloud.tools.jib.http.Request)2 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)2 URL (java.net.URL)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)1 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 NoHttpResponseException (org.apache.http.NoHttpResponseException)1