@Override public Map<String, String> cookies() { Map<String, String> map = new HashMap<>(8); this.cookies.forEach(cookie -> map.put(cookie.name(), cookie.value())); return map; }
@Override public Map<String, String> cookies() { Map<String, String> map = new HashMap<>(8); this.cookies.forEach(cookie -> map.put(cookie.name(), cookie.value())); return map; }
@Override public Response removeCookie(@NonNull String name) { Optional<Cookie> cookieOpt = this.cookies.stream().filter(cookie -> cookie.name().equals(name)).findFirst(); cookieOpt.ifPresent(cookie -> { cookie.setValue(""); cookie.setMaxAge(-1); }); Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, ""); nettyCookie.setMaxAge(-1); this.cookies.add(nettyCookie); return this; }
private static ImmutableMap<String, String> doExtractCookies(final String[] cookieStrings) { ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); for (String cookie : cookieStrings) { Set<Cookie> decodeCookies = ServerCookieDecoder.STRICT.decode(cookie); for (Cookie decodeCookie : decodeCookies) { builder.put(decodeCookie.name(), decodeCookie.value()); } } return builder.build(); } }
@Override public Response removeCookie(@NonNull String name) { Optional<Cookie> cookieOpt = this.cookies.stream().filter(cookie -> cookie.name().equals(name)).findFirst(); cookieOpt.ifPresent(cookie -> { cookie.setValue(""); cookie.setMaxAge(-1); }); Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, ""); nettyCookie.setMaxAge(-1); this.cookies.add(nettyCookie); return this; }
@Get("/welcome") public HttpResponse welcome(Cookies cookies) { final String name = cookies.stream().filter(c -> "username".equals(c.name())) .map(Cookie::value).findFirst() .orElseThrow(() -> new IllegalArgumentException("No username is found.")); return HttpResponse.of(HttpStatus.OK, MediaType.HTML_UTF_8, "<html><body>Hello, %s! You can see this message " + "because you've been authenticated by SSOCircle.</body></html>", name); } }
@Override public MultiValueMap<String, ResponseCookie> getCookies() { MultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>(); this.response.cookies().values().stream().flatMap(Collection::stream) .forEach(cookie -> result.add(cookie.name(), ResponseCookie.from(cookie.name(), cookie.value()) .domain(cookie.domain()) .path(cookie.path()) .maxAge(cookie.maxAge()) .secure(cookie.isSecure()) .httpOnly(cookie.isHttpOnly()) .build())); return CollectionUtils.unmodifiableMultiValueMap(result); }
@Override public MultiValueMap<String, ResponseCookie> getCookies() { MultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>(); this.response.cookies().values().stream().flatMap(Collection::stream) .forEach(cookie -> result.add(cookie.name(), ResponseCookie.from(cookie.name(), cookie.value()) .domain(cookie.domain()) .path(cookie.path()) .maxAge(cookie.maxAge()) .secure(cookie.isSecure()) .httpOnly(cookie.isHttpOnly()) .build())); return CollectionUtils.unmodifiableMultiValueMap(result); }
private void add(String requestDomain, String requestPath, Cookie cookie) { AbstractMap.SimpleEntry<String, Boolean> pair = cookieDomain(cookie.domain(), requestDomain); String keyDomain = pair.getKey(); boolean hostOnly = pair.getValue(); String keyPath = cookiePath(cookie.path(), requestPath); CookieKey key = new CookieKey(cookie.name().toLowerCase(), keyDomain, keyPath); if (hasCookieExpired(cookie, 0)) cookieJar.remove(key); else cookieJar.put(key, new StoredCookie(cookie, hostOnly, cookie.maxAge() != Cookie.UNDEFINED_MAX_AGE)); }
private boolean cookieHeaderAlreadyExists(HttpResponse response, Cookie cookieValue) { List<String> setCookieHeaders = response.getHeader(SET_COOKIE.toString()); for (String setCookieHeader : setCookieHeaders) { String existingCookieName = ClientCookieDecoder.LAX.decode(setCookieHeader).name(); String existingCookieValue = ClientCookieDecoder.LAX.decode(setCookieHeader).value(); if (existingCookieName.equalsIgnoreCase(cookieValue.getName().getValue()) && existingCookieValue.equalsIgnoreCase(cookieValue.getValue().getValue())) { return true; } } return false; }
private boolean cookieHeaderAlreadyExists(HttpResponse response, org.mockserver.model.Cookie cookieValue) { List<String> setCookieHeaders = response.getHeader(SET_COOKIE.toString()); for (String setCookieHeader : setCookieHeaders) { String existingCookieName = ClientCookieDecoder.LAX.decode(setCookieHeader).name(); String existingCookieValue = ClientCookieDecoder.LAX.decode(setCookieHeader).value(); if (existingCookieName.equalsIgnoreCase(cookieValue.getName().getValue()) && existingCookieValue.equalsIgnoreCase(cookieValue.getValue().getValue())) { return true; } } return false; } }
private void encode(StringBuilder buf, Cookie c) { final String name = c.name(); final String value = c.value() != null ? c.value() : ""; validateCookie(name, value); if (c.wrap()) { addQuoted(buf, name, value); } else { add(buf, name, value); } } }
@Test public void should_set_and_recognize_cookie() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); }
@Test public void should_set_and_recognize_cookie_with_domain() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-domain"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.domain(), is("github.com")); }
@Test public void should_set_and_recognize_cookie_with_secure() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-secure"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.isSecure(), is(true)); }
@Test public void should_set_and_recognize_cookie_with_path() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-path"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.path(), is("/")); }
@Test public void should_set_and_recognize_cookie_with_max_age() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-max-age"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.maxAge(), is(3600L)); }
@Test public void should_set_and_recognize_cookie_with_http_only() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-http-only"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.isHttpOnly(), is(true)); }
/** * Parse netty cookie to {@link Cookie}. * * @param nettyCookie netty raw cookie instance */ private void parseCookie(io.netty.handler.codec.http.cookie.Cookie nettyCookie) { var cookie = new Cookie(); cookie.name(nettyCookie.name()); cookie.value(nettyCookie.value()); cookie.httpOnly(nettyCookie.isHttpOnly()); cookie.path(nettyCookie.path()); cookie.domain(nettyCookie.domain()); cookie.maxAge(nettyCookie.maxAge()); this.cookies.put(cookie.name(), cookie); }
/** * Parse netty cookie to {@link Cookie}. * * @param nettyCookie netty raw cookie instance */ private void parseCookie(io.netty.handler.codec.http.cookie.Cookie nettyCookie) { var cookie = new Cookie(); cookie.name(nettyCookie.name()); cookie.value(nettyCookie.value()); cookie.httpOnly(nettyCookie.isHttpOnly()); cookie.path(nettyCookie.path()); cookie.domain(nettyCookie.domain()); cookie.maxAge(nettyCookie.maxAge()); this.cookies.put(cookie.name(), cookie); }