MockHttpSevlet[Request|Response] set cookie headers
Issue: SPR-15225
This commit is contained in:
@@ -251,12 +251,14 @@ public class MockHttpServletRequestTests {
|
||||
request.setCookies(cookie1, cookie2);
|
||||
|
||||
Cookie[] cookies = request.getCookies();
|
||||
List<String> cookieHeaders = Collections.list(request.getHeaders("Cookie"));
|
||||
|
||||
assertEquals(2, cookies.length);
|
||||
assertEquals("foo", cookies[0].getName());
|
||||
assertEquals("bar", cookies[0].getValue());
|
||||
assertEquals("baz", cookies[1].getName());
|
||||
assertEquals("qux", cookies[1].getValue());
|
||||
assertEquals(Arrays.asList("foo=bar", "baz=qux"), cookieHeaders);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -19,10 +19,14 @@ package org.springframework.mock.web;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -155,6 +159,22 @@ public class MockHttpServletResponseTests {
|
||||
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
|
||||
}
|
||||
|
||||
@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, 01 Jan 1970 00:00:00 GMT;" +
|
||||
"Secure;HttpOnly", response.getHeader(HttpHeaders.SET_COOKIE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void servletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
|
||||
assertFalse(response.isCommitted());
|
||||
|
||||
@@ -116,8 +116,8 @@ public class MockWebResponseBuilderTests {
|
||||
assertThat(header.getValue(), equalTo("value"));
|
||||
header = responseHeaders.get(2);
|
||||
assertThat(header.getName(), equalTo("Set-Cookie"));
|
||||
assertThat(header.getValue(), startsWith("cookieA=valueA;domain=domain;path=/path;expires="));
|
||||
assertThat(header.getValue(), endsWith(";secure;httpOnly"));
|
||||
assertThat(header.getValue(), startsWith("cookieA=valueA;Path=/path;Domain=domain;Max-Age=1800;Expires="));
|
||||
assertThat(header.getValue(), endsWith(";Secure;HttpOnly"));
|
||||
}
|
||||
|
||||
// SPR-14169
|
||||
|
||||
@@ -19,8 +19,8 @@ package org.springframework.test.web.servlet.result;
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -40,7 +40,8 @@ import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.FlashMap;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PrintingResultHandler}.
|
||||
@@ -114,10 +115,18 @@ public class PrintingResultHandlerTests {
|
||||
|
||||
this.handler.handle(this.mvcResult);
|
||||
|
||||
// Manually validate cookie values since maxAge changes...
|
||||
List<String> cookieValues = this.response.getHeaders("Set-Cookie");
|
||||
assertEquals(2, cookieValues.size());
|
||||
assertEquals("cookie=cookieValue", cookieValues.get(0));
|
||||
assertTrue("Actual: " + cookieValues.get(1), cookieValues.get(1).startsWith(
|
||||
"enigma=42;Path=/crumbs;Domain=.example.com;Max-Age=1234;Expires="));
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("header", "headerValue");
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
headers.setLocation(new URI("/redirectFoo"));
|
||||
headers.put("Set-Cookie", cookieValues);
|
||||
|
||||
String heading = "MockHttpServletResponse";
|
||||
assertValue(heading, "Status", this.response.getStatus());
|
||||
|
||||
Reference in New Issue
Block a user