use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.
the class DigestPostTest method testServerWithHttpClientStringContent.
@Test
public void testServerWithHttpClientStringContent() throws Exception {
String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
HttpClient client = new HttpClient();
try {
AuthenticationStore authStore = client.getAuthenticationStore();
authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
client.start();
Request request = client.newRequest(srvUrl);
request.method(HttpMethod.POST);
request.content(new BytesContentProvider(__message.getBytes("UTF8")));
_received = null;
request = request.timeout(5, TimeUnit.SECONDS);
ContentResponse response = request.send();
Assert.assertEquals(__message, _received);
Assert.assertEquals(200, response.getStatus());
} finally {
client.stop();
}
}
use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.
the class HttpClientAuthenticationTest method test_Authentication.
private void test_Authentication(Authentication authentication) throws Exception {
AuthenticationStore authenticationStore = client.getAuthenticationStore();
final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(1));
Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.get().countDown();
}
};
client.getRequestListeners().add(requestListener);
// Request without Authentication causes a 401
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(401, response.getStatus());
Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
client.getRequestListeners().remove(requestListener);
authenticationStore.addAuthentication(authentication);
requests.set(new CountDownLatch(2));
requestListener = new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.get().countDown();
}
};
client.getRequestListeners().add(requestListener);
// Request with authentication causes a 401 (no previous successful authentication) + 200
request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
response = request.timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
client.getRequestListeners().remove(requestListener);
requests.set(new CountDownLatch(1));
requestListener = new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.get().countDown();
}
};
client.getRequestListeners().add(requestListener);
// Further requests do not trigger 401 because there is a previous successful authentication
// Remove existing header to be sure it's added by the implementation
request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
response = request.timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
client.getRequestListeners().remove(requestListener);
}
use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.
the class HttpClientAuthenticationTest method test_PreemptedAuthentication.
@Test
public void test_PreemptedAuthentication() throws Exception {
startBasic(new EmptyServerHandler());
AuthenticationStore authenticationStore = client.getAuthenticationStore();
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
authenticationStore.addAuthenticationResult(new BasicAuthentication.BasicResult(uri, "basic", "basic"));
AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.incrementAndGet();
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(1, requests.get());
}
use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.
the class HttpClientAuthenticationTest method test_BasicAuthentication_WithWrongPassword.
@Test
public void test_BasicAuthentication_WithWrongPassword() throws Exception {
startBasic(new EmptyServerHandler());
AuthenticationStore authenticationStore = client.getAuthenticationStore();
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "wrong");
authenticationStore.addAuthentication(authentication);
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(401, response.getStatus());
}
use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.
the class HttpAuthenticationStoreTest method testFindAuthenticationWithDefaultHTTPPort.
@Test
public void testFindAuthenticationWithDefaultHTTPPort() throws Exception {
AuthenticationStore store = new HttpAuthenticationStore();
URI uri1 = URI.create("http://host:80");
URI uri2 = URI.create("http://host");
String realm = "realm";
store.addAuthentication(new BasicAuthentication(uri1, realm, "user", "password"));
Authentication result = store.findAuthentication("Basic", uri2, realm);
Assert.assertNotNull(result);
store.clearAuthentications();
// Flip the URIs.
uri1 = URI.create("https://server/");
uri2 = URI.create("https://server:443/path");
store.addAuthentication(new DigestAuthentication(uri1, realm, "user", "password"));
result = store.findAuthentication("Digest", uri2, realm);
Assert.assertNotNull(result);
}
Aggregations