Search in sources :

Example 11 with TestWebServer

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

the class RegistryClientTest method testAuthPullByWwwAuthenticate_basicAuthRequestedButNullCredential.

@Test
public void testAuthPullByWwwAuthenticate_basicAuthRequestedButNullCredential() 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(null);
    registryClient.authPullByWwwAuthenticate("Basic foo");
    Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
    Assert.assertEquals(5678, digestAndSize.get().getSize());
    MatcherAssert.assertThat(registry.getInputRead(), CoreMatchers.not(CoreMatchers.containsString("Authorization:")));
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 12 with TestWebServer

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

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 13 with TestWebServer

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

the class RegistryClientTest method setUpAuthServerAndRegistry.

/**
 * Sets up an auth server and a registry. The auth server can return a bearer token up to {@code
 * maxAuthTokens} times. The registry will initially return "401 Unauthorized" for {@code
 * maxTokenResponses} times. (Therefore, a registry client has to get auth tokens from the auth
 * server {@code maxAuthTokens} times. After that, the registry returns {@code finalResponse}.
 */
private void setUpAuthServerAndRegistry(int maxAuthTokens, @Nullable String finalResponse) throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException {
    String tokenResponse = "HTTP/1.1 200 OK\nContent-Length: 26\n\n{\"token\":\"awesome-token!\"}";
    authServer = new TestWebServer(false, Arrays.asList(tokenResponse), maxAuthTokens);
    String bearerAuth = "HTTP/1.1 401 Unauthorized\nContent-Length: 0\nWWW-Authenticate: Bearer realm=\"" + authServer.getEndpoint() + "\"\n\n";
    List<String> responses = new ArrayList<>(Collections.nCopies(maxAuthTokens, bearerAuth));
    if (finalResponse != null) {
        responses.add(finalResponse);
    }
    registry = new TestWebServer(false, responses, responses.size(), true);
}
Also used : ArrayList(java.util.ArrayList) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer)

Example 14 with TestWebServer

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

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 15 with TestWebServer

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

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