From e80de55929e32c00b0677574617ed0fa780ce880 Mon Sep 17 00:00:00 2001 From: nyo Date: Tue, 4 Apr 2017 19:48:58 +0200 Subject: [PATCH] Added httpOnly cookie ResultMatcher --- .../test/web/servlet/result/CookieResultMatchers.java | 10 ++++++++++ .../resultmatchers/CookieAssertionTests.java | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java index 45ef7c90149..bafe7e6260f 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java @@ -198,4 +198,14 @@ public class CookieResultMatchers { }; } + /** + * Assert whether the cookie must be httpOnly. + */ + public ResultMatcher httpOnly(final String name, final boolean httpOnly) { + return result -> { + Cookie cookie = result.getResponse().getCookie(name); + assertEquals("Response cookie httpOnly", httpOnly, cookie.isHttpOnly()); + }; + } + } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/CookieAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/CookieAssertionTests.java index 7fa667c211e..d68325e00f9 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/CookieAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/CookieAssertionTests.java @@ -46,7 +46,7 @@ public class CookieAssertionTests { public void setup() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setCookieDomain("domain"); - + localeResolver.setCookieHttpOnly(true); this.mockMvc = standaloneSetup(new SimpleController()) .addInterceptors(new LocaleChangeInterceptor()) .setLocaleResolver(localeResolver) @@ -62,7 +62,7 @@ public class CookieAssertionTests { @Test public void testNotExists() throws Exception { - this.mockMvc.perform(get("/")).andExpect(cookie().doesNotExist("unknowCookie")); + this.mockMvc.perform(get("/")).andExpect(cookie().doesNotExist("unknownCookie")); } @Test @@ -101,6 +101,10 @@ public class CookieAssertionTests { 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 private static class SimpleController {