private boolean setSpecialHeader(String name, Object value) { if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { setContentType(value.toString()); return true; } else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { setContentLength(value instanceof Number ? ((Number) value).intValue() : Integer.parseInt(value.toString())); return true; } else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) { HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_LANGUAGE, value.toString()); Locale language = headers.getContentLanguage(); setLocale(language != null ? language : Locale.getDefault()); return true; } else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) { MockCookie cookie = MockCookie.parse(value.toString()); addCookie(cookie); return true; } else { return false; } }
@Test public void cookies() { Cookie cookie = new Cookie("foo", "bar"); cookie.setPath("/path"); cookie.setDomain("example.com"); cookie.setMaxAge(0); cookie.setSecure(true); cookie.setHttpOnly(true); response.addCookie(cookie); assertEquals("foo=bar; Path=/path; Domain=example.com; " + "Max-Age=0; Expires=Thu, 1 Jan 1970 00:00:00 GMT; " + "Secure; HttpOnly", response.getHeader(HttpHeaders.SET_COOKIE)); }
@Test public void buildResponseHeadersNullDomainDefaulted() throws Exception { Cookie cookie = new Cookie("cookieA", "valueA"); this.response.addCookie(cookie); WebResponse webResponse = this.responseBuilder.build(); List<NameValuePair> responseHeaders = webResponse.getResponseHeaders(); assertThat(responseHeaders.size(), equalTo(1)); NameValuePair header = responseHeaders.get(0); assertThat(header.getName(), equalTo("Set-Cookie")); assertThat(header.getValue(), equalTo("cookieA=valueA")); }
@Test public void buildResponseHeaders() throws Exception { this.response.addHeader("Content-Type", "text/html"); this.response.addHeader("X-Test", "value"); Cookie cookie = new Cookie("cookieA", "valueA"); cookie.setDomain("domain"); cookie.setPath("/path"); cookie.setMaxAge(1800); cookie.setSecure(true); cookie.setHttpOnly(true); this.response.addCookie(cookie); WebResponse webResponse = this.responseBuilder.build(); List<NameValuePair> responseHeaders = webResponse.getResponseHeaders(); assertThat(responseHeaders.size(), equalTo(3)); NameValuePair header = responseHeaders.get(0); assertThat(header.getName(), equalTo("Content-Type")); assertThat(header.getValue(), equalTo("text/html")); header = responseHeaders.get(1); assertThat(header.getName(), equalTo("X-Test")); assertThat(header.getValue(), equalTo("value")); header = responseHeaders.get(2); assertThat(header.getName(), equalTo("Set-Cookie")); assertThat(header.getValue(), startsWith("cookieA=valueA; Path=/path; Domain=domain; Max-Age=1800; Expires=")); assertThat(header.getValue(), endsWith("; Secure; HttpOnly")); }
@Test public void addMockCookie() { MockCookie mockCookie = new MockCookie("SESSION", "123"); mockCookie.setPath("/"); mockCookie.setDomain("example.com"); mockCookie.setMaxAge(0); mockCookie.setSecure(true); mockCookie.setHttpOnly(true); mockCookie.setSameSite("Lax"); response.addCookie(mockCookie); assertEquals("SESSION=123; Path=/; Domain=example.com; Max-Age=0; " + "Expires=Thu, 1 Jan 1970 00:00:00 GMT; Secure; HttpOnly; SameSite=Lax", response.getHeader(HttpHeaders.SET_COOKIE)); }
@Test public void responseWithCookie() { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(HttpServletResponse.SC_OK); Cookie cookie = new Cookie("name", "value"); cookie.setDomain("localhost"); cookie.setHttpOnly(true); response.addCookie(cookie); OperationResponse operationResponse = this.factory.convert(response); assertThat(operationResponse.getHeaders()).hasSize(1); assertThat(operationResponse.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE, Collections.singletonList("name=value; Domain=localhost; HttpOnly")); }
private boolean setSpecialHeader(String name, Object value) { if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { setContentType(value.toString()); return true; } else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { setContentLength(value instanceof Number ? ((Number) value).intValue() : Integer.parseInt(value.toString())); return true; } else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) { HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_LANGUAGE, value.toString()); Locale language = headers.getContentLanguage(); setLocale(language != null ? language : Locale.getDefault()); return true; } else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) { MockCookie cookie = MockCookie.parse(value.toString()); addCookie(cookie); return true; } else { return false; } }