use of java.net.HttpCookie in project jetty.project by eclipse.
the class CookieTest method testViaCookieManager.
@Test
public void testViaCookieManager() throws Exception {
// Setup client
CookieManager cookieMgr = new CookieManager();
client.setCookieStore(cookieMgr.getCookieStore());
HttpCookie cookie = new HttpCookie("hello", "world");
cookie.setPath("/");
cookie.setVersion(0);
cookie.setMaxAge(100000);
cookieMgr.getCookieStore().add(server.getWsUri(), cookie);
cookie = new HttpCookie("foo", "bar is the word");
cookie.setPath("/");
cookie.setMaxAge(100000);
cookieMgr.getCookieStore().add(server.getWsUri(), cookie);
// Client connects
CookieTrackingSocket clientSocket = new CookieTrackingSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
// Server accepts connect
IBlockheadServerConnection serverConn = server.accept();
// client confirms upgrade and receipt of frame
String serverCookies = confirmClientUpgradeAndCookies(clientSocket, clientConnectFuture, serverConn);
assertThat("Cookies seen at server side", serverCookies, containsString("hello=world"));
assertThat("Cookies seen at server side", serverCookies, containsString("foo=bar is the word"));
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class ServletUpgradeRequest method getCookies.
@Override
public List<HttpCookie> getCookies() {
if (cookies == null) {
Cookie[] requestCookies = request.getCookies();
if (requestCookies != null) {
cookies = new ArrayList<>();
for (Cookie requestCookie : requestCookies) {
HttpCookie cookie = new HttpCookie(requestCookie.getName(), requestCookie.getValue());
// No point handling domain/path/expires/secure/httponly on client request cookies
cookies.add(cookie);
}
}
}
return cookies;
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class HttpClientTest method testStoppingClosesConnections.
@Test
public void testStoppingClosesConnections() throws Exception {
start(new EmptyServerHandler());
String host = "localhost";
int port = connector.getLocalPort();
String path = "/";
Response response = client.GET(scheme + "://" + host + ":" + port + path);
Assert.assertEquals(200, response.getStatus());
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
long start = System.nanoTime();
HttpConnectionOverHTTP connection = null;
while (connection == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
connection = (HttpConnectionOverHTTP) connectionPool.getIdleConnections().peek();
TimeUnit.MILLISECONDS.sleep(10);
}
Assert.assertNotNull(connection);
String uri = destination.getScheme() + "://" + destination.getHost() + ":" + destination.getPort();
client.getCookieStore().add(URI.create(uri), new HttpCookie("foo", "bar"));
client.stop();
Assert.assertEquals(0, client.getDestinations().size());
Assert.assertEquals(0, connectionPool.getIdleConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnectionCount());
Assert.assertFalse(connection.getEndPoint().isOpen());
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class HttpCookieTest method test_CookieIsSent.
@Test
public void test_CookieIsSent() throws Exception {
final String name = "foo";
final String value = "bar";
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Cookie[] cookies = request.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.length);
Cookie cookie = cookies[0];
Assert.assertEquals(name, cookie.getName());
Assert.assertEquals(value, cookie.getValue());
}
});
String host = "localhost";
int port = connector.getLocalPort();
String path = "/path";
String uri = scheme + "://" + host + ":" + port;
HttpCookie cookie = new HttpCookie(name, value);
client.getCookieStore().add(URI.create(uri), cookie);
Response response = client.GET(scheme + "://" + host + ":" + port + path);
Assert.assertEquals(200, response.getStatus());
}
use of java.net.HttpCookie in project jetty.project by eclipse.
the class Usage method testCookie.
@Test
public void testCookie() throws Exception {
HttpClient client = new HttpClient();
client.start();
// Set a cookie to be sent in requests that match the cookie's domain
client.getCookieStore().add(URI.create("http://host:8080/path"), new HttpCookie("name", "value"));
// Send a request for the cookie's domain
Response response = client.newRequest("host", 8080).send();
Assert.assertEquals(200, response.getStatus());
}
Aggregations