use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.
the class UpdateCheckerTest method setUp.
@Before
public void setUp() throws InterruptedException, GeneralSecurityException, URISyntaxException, IOException {
String response = "HTTP/1.1 200 OK\nContent-Length:18\n\n{\"latest\":\"2.0.0\"}";
testWebServer = new TestWebServer(false, Collections.singletonList(response), 1);
configDir = temporaryFolder.getRoot().toPath();
}
use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.
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"));
}
}
use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.
the class RegistryClientTest method testAutomaticTokenRefresh_badWwwAuthenticateResponse.
@Test
public void testAutomaticTokenRefresh_badWwwAuthenticateResponse() 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), 3);
List<String> responses = Arrays.asList("HTTP/1.1 401 Unauthorized\nContent-Length: 0\nWWW-Authenticate: Bearer realm=\"" + authServer.getEndpoint() + "\"\n\n", "HTTP/1.1 401 Unauthorized\nContent-Length: 0\nWWW-Authenticate: Basic realm=foo\n\n", "HTTP/1.1 401 Unauthorized\nContent-Length: 0\n\n", "HTTP/1.1 200 OK\nContent-Length: 5678\n\n");
registry = new TestWebServer(false, responses, responses.size(), true);
RegistryClient registryClient = createRegistryClient(null);
Assert.assertTrue(registryClient.doPushBearerAuth());
Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
Assert.assertEquals(5678, digestAndSize.get().getSize());
// Verify authServer returned bearer token three times (i.e., refreshed twice)
Assert.assertEquals(3, authServer.getTotalResponsesServed());
Assert.assertEquals(4, registry.getTotalResponsesServed());
Mockito.verify(eventHandlers).dispatch(logContains("server did not return 'WWW-Authenticate: Bearer' header. Actual: Basic"));
Mockito.verify(eventHandlers).dispatch(logContains("server did not return 'WWW-Authenticate: Bearer' header. Actual: null"));
}
use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.
the class RegistryClientTest method testDoBearerAuth_returnsFalseOnBasicAuth.
@Test
public void testDoBearerAuth_returnsFalseOnBasicAuth() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
String basicAuth = "HTTP/1.1 401 Unauthorized\nContent-Length: 0\nWWW-Authenticate: Basic foo\n\n";
registry = new TestWebServer(false, Arrays.asList(basicAuth), 1);
RegistryClient registryClient = createRegistryClient(null);
Assert.assertFalse(registryClient.doPullBearerAuth());
Mockito.verify(eventHandlers).dispatch(logContains("attempting bearer auth"));
Mockito.verify(eventHandlers).dispatch(logContains("server requires basic auth"));
}
use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.
the class RegistryClientTest method testPullManifest.
@Test
public void testPullManifest() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
String manifestResponse = "HTTP/1.1 200 OK\nContent-Length: 307\n\n{\n" + " \"schemaVersion\": 2,\n" + " \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n" + " \"config\": {\n" + " \"mediaType\": \"application/vnd.docker.container.image.v1+json\",\n" + " \"size\": 7023,\n" + " \"digest\": \"sha256:b5b2b2c507a0944348e0303114d8d93aaaa081732b86451d9bce1f432a537bc7\"\n" + " }\n" + "}";
registry = new TestWebServer(false, Arrays.asList(manifestResponse), 1);
RegistryClient registryClient = createRegistryClient(null);
ManifestAndDigest<?> manifestAndDigest = registryClient.pullManifest("image-tag");
Assert.assertEquals("sha256:6b61466eabab6e5ffb68ae2bd9b85c789225540c2ac54ea1f71eb327588e8946", manifestAndDigest.getDigest().toString());
Assert.assertTrue(manifestAndDigest.getManifest() instanceof V22ManifestTemplate);
V22ManifestTemplate manifest = (V22ManifestTemplate) manifestAndDigest.getManifest();
Assert.assertEquals(2, manifest.getSchemaVersion());
Assert.assertEquals("application/vnd.docker.distribution.manifest.v2+json", manifest.getManifestMediaType());
Assert.assertEquals("sha256:b5b2b2c507a0944348e0303114d8d93aaaa081732b86451d9bce1f432a537bc7", manifest.getContainerConfiguration().getDigest().toString());
Assert.assertEquals(7023, manifest.getContainerConfiguration().getSize());
MatcherAssert.assertThat(registry.getInputRead(), CoreMatchers.containsString("GET /v2/foo/bar/manifests/image-tag "));
}
Aggregations