@Test public void header() { this.builder.header("foo", "bar", "baz"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); List<String> headers = Collections.list(request.getHeaders("foo")); assertEquals(2, headers.size()); assertEquals("bar", headers.get(0)); assertEquals("baz", headers.get(1)); }
@Test // SPR-11308 public void contentTypeViaMultipleHeaderValues() { this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); assertEquals("text/html", request.getContentType()); }
@Test // SPR-11308 public void contentTypeViaHeader() { this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); String contentType = request.getContentType(); assertEquals("text/html", contentType); }
@Test public void stringWithMatcherAndCorrectResponseHeaderValue() throws Exception { this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, minuteAgo)) .andExpect(header().string(LAST_MODIFIED, equalTo(now))); }
private void assertIncorrectResponseHeader(ResultMatcher matcher, String expected) throws Exception { try { this.mockMvc.perform(get("/persons/1") .header(IF_MODIFIED_SINCE, minuteAgo)) .andExpect(matcher); fail(ERROR_MESSAGE); } catch (AssertionError err) { if (ERROR_MESSAGE.equals(err.getMessage())) { throw err; } // SPR-10659: ensure header name is in the message // Unfortunately, we can't control formatting from JUnit or Hamcrest. assertMessageContains(err, "Response header '" + LAST_MODIFIED + "'"); assertMessageContains(err, expected); assertMessageContains(err, this.now); } }
@Test public void longValueWithMissingResponseHeader() throws Exception { try { this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, now)) .andExpect(status().isNotModified()) .andExpect(header().longValue("X-Custom-Header", 99L)); fail(ERROR_MESSAGE); } catch (AssertionError err) { if (ERROR_MESSAGE.equals(err.getMessage())) { throw err; } assertEquals("Response does not contain header 'X-Custom-Header'", err.getMessage()); } }
@Test public void stringWithCorrectResponseHeaderValue() throws Exception { this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, minuteAgo)) .andExpect(header().string(LAST_MODIFIED, now)); }
@Test public void dateValueWithCorrectResponseHeaderValue() throws Exception { this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, minuteAgo)) .andExpect(header().dateValue(LAST_MODIFIED, this.currentTime)); }
@Test public void stringWithMatcherAndMissingResponseHeader() throws Exception { this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, now)) .andExpect(status().isNotModified()) .andExpect(header().string("X-Custom-Header", nullValue())); }
@Test public void mergeHeader() throws Exception { String headerName = "PARENT"; String headerValue = "VALUE"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/").header(headerName, headerValue)) .build(); assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getHeader(headerName), equalTo(headerValue)); }
@Test public void stringWithMissingResponseHeader() throws Exception { this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, now)) .andExpect(status().isNotModified()) .andExpect(header().stringValues("X-Custom-Header")); }
@Test public void getWhenAcceptHeaderIsApplicationXhtmlXmlThenRespondsWith302() throws Exception { this.spring.register(HttpBasicAndFormLoginEntryPointsConfig.class).autowire(); this.mvc.perform(get("/") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XHTML_XML)) .andExpect(status().isFound()); }
@Test public void getWhenAcceptHeaderIsImageJpgThenRespondsWith302() throws Exception { this.spring.register(HttpBasicAndFormLoginEntryPointsConfig.class).autowire(); this.mvc.perform(get("/") .header(HttpHeaders.ACCEPT, MediaType.IMAGE_JPEG)) .andExpect(status().isFound()); }
@Test public void getWhenAcceptIsChromeThenRespondsWith302() throws Exception { this.spring.register(DefaultSecurityConfig.class).autowire(); this.mvc.perform(get("/") .header(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")) .andExpect(status().isFound()); }
@Test public void getWhenDeclaringHttpBasicBeforeFormLoginThenRespondsWith401() throws Exception { this.spring.register(BasicAuthenticationEntryPointBeforeFormLoginConfig.class).autowire(); this.mvc.perform(get("/") .header(HttpHeaders.ACCEPT, "bogus/type")) .andExpect(status().isUnauthorized()); }
@Test public void getWhenAcceptHeaderIsImagePngThenRespondsWith302() throws Exception { this.spring.register(HttpBasicAndFormLoginEntryPointsConfig.class).autowire(); this.mvc.perform(get("/") .header(HttpHeaders.ACCEPT, MediaType.IMAGE_PNG)) .andExpect(status().isFound()); }
@Test public void getWhenAcceptHeaderIsApplicationAtomXmlThenRespondsWith401() throws Exception { this.spring.register(HttpBasicAndFormLoginEntryPointsConfig.class).autowire(); this.mvc.perform(get("/") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_ATOM_XML)) .andExpect(status().isUnauthorized()); }
@Test public void getWhenAcceptHeaderIsApplicationFormUrlEncodedThenRespondsWith401() throws Exception { this.spring.register(HttpBasicAndFormLoginEntryPointsConfig.class).autowire(); this.mvc.perform(get("/") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(status().isUnauthorized()); }
@Test public void requestTokenWhenUsingPasswordGrantTypeThenOk() throws Exception { this.mvc.perform(post("/oauth/token") .param("grant_type", "password") .param("username", "subject") .param("password", "password") .header("Authorization", "Basic cmVhZGVyOnNlY3JldA==")) .andExpect(status().isOk()); }
@Test public void getWhenBookmarkedRequestIsRequestedWithAndroidThenPostAuthenticationRemembers() throws Exception { this.spring.register(RequestCacheDefaultsConfig.class, DefaultSecurityConfig.class).autowire(); MockHttpSession session = (MockHttpSession) this.mvc.perform(get("/messages") .header("X-Requested-With", "com.android")) .andExpect(redirectedUrl("http://localhost/login")) .andReturn().getRequest().getSession(); this.mvc.perform(formLogin(session)) .andExpect(redirectedUrl("http://localhost/messages")); }