ResultMatcher noCookies() { return result -> assertThat(result.getResponse().getCookies()).isEmpty(); }
public WebResponse getResponse(WebRequest webRequest) throws IOException { long startTime = System.currentTimeMillis(); HtmlUnitRequestBuilder requestBuilder = new HtmlUnitRequestBuilder(this.sessions, this.webClient, webRequest); requestBuilder.setContextPath(this.contextPath); MockHttpServletResponse httpServletResponse = getResponse(requestBuilder); String forwardedUrl = httpServletResponse.getForwardedUrl(); while (forwardedUrl != null) { requestBuilder.setForwardPostProcessor(new ForwardRequestPostProcessor(forwardedUrl)); httpServletResponse = getResponse(requestBuilder); forwardedUrl = httpServletResponse.getForwardedUrl(); } storeCookies(webRequest, httpServletResponse.getCookies()); return new MockWebResponseBuilder(startTime, webRequest, httpServletResponse).build(); }
private void nextRequest() throws Exception { Map<String, Cookie> nameToCookie = new HashMap<>(); if (this.request.getCookies() != null) { for (Cookie cookie : this.request.getCookies()) { nameToCookie.put(cookie.getName(), cookie); } } for (Cookie cookie : this.response.getCookies()) { nameToCookie.put(cookie.getName(), cookie); } Cookie[] nextRequestCookies = new ArrayList<>(nameToCookie.values()) .toArray(new Cookie[0]); setupRequest(); this.request.setCookies(nextRequestCookies); }
@Test public void onNewSessionTwiceNewId() throws Exception { Session newSession = new MapSession(); this.strategy.setSessionId(this.request, this.response, this.session.getId()); this.strategy.setSessionId(this.request, this.response, newSession.getId()); Cookie[] cookies = this.response.getCookies(); assertThat(cookies).hasSize(2); assertThat(base64Decode(cookies[0].getValue())).isEqualTo(this.session.getId()); assertThat(base64Decode(cookies[1].getValue())).isEqualTo(newSession.getId()); }
@Test public void onNewSessionTwiceSameId() throws Exception { this.strategy.setSessionId(this.request, this.response, this.session.getId()); this.strategy.setSessionId(this.request, this.response, this.session.getId()); assertThat(this.response.getCookies()).hasSize(1); }
/** * Print the response. */ protected void printResponse(MockHttpServletResponse response) throws Exception { String body = (response.getCharacterEncoding() != null ? response.getContentAsString() : MISSING_CHARACTER_ENCODING); this.printer.printValue("Status", response.getStatus()); this.printer.printValue("Error message", response.getErrorMessage()); this.printer.printValue("Headers", getResponseHeaders(response)); this.printer.printValue("Content type", response.getContentType()); this.printer.printValue("Body", body); this.printer.printValue("Forwarded URL", response.getForwardedUrl()); this.printer.printValue("Redirected URL", response.getRedirectedUrl()); printCookies(response.getCookies()); }
@Test public void testDeleteSavedAccount() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); LoginInfoEndpoint endpoint = getEndpoint(); String userId = "testUserId"; String result = endpoint.deleteSavedAccount(request, response, userId); Cookie[] cookies = response.getCookies(); assertEquals(cookies.length, 1); assertEquals(cookies[0].getName(), "Saved-Account-" + userId); assertEquals(cookies[0].getMaxAge(), 0); assertEquals("redirect:/login", result); } @Test
put(TimingFilter.RESPONSE_TIME_MILLISECONDS, responseTime); }}); restAssuredResponse.setCookies(convertCookies(response.getCookies()));
/** * http/logout custom */ @Test @WithMockUser public void logoutWhenUsingVariousCustomizationsMatchesNamespace() throws Exception { this.spring.register(CustomHttpLogoutConfig.class).autowire(); this.mvc.perform(post("/custom-logout").with(csrf())) .andExpect(authenticated(false)) .andExpect(redirectedUrl("/logout-success")) .andExpect(result -> assertThat(result.getResponse().getCookies()).hasSize(1)) .andExpect(cookie().maxAge("remove", 0)) .andExpect(session(Objects::nonNull)); }
private HttpHeaders extractHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String headerName : response.getHeaderNames()) { for (String value : response.getHeaders(headerName)) { headers.add(headerName, value); } } if (response.getCookies() != null && !headers.containsKey(HttpHeaders.SET_COOKIE)) { for (Cookie cookie : response.getCookies()) { headers.add(HttpHeaders.SET_COOKIE, generateSetCookieHeader(cookie)); } } return headers; }
@Test public void dotMobiTabletDeviceMobilePreferenceTabletIsMobile() throws Exception { device.setDeviceType(DeviceType.TABLET); SiteSwitcherHandlerInterceptor dotMobi = SiteSwitcherHandlerInterceptor.dotMobi("app.com", true); request.addParameter("site_preference", "mobile"); assertFalse(dotMobi.preHandle(request, response, null)); assertEquals(1, response.getCookies().length); assertEquals(".app.com", response.getCookies()[0].getDomain()); assertEquals("MOBILE", response.getCookies()[0].getValue()); assertEquals("http://app.mobi", response.getRedirectedUrl()); }
@Test public void dotMobiNormalDeviceTabletPreferenceRequestMobileSite() throws Exception { device.setDeviceType(DeviceType.NORMAL); request.setServerName("app.mobi"); SiteSwitcherHandlerInterceptor dotMobi = SiteSwitcherHandlerInterceptor.dotMobi("app.com"); request.addParameter("site_preference", "tablet"); assertTrue(dotMobi.preHandle(request, response, null)); assertEquals(1, response.getCookies().length); assertEquals(".app.com", response.getCookies()[0].getDomain()); assertEquals("TABLET", response.getCookies()[0].getValue()); assertNull(response.getRedirectedUrl()); }
@Test public void dotMobiMobileDeviceNormalPreferenceRequestMobileSite() throws Exception { device.setDeviceType(DeviceType.MOBILE); request.setServerName("app.mobi"); SiteSwitcherHandlerInterceptor dotMobi = SiteSwitcherHandlerInterceptor.dotMobi("app.com"); request.addParameter("site_preference", "normal"); assertFalse(dotMobi.preHandle(request, response, null)); assertEquals(1, response.getCookies().length); assertEquals(".app.com", response.getCookies()[0].getDomain()); assertEquals("NORMAL", response.getCookies()[0].getValue()); assertEquals("http://app.com", response.getRedirectedUrl()); }
@Test public void urlPathRootPathNormalDeviceTabletPreferenceRequestTabletSite() throws Exception { SiteSwitcherHandlerInterceptor urlPath = SiteSwitcherHandlerInterceptor.urlPath("/mob", "/tab", "/app"); device.setDeviceType(DeviceType.NORMAL); request.setRequestURI("/app/tab"); request.addParameter("site_preference", "tablet"); assertTrue(urlPath.preHandle(request, response, null)); assertEquals(1, response.getCookies().length); assertEquals("TABLET", response.getCookies()[0].getValue()); assertNull(response.getRedirectedUrl()); }
@Test public void urlPathRootPathMobileDeviceMobilePreferenceRequestTabletSite() throws Exception { SiteSwitcherHandlerInterceptor urlPath = SiteSwitcherHandlerInterceptor.urlPath("/mob", "/tab", "/app"); device.setDeviceType(DeviceType.MOBILE); request.setRequestURI("/app/tab"); request.addParameter("site_preference", "mobile"); assertFalse(urlPath.preHandle(request, response, null)); assertEquals(1, response.getCookies().length); assertEquals("MOBILE", response.getCookies()[0].getValue()); assertEquals("http://" + request.getServerName() + "/app/mob", response.getRedirectedUrl()); }
@Test public void urlPathRootPathTabletDeviceTabletPreferenceRequestMobileSiteWithSlash() throws Exception { SiteSwitcherHandlerInterceptor urlPath = SiteSwitcherHandlerInterceptor.urlPath("/mob", "/tab", "/app"); device.setDeviceType(DeviceType.TABLET); request.setRequestURI("/app/mob/"); request.addParameter("site_preference", "tablet"); assertFalse(urlPath.preHandle(request, response, null)); assertEquals(1, response.getCookies().length); assertEquals("TABLET", response.getCookies()[0].getValue()); assertEquals("http://" + request.getServerName() + "/app/tab/", response.getRedirectedUrl()); }
@Test public void setSitePreferenceTablet() { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); assertNull(repository.loadSitePreference(request)); repository.saveSitePreference(SitePreference.TABLET, request, response); request.setCookies(response.getCookies()); assertEquals(SitePreference.TABLET, repository.loadSitePreference(request)); } }
@Test public void dotMobiTabletDeviceNoPreferenceTabletIsMobileRequestMobileSite() throws Exception { device.setDeviceType(DeviceType.TABLET); request.setServerName("app.mobi"); SiteSwitcherHandlerInterceptor dotMobi = SiteSwitcherHandlerInterceptor.dotMobi("app.com", true); assertTrue(dotMobi.preHandle(request, response, null)); assertEquals(0, response.getCookies().length); assertNull(response.getRedirectedUrl()); }
@Test public void urlPathRootPathTabletDeviceNoPreferenceRequestTabletSiteWithSlash() throws Exception { SiteSwitcherHandlerInterceptor urlPath = SiteSwitcherHandlerInterceptor.urlPath("/mob", "/tab", "/app"); device.setDeviceType(DeviceType.TABLET); request.setRequestURI("/app/tab"); assertTrue(urlPath.preHandle(request, response, null)); assertEquals(0, response.getCookies().length); assertNull(response.getRedirectedUrl()); }
@Test public void urlPathRootPathMobileDeviceNoPreferenceQueryString() throws Exception { SiteSwitcherHandlerInterceptor urlPath = SiteSwitcherHandlerInterceptor.urlPath("/mob", "/app"); device.setDeviceType(DeviceType.MOBILE); request.setQueryString("x=123"); assertFalse(urlPath.preHandle(request, response, null)); assertEquals(0, response.getCookies().length); assertEquals("http://" + request.getServerName() + "/app/mob?x=123", response.getRedirectedUrl()); }