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 jetty.project by eclipse.
the class HttpConnection method convertCookies.
private StringBuilder convertCookies(List<HttpCookie> cookies, StringBuilder builder) {
for (int i = 0; i < cookies.size(); ++i) {
if (builder == null)
builder = new StringBuilder();
if (builder.length() > 0)
builder.append("; ");
HttpCookie cookie = cookies.get(i);
builder.append(cookie.getName()).append("=").append(cookie.getValue());
}
return builder;
}
use of java.net.HttpCookie in project j2objc by google.
the class CookiesTest method testCookieStoreUriDropsUserInfo.
public void testCookieStoreUriDropsUserInfo() throws URISyntaxException {
CookieStore cookieStore = new CookieManager().getCookieStore();
cookieStore.add(new URI("http://jesse:secret@a.com/"), new HttpCookie("a", "android"));
assertEquals(Arrays.asList(new URI("http://a.com")), cookieStore.getURIs());
}
Aggregations