CookieEnricher { List<Cookie> enrich(List<Cookie> cookies, final TestContext context) { final List<Cookie> enrichedCookies = new LinkedList<>(); for (final Cookie cookie : cookies) { final Cookie enrichedCookie = new Cookie(cookie.getName(), cookie.getValue()); if (cookie.getValue() != null) { enrichedCookie.setValue(context.replaceDynamicContentInString(cookie.getValue())); } if (cookie.getComment() != null) { enrichedCookie.setComment(context.replaceDynamicContentInString(cookie.getComment())); } if (cookie.getPath() != null) { enrichedCookie.setPath(context.replaceDynamicContentInString(cookie.getPath())); } if (cookie.getDomain() != null) { enrichedCookie.setDomain(context.replaceDynamicContentInString(cookie.getDomain())); } enrichedCookie.setMaxAge(cookie.getMaxAge()); enrichedCookie.setVersion(cookie.getVersion()); enrichedCookie.setHttpOnly(cookie.isHttpOnly()); enrichedCookie.setSecure(cookie.getSecure()); enrichedCookies.add(enrichedCookie); } return enrichedCookies; }    }
@Test public void testCookiesArePreserved(){ cookie.setMaxAge(42); cookie.setVersion(24); cookie.setHttpOnly(true); cookie.setSecure(true); final List<Cookie> cookies = Collections.singletonList(cookie); final List<Cookie> enrichedCookies = cookieEnricher.enrich(cookies, testContextMock); assertEquals(enrichedCookies.size(), 1); final Cookie enrichedCookie = enrichedCookies.get(0); assertEquals(enrichedCookie.getName(), "foo"); assertEquals(enrichedCookie.getValue(), "bar"); assertEquals(enrichedCookie.getMaxAge(), 42); assertEquals(enrichedCookie.getVersion(), 24); assertTrue(enrichedCookie.isHttpOnly()); assertTrue(enrichedCookie.getSecure()); }
@Test public void testTwoCookiesArePreserved(){ final List<Cookie> cookies = Arrays.asList(cookie, cookie); final List<Cookie> enrichedCookies = cookieEnricher.enrich(cookies, testContextMock); assertEquals(enrichedCookies.size(), 2); }
@Test public void testValueVariablesAreReplaced(){ Cookie cookie = new Cookie("foo", "${foobar}"); final List<Cookie> cookies = Collections.singletonList(cookie); testContextMock.setVariable("foobar", "bar"); final List<Cookie> enrichedCookies = cookieEnricher.enrich(cookies, testContextMock); assertEquals(enrichedCookies.get(0).getName(), "foo"); assertEquals(enrichedCookies.get(0).getValue(), "bar"); }
@Test public void testCommentVariablesAreReplaced(){ cookie.setComment("${variable}"); final List<Cookie> cookies = Collections.singletonList(cookie); testContextMock.setVariable("variable", "foobar"); final List<Cookie> enrichedCookies = cookieEnricher.enrich(cookies, testContextMock); assertEquals(enrichedCookies.get(0).getName(), "foo"); assertEquals(enrichedCookies.get(0).getComment(), "foobar"); }
@Test public void testPathVariablesAreReplaced(){ cookie.setPath("/path/to/${variable}"); final List<Cookie> cookies = Collections.singletonList(cookie); testContextMock.setVariable("variable", "foobar"); final List<Cookie> enrichedCookies = cookieEnricher.enrich(cookies, testContextMock); assertEquals(enrichedCookies.get(0).getName(), "foo"); assertEquals(enrichedCookies.get(0).getPath(), "/path/to/foobar"); }
@Test public void testDomainVariablesAreReplaced(){ cookie.setDomain("${variable}"); final List<Cookie> cookies = Collections.singletonList(cookie); testContextMock.setVariable("variable", "localhost"); final List<Cookie> enrichedCookies = cookieEnricher.enrich(cookies, testContextMock); assertEquals(enrichedCookies.get(0).getName(), "foo"); assertEquals(enrichedCookies.get(0).getDomain(), "localhost"); }