Allow for HttpOnly cookie result matcher

Issue: SPR-15488
(cherry picked from commit 04f0f13)
This commit is contained in:
Juergen Hoeller 2017-04-27 21:32:11 +02:00
parent 38089d8e66
commit 9b647021f7
2 changed files with 61 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import static org.springframework.test.util.AssertionErrors.*;
/** /**
* Factory for response cookie assertions. * Factory for response cookie assertions.
*
* <p>An instance of this class is typically accessed via * <p>An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#cookie}. * {@link MockMvcResultMatchers#cookie}.
* *
@ -52,9 +53,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertTrue("Response cookie not found: " + name, cookie != null); assertThat("Response cookie '" + name + "'", cookie.getValue(), matcher);
assertThat("Response cookie", cookie.getValue(), matcher);
} }
}; };
} }
@ -66,8 +66,7 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertTrue("Response cookie not found: " + name, cookie != null);
assertEquals("Response cookie", expectedValue, cookie.getValue()); assertEquals("Response cookie", expectedValue, cookie.getValue());
} }
}; };
@ -81,8 +80,7 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name); getCookie(result, name);
assertTrue("No cookie with name: " + name, cookie != null);
} }
}; };
} }
@ -96,7 +94,7 @@ public class CookieResultMatchers {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = result.getResponse().getCookie(name);
assertTrue("Unexpected cookie with name " + name, cookie == null); assertTrue("Unexpected cookie with name '" + name + "'", cookie == null);
} }
}; };
} }
@ -108,9 +106,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertTrue("No cookie with name: " + name, cookie != null); assertThat("Response cookie '" + name + "' maxAge", cookie.getMaxAge(), matcher);
assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher);
} }
}; };
} }
@ -122,9 +119,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertTrue("No cookie with name: " + name, cookie != null); assertEquals("Response cookie '" + name + "' maxAge", maxAge, cookie.getMaxAge());
assertEquals("Response cookie maxAge", maxAge, cookie.getMaxAge());
} }
}; };
} }
@ -136,8 +132,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertThat("Response cookie path", cookie.getPath(), matcher); assertThat("Response cookie '" + name + "' path", cookie.getPath(), matcher);
} }
}; };
} }
@ -146,8 +142,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertEquals("Response cookie path", path, cookie.getPath()); assertEquals("Response cookie '" + name + "' path", path, cookie.getPath());
} }
}; };
} }
@ -159,8 +155,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertThat("Response cookie domain", cookie.getDomain(), matcher); assertThat("Response cookie '" + name + "' domain", cookie.getDomain(), matcher);
} }
}; };
} }
@ -172,8 +168,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertEquals("Response cookie domain", domain, cookie.getDomain()); assertEquals("Response cookie '" + name + "' domain", domain, cookie.getDomain());
} }
}; };
} }
@ -185,8 +181,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertThat("Response cookie comment", cookie.getComment(), matcher); assertThat("Response cookie '" + name + "' comment", cookie.getComment(), matcher);
} }
}; };
} }
@ -198,8 +194,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertEquals("Response cookie comment", comment, cookie.getComment()); assertEquals("Response cookie '" + name + "' comment", comment, cookie.getComment());
} }
}; };
} }
@ -211,8 +207,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertThat("Response cookie version", cookie.getVersion(), matcher); assertThat("Response cookie '" + name + "' version", cookie.getVersion(), matcher);
} }
}; };
} }
@ -224,8 +220,8 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertEquals("Response cookie version", version, cookie.getVersion()); assertEquals("Response cookie '" + name + "' version", version, cookie.getVersion());
} }
}; };
} }
@ -237,10 +233,31 @@ public class CookieResultMatchers {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name); Cookie cookie = getCookie(result, name);
assertEquals("Response cookie secure", secure, cookie.getSecure()); assertEquals("Response cookie '" + name + "' secure", secure, cookie.getSecure());
} }
}; };
} }
/**
* Assert whether the cookie must be HTTP only.
* @since 4.3.9
*/
public ResultMatcher httpOnly(final String name, final boolean httpOnly) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
Cookie cookie = getCookie(result, name);
assertEquals("Response cookie '" + name + "' httpOnly", httpOnly, cookie.isHttpOnly());
}
};
}
private static Cookie getCookie(MvcResult result, String name) {
Cookie cookie = result.getResponse().getCookie(name);
assertTrue("No cookie with name '" + name + "'", cookie != null);
return cookie;
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
* Examples of expectations on response cookies values. * Examples of expectations on response cookies values.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Nikola Yovchev
*/ */
public class CookieAssertionTests { public class CookieAssertionTests {
@ -46,6 +47,7 @@ public class CookieAssertionTests {
public void setup() { public void setup() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver(); CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setCookieDomain("domain"); localeResolver.setCookieDomain("domain");
localeResolver.setCookieHttpOnly(true);
this.mockMvc = standaloneSetup(new SimpleController()) this.mockMvc = standaloneSetup(new SimpleController())
.addInterceptors(new LocaleChangeInterceptor()) .addInterceptors(new LocaleChangeInterceptor())
@ -55,6 +57,7 @@ public class CookieAssertionTests {
.build(); .build();
} }
@Test @Test
public void testExists() throws Exception { public void testExists() throws Exception {
this.mockMvc.perform(get("/")).andExpect(cookie().exists(COOKIE_NAME)); this.mockMvc.perform(get("/")).andExpect(cookie().exists(COOKIE_NAME));
@ -62,7 +65,7 @@ public class CookieAssertionTests {
@Test @Test
public void testNotExists() throws Exception { public void testNotExists() throws Exception {
this.mockMvc.perform(get("/")).andExpect(cookie().doesNotExist("unknowCookie")); this.mockMvc.perform(get("/")).andExpect(cookie().doesNotExist("unknownCookie"));
} }
@Test @Test
@ -101,6 +104,11 @@ public class CookieAssertionTests {
this.mockMvc.perform(get("/")).andExpect(cookie().secure(COOKIE_NAME, false)); this.mockMvc.perform(get("/")).andExpect(cookie().secure(COOKIE_NAME, false));
} }
@Test
public void testHttpOnly() throws Exception {
this.mockMvc.perform(get("/")).andExpect(cookie().httpOnly(COOKIE_NAME, true));
}
@Controller @Controller
private static class SimpleController { private static class SimpleController {
@ -110,4 +118,5 @@ public class CookieAssertionTests {
return "home"; return "home";
} }
} }
} }