use of java.net.HttpCookie in project jetty.project by eclipse.
the class ResponseTest method testAddCookie_JavaNet.
/**
* Testing behavior documented in Chrome bug
* https://bugs.chromium.org/p/chromium/issues/detail?id=700618
*/
@Test
public void testAddCookie_JavaNet() throws Exception {
HttpCookie cookie = new HttpCookie("foo", URLEncoder.encode("bar;baz", UTF_8.toString()));
cookie.setPath("/secure");
assertEquals("foo=\"bar%3Bbaz\";$Path=\"/secure\"", cookie.toString());
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class CookiesTest method testCookiesAreSentToClient.
@Test
public void testCookiesAreSentToClient() throws Exception {
final String cookieName = "name";
final String cookieValue = "value";
final String cookieDomain = "domain";
final String cookiePath = "/path";
startServer(new EchoHandler() {
@Override
public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
String cookieString = cookieName + "=" + cookieValue + ";Domain=" + cookieDomain + ";Path=" + cookiePath;
response.getHeaders().put("Set-Cookie", Collections.singletonList(cookieString));
return super.createWebSocket(request, response);
}
});
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
builder.configurator(new ClientEndpointConfig.Configurator() {
@Override
public void afterResponse(HandshakeResponse response) {
Map<String, List<String>> headers = response.getHeaders();
// Test case insensitivity
Assert.assertTrue(headers.containsKey("set-cookie"));
List<String> values = headers.get("Set-Cookie");
Assert.assertNotNull(values);
Assert.assertEquals(1, values.size());
List<HttpCookie> cookies = HttpCookie.parse(values.get(0));
Assert.assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
Assert.assertEquals(cookieName, cookie.getName());
Assert.assertEquals(cookieValue, cookie.getValue());
Assert.assertEquals(cookieDomain, cookie.getDomain());
Assert.assertEquals(cookiePath, cookie.getPath());
}
});
ClientEndpointConfig config = builder.build();
Endpoint endPoint = new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
}
};
Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
session.close();
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class CookiesTest method testCookiesAreSentToServer.
@Test
public void testCookiesAreSentToServer() throws Exception {
final String cookieName = "name";
final String cookieValue = "value";
final String cookieString = cookieName + "=" + cookieValue;
startServer(new EchoHandler() {
@Override
public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
List<HttpCookie> cookies = request.getCookies();
assertThat("Cookies", cookies, notNullValue());
assertThat("Cookies", cookies.size(), is(1));
HttpCookie cookie = cookies.get(0);
Assert.assertEquals(cookieName, cookie.getName());
Assert.assertEquals(cookieValue, cookie.getValue());
Map<String, List<String>> headers = request.getHeaders();
// Test case insensitivity
Assert.assertTrue(headers.containsKey("cookie"));
List<String> values = headers.get("Cookie");
Assert.assertNotNull(values);
Assert.assertEquals(1, values.size());
Assert.assertEquals(cookieString, values.get(0));
return super.createWebSocket(request, response);
}
});
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
builder.configurator(new ClientEndpointConfig.Configurator() {
@Override
public void beforeRequest(Map<String, List<String>> headers) {
headers.put("Cookie", Collections.singletonList(cookieString));
}
});
ClientEndpointConfig config = builder.build();
Endpoint endPoint = new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
}
};
Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
session.close();
}
use of java.net.HttpCookie in project hadoop by apache.
the class TestAuthenticationSessionCookie method testSessionCookie.
@Test
public void testSessionCookie() throws IOException {
try {
startServer(true);
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}
URL base = new URL("http://" + NetUtils.getHostPortString(server.getConnectorAddress(0)));
HttpURLConnection conn = (HttpURLConnection) new URL(base, "/echo").openConnection();
String header = conn.getHeaderField("Set-Cookie");
List<HttpCookie> cookies = HttpCookie.parse(header);
Assert.assertTrue(!cookies.isEmpty());
Log.getLog().info(header);
Assert.assertFalse(header.contains("; Expires="));
Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
use of java.net.HttpCookie in project hadoop by apache.
the class TestHttpCookieFlag method testHttpsCookie.
@Test
public void testHttpsCookie() throws IOException, GeneralSecurityException {
URL base = new URL("https://" + NetUtils.getHostPortString(server.getConnectorAddress(1)));
HttpsURLConnection conn = (HttpsURLConnection) new URL(base, "/echo").openConnection();
conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());
String header = conn.getHeaderField("Set-Cookie");
List<HttpCookie> cookies = HttpCookie.parse(header);
Assert.assertTrue(!cookies.isEmpty());
Assert.assertTrue(header.contains("; HttpOnly"));
Assert.assertTrue(cookies.get(0).getSecure());
Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
Aggregations