Search in sources :

Example 1 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by GoogleContainerTools.

the class RegistryAuthenticatorTest method testUserAgent.

@Test
public void testUserAgent() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryCredentialsNotSentException {
    try (TestWebServer server = new TestWebServer(false)) {
        try {
            RegistryAuthenticator authenticator = RegistryAuthenticator.fromAuthenticationMethod("Bearer realm=\"" + server.getEndpoint() + "\"", registryEndpointRequestProperties, "Competent-Agent", new FailoverHttpClient(true, false, ignored -> {
            })).get();
            authenticator.authenticatePush(null);
        } catch (RegistryAuthenticationFailedException ex) {
        // Doesn't matter if auth fails. We only examine what we sent.
        }
        MatcherAssert.assertThat(server.getInputRead(), CoreMatchers.containsString("User-Agent: Competent-Agent"));
    }
}
Also used : RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 2 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by GoogleContainerTools.

the class RegistryClientTest method testAuthPullByWwwAuthenticate_bearerAuth.

@Test
public void testAuthPullByWwwAuthenticate_bearerAuth() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String tokenResponse = "HTTP/1.1 200 OK\nContent-Length: 26\n\n{\"token\":\"awesome-token!\"}";
    authServer = new TestWebServer(false, Arrays.asList(tokenResponse), 1);
    String blobResponse = "HTTP/1.1 200 OK\nContent-Length: 5678\n\n";
    registry = new TestWebServer(false, Arrays.asList(blobResponse), 1);
    RegistryClient registryClient = createRegistryClient(Credential.from("user", "pass"));
    registryClient.authPullByWwwAuthenticate("Bearer realm=\"" + authServer.getEndpoint() + "\"");
    Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
    Assert.assertEquals(5678, digestAndSize.get().getSize());
    Mockito.verify(eventHandlers).dispatch(logContains("bearer auth succeeded"));
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 3 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by GoogleContainerTools.

the class RegistryClientTest method testConfigureBasicAuth.

@Test
public void testConfigureBasicAuth() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String basicAuth = "HTTP/1.1 200 OK\nContent-Length: 56789\n\n";
    registry = new TestWebServer(false, Arrays.asList(basicAuth), 1);
    RegistryClient registryClient = createRegistryClient(Credential.from("user", "pass"));
    registryClient.configureBasicAuth();
    Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
    Assert.assertEquals(56789, digestAndSize.get().getSize());
    MatcherAssert.assertThat(registry.getInputRead(), CoreMatchers.containsString("Authorization: Basic dXNlcjpwYXNz"));
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 4 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by GoogleContainerTools.

the class RegistryClientTest method testAuthPullByWwwAuthenticate_basicAuth.

@Test
public void testAuthPullByWwwAuthenticate_basicAuth() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String blobResponse = "HTTP/1.1 200 OK\nContent-Length: 5678\n\n";
    registry = new TestWebServer(false, Arrays.asList(blobResponse), 1);
    RegistryClient registryClient = createRegistryClient(Credential.from("user", "pass"));
    registryClient.authPullByWwwAuthenticate("Basic foo");
    Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
    Assert.assertEquals(5678, digestAndSize.get().getSize());
    MatcherAssert.assertThat(registry.getInputRead(), CoreMatchers.containsString("Authorization: Basic dXNlcjpwYXNz"));
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 5 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by GoogleContainerTools.

the class UpdateCheckerTest method testPerformUpdateCheck_newJsonField.

@Test
public void testPerformUpdateCheck_newJsonField() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException {
    String response = "HTTP/1.1 200 OK\nContent-Length:43\n\n{\"latest\":\"2.0.0\",\"unknownField\":\"unknown\"}";
    try (TestWebServer server = new TestWebServer(false, Collections.singletonList(response), 1)) {
        setupLastUpdateCheck();
        Optional<String> message = UpdateChecker.performUpdateCheck(configDir, "1.0.2", server.getEndpoint(), "tool-name", ignored -> {
        });
        assertThat(message).hasValue("2.0.0");
    }
}
Also used : TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Aggregations

TestWebServer (com.google.cloud.tools.jib.http.TestWebServer)30 Test (org.junit.Test)26 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)12 Credential (com.google.cloud.tools.jib.api.Credential)2 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)2 RegistryUnauthorizedException (com.google.cloud.tools.jib.api.RegistryUnauthorizedException)2 FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)2 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2