/** * {@inheritDoc} */ @Override public String getRequestedSessionId() { Cookie sessionCookie = getCookie(HttpSessionImpl.COOKIE_NAME); if (sessionCookie == null) { return null; } return sessionCookie.getValue(); }
sb.append(cookie.getName()) .append(EQUALS) .append(StringUtilities.urlEncode(cookie.getValue()));
@Test public void shouldAllowLongValues() { Cookie cookie = new Cookie("someName", 1545454454544844l); assertThat(cookie.getValue(), is("1545454454544844")); }
@Test public void shouldAllowDoubleValues() { Cookie cookie = new Cookie("someName", 22.33); assertThat(cookie.getValue(), is("22.33")); } }
@Test public void shouldAllowBooleanValues() { Cookie cookie = new Cookie("someName", true); assertThat(cookie.getValue(), is("true")); }
@Test public void shouldAllowIntValues() { Cookie cookie = new Cookie("someName", 14); assertThat(cookie.getValue(), is("14")); }
/** * {@inheritDoc} */ @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { int pageHits = 0; if (request.getCookie(PAGE_HITS_COOKIE_NAME) != null) { pageHits = Integer.parseInt(request.getCookie(PAGE_HITS_COOKIE_NAME).getValue()); } ++pageHits; response.addCookie(new Cookie(PAGE_HITS_COOKIE_NAME, pageHits)); String firstVisitedAt; if (request.getCookie(FIRST_VISITED_AT_COOKIE_NAME) != null) { firstVisitedAt = request.getCookie(FIRST_VISITED_AT_COOKIE_NAME).getValue(); } else { firstVisitedAt = (new Date()).toString(); response.addCookie(new Cookie(FIRST_VISITED_AT_COOKIE_NAME, firstVisitedAt)); } response.getWriter().println("<p>Cookie page hits: " + pageHits + "</p>"); response.getWriter().println("<p>First visited at: " + firstVisitedAt + "</p>"); } }
@Test public void shouldSetCookieAndPersistForValidSession() throws IOException { HttpSessionImpl session = new HttpSessionImpl("123", System.currentTimeMillis()); servletContext.handleSession(session, response); verify(sessionStorage, times(1)).persistSession(session); assertThat(response.getCookies().size(), is(greaterThan(0))); for (Cookie cookie : response.getCookies()) { if (cookie.getName().equals(HttpSessionImpl.COOKIE_NAME)) { assertThat(cookie.getValue(), is(not(nullValue()))); return; } } fail("Session cookie was not set."); }
@Test public void shouldParseCookieHavingSpaceInValue() throws MalformedInputException { String value = "value containing spaces"; Map<String, Cookie> cookies = cookieParser.parse("name=" + value); assertThat(cookies, hasKey("name")); assertThat(cookies.get("name").getValue(), is(value)); }
@Test public void shouldWorkGettersAndSetters() { Cookie cookie = new Cookie("someName", "someValue"); cookie.setComment("comment"); cookie.setDomain("example.com"); cookie.setPath("/somepath"); cookie.setSecure(true); cookie.setHttpOnly(true); assertThat(cookie.getName(), is("someName")); assertThat(cookie.getValue(), is("someValue")); cookie.setValue("SomeValue2"); assertThat(cookie.getValue(), is("SomeValue2")); assertThat(cookie.getComment(), is("comment")); assertThat(cookie.getDomain(), is("example.com")); assertThat(cookie.getPath(), is("/somepath")); assertThat(cookie.isSecure(), is(true)); assertThat(cookie.isHttpOnly(), is(true)); assertThat(cookie.getMaxAge(), is(-1)); cookie.setMaxAge(125); assertThat(cookie.getMaxAge(), is(125)); }
@Test public void shouldParseCookieHavingUrlEncodedValue() throws MalformedInputException { String value = "&<>some value"; Map<String, Cookie> cookies = cookieParser.parse("name=" + StringUtilities.urlEncode(value)); assertThat(cookies, hasKey("name")); assertThat(cookies.get("name").getValue(), is(value)); }