public void validate(TestContext context, Iterable<Cookie> cookies, String[] expectedNames, String[] expectedVals) { List<String> foundNames = new ArrayList<>(); List<String> foundVals = new ArrayList<>(); for (String s : expectedNames) { for (Cookie c : cookies) { if (c.name().equals(s)) { foundNames.add(c.name()); foundVals.add(c.value()); break; } } } assertArrayEquals(context, expectedNames, foundNames.toArray()); if (expectedVals != null) { assertArrayEquals(context, expectedVals, foundVals.toArray()); } int count = 0; Iterator<Cookie> iter = cookies.iterator(); while (iter.hasNext()) { iter.next(); count++; } assertEquals(expectedNames.length, count); }
@Before public void setUp(TestContext context) throws Exception { vertx = Vertx.vertx(); plainWebClient = buildPlainWebClient(); client = buildClient(plainWebClient, CookieStore.build()); server = vertx.createHttpServer(new HttpServerOptions().setPort(PORT).setHost("0.0.0.0")); }
@Test public void testReceiveAndSendCookieQuestionMark(TestContext context) { testReceiveAndSendCookie(context, "/path?a=b", "/path"); }
@Test public void testReadCookie(TestContext context) { Async async = context.async(); prepareServer(context, req -> { req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test", "toast"))); }); client.get(PORT, "localhost", "/").send(ar -> { context.assertTrue(ar.succeeded()); validate(context, client.cookieStore().get(false, "localhost", "/"), new String[] { "test" }, new String[] { "toast" }); async.complete(); }); }
@Test public void testSharedWebClient(TestContext context) { AtomicInteger cnt = new AtomicInteger(0); prepareServer(context, req -> { req.response().setChunked(true); Cookie c = getCookieValue(req, "test"); if (c != null) { req.response().write("OK"); .mapToObj(val -> val == 0 ? client : buildClient(plainWebClient, CookieStore.build())) .toArray(WebClientSession[]::new);
String cookieValue = "123"; prepareServer(context, req -> { if (!headerValue.equals(req.getHeader(headerName))) { req.response().setStatusCode(500); req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie(cookieName, cookieValue))); } else { Cookie c = getCookieValue(req, cookieName); if (c == null || !cookieValue.equals(c.value())) { req.response().setStatusCode(500);
@Test public void testSendRequest(TestContext context) throws IOException { AtomicInteger count = new AtomicInteger(0); client = buildClient(plainWebClient, new CookieStoreImpl() { @Override public CookieStore put(Cookie cookie) { prepareServer(context, req -> { req.response().headers().add("set-cookie", encodedCookie); });
@Test public void testClientHeaders(TestContext context) { final String headerPrefix = "x-h"; prepareServer(context, req -> { String expected; for (int i = 0; i < 3; i++) {
store.put(c); validate(context, store.get(false, "www.vertx.io", "/"), new String[] { "a", "b" }, new String[] { "1", "20"} ); validate(context, store.get(false, "a.www.vertx.io", "/"), new String[] { "a", "b" }, new String[] { "1", "20"}); validate(context, store.get(false, "test.vertx.io", "/"), new String[] { "a", "b" }, new String[] { "1", "2" }); validate(context, store.get(false, "www.vertx.io", "/web-client"), new String[] { "a", "b", "c", "d" }, new String[] { "1", "200", "3", "4" }); validate(context, store.get(true, "test.vertx.io", "/"), new String[] { "a", "b", "e" }, new String[] { "1", "2", "5" });
@Test public void testRequestIsPrepared(TestContext context) { prepareServer(context, req -> { req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test", "toast"))); }); r.send(ar -> { async.complete(); validate(context, client.cookieStore().get(false, "localhost", "/"), new String[] { "test" }, new String[] { "toast" }); });
public void testReceiveAndSendCookie(TestContext context, String pathToCall, String cookiePath) { prepareServer(context, req -> { req.response().setChunked(true); Cookie c = new DefaultCookie("test", "toast"); c.setPath(cookiePath); req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(c)); c = getCookieValue(req, "test"); if (c != null) { req.response().write("OK");
@Test public void testSessionHeaders(TestContext context) { String headerName = "x-client-header"; String headerVal = "MY-HEADER"; prepareServer(context, req -> { req.response().setChunked(true); if (headerVal.equals(req.getHeader(headerName))) { req.response().write("OK"); } else { req.response().write("ERR"); } }); Async async = context.async(); client.addHeader(headerName, headerVal); client.get("/").send(ar -> { context.assertTrue(ar.succeeded()); HttpResponse<Buffer> res = ar.result(); context.assertEquals(200, res.statusCode()); context.assertEquals("OK", res.bodyAsString()); async.complete(); }); }
@Test public void testReadManyCookies(TestContext context) { Async async = context.async(); prepareServer(context, req -> { req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test1", "toast1"))); req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test2", "toast2"))); req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test3", "toast3"))); }); client.get(PORT, "localhost", "/").send(ar -> { context.assertTrue(ar.succeeded()); validate(context, client.cookieStore().get(false, "localhost", "/"), new String[] { "test1" ,"test2", "test3" }, new String[] { "toast1", "toast2", "toast3" }); async.complete(); }); }
@Test public void testReceiveAndSendCookieRegularPath(TestContext context) { testReceiveAndSendCookie(context, "/", "/"); }
String cookieName = "a"; prepareServer(testContext, req -> { req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie(cookieName, req.toString()))); });
@Test public void testReceiveAndSendCookieHash(TestContext context) { testReceiveAndSendCookie(context, "/path#fragment", "/path"); }