use of java.net.HttpCookie in project jetty.project by eclipse.
the class CookieTest method testViaServletUpgradeRequest.
@Test
public void testViaServletUpgradeRequest() throws Exception {
// Setup client
HttpCookie cookie = new HttpCookie("hello", "world");
cookie.setPath("/");
cookie.setMaxAge(100000);
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setCookies(Collections.singletonList(cookie));
// Client connects
CookieTrackingSocket clientSocket = new CookieTrackingSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri(), request);
// Server accepts connect
IBlockheadServerConnection serverConn = server.accept();
// client confirms upgrade and receipt of frame
String serverCookies = confirmClientUpgradeAndCookies(clientSocket, clientConnectFuture, serverConn);
Assert.assertThat("Cookies seen at server side", serverCookies, containsString("hello=world"));
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class ProxyServletTest method testCookiesFromDifferentClientsAreNotMixed.
@Test
public void testCookiesFromDifferentClientsAreNotMixed() throws Exception {
final String name = "biscuit";
startServer(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
String value = req.getHeader(name);
if (value != null) {
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(3600);
resp.addCookie(cookie);
} else {
Cookie[] cookies = req.getCookies();
Assert.assertEquals(1, cookies.length);
}
}
});
startProxy();
startClient();
String value1 = "1";
ContentResponse response1 = client.newRequest("localhost", serverConnector.getLocalPort()).header(name, value1).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response1.getStatus());
Assert.assertTrue(response1.getHeaders().containsKey(PROXIED_HEADER));
List<HttpCookie> cookies = client.getCookieStore().getCookies();
Assert.assertEquals(1, cookies.size());
Assert.assertEquals(name, cookies.get(0).getName());
Assert.assertEquals(value1, cookies.get(0).getValue());
HttpClient client2 = prepareClient();
try {
String value2 = "2";
ContentResponse response2 = client2.newRequest("localhost", serverConnector.getLocalPort()).header(name, value2).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response2.getStatus());
Assert.assertTrue(response2.getHeaders().containsKey(PROXIED_HEADER));
cookies = client2.getCookieStore().getCookies();
Assert.assertEquals(1, cookies.size());
Assert.assertEquals(name, cookies.get(0).getName());
Assert.assertEquals(value2, cookies.get(0).getValue());
// Make a third request to be sure the proxy does not mix cookies
ContentResponse response3 = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response3.getStatus());
Assert.assertTrue(response3.getHeaders().containsKey(PROXIED_HEADER));
} finally {
client2.stop();
}
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class RequestHeadersTest method testAccessRequestCookies.
@Test
public void testAccessRequestCookies() throws Exception {
BlockheadClient client = new BlockheadClient(server.getServerUri());
client.setTimeout(1, TimeUnit.SECONDS);
try {
client.connect();
client.addHeader("Cookie: fruit=Pear; type=Anjou\r\n");
client.sendStandardRequest();
client.expectUpgradeResponse();
UpgradeRequest req = echoCreator.getLastRequest();
Assert.assertThat("Last Request", req, notNullValue());
List<HttpCookie> cookies = req.getCookies();
Assert.assertThat("Request cookies", cookies, notNullValue());
Assert.assertThat("Request cookies.size", cookies.size(), is(2));
for (HttpCookie cookie : cookies) {
Assert.assertThat("Cookie name", cookie.getName(), anyOf(is("fruit"), is("type")));
Assert.assertThat("Cookie value", cookie.getValue(), anyOf(is("Pear"), is("Anjou")));
}
} finally {
client.close();
}
}
use of java.net.HttpCookie in project zeppelin by apache.
the class ZeppelinhubClient method setConnectionrequest.
private ClientUpgradeRequest setConnectionrequest(String token) {
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setCookies(Lists.newArrayList(new HttpCookie(ZeppelinHubRepo.TOKEN_HEADER, token)));
return request;
}
use of java.net.HttpCookie in project hadoop by apache.
the class TestWebAppProxyServlet method isResponseCookiePresent.
private boolean isResponseCookiePresent(HttpURLConnection proxyConn, String expectedName, String expectedValue) {
Map<String, List<String>> headerFields = proxyConn.getHeaderFields();
List<String> cookiesHeader = headerFields.get("Set-Cookie");
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
HttpCookie c = HttpCookie.parse(cookie).get(0);
if (c.getName().equals(expectedName) && c.getValue().equals(expectedValue)) {
return true;
}
}
}
return false;
}
Aggregations