@Test public void acceptHeader() { this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); List<String> accept = Collections.list(request.getHeaders("Accept")); List<MediaType> result = MediaType.parseMediaTypes(accept.get(0)); assertEquals(1, accept.size()); assertEquals("text/html", result.get(0).toString()); assertEquals("application/xml", result.get(1).toString()); }
@Test void getPerson99() throws Exception { this.mockMvc.perform(get("/person/99").accept(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.name", is("Wally"))); }
@Test void getPerson42() throws Exception { this.mockMvc.perform(get("/person/42").accept(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.name", is("Dilbert"))); }
@Test public void testContentStringMatcher() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().string(containsString("world"))); }
@Test public void callableInterceptor() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/callable").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(Collections.singletonMap("key", "value"))) .andReturn(); Mockito.verify(this.callableInterceptor).beforeConcurrentHandling(any(), any()); Mockito.verify(this.callableInterceptor).preProcess(any(), any()); Mockito.verify(this.callableInterceptor).postProcess(any(), any(), any()); Mockito.verifyNoMoreInteractions(this.callableInterceptor); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().string("{\"key\":\"value\"}")); Mockito.verify(this.callableInterceptor).afterCompletion(any(), any()); Mockito.verifyNoMoreInteractions(this.callableInterceptor); }
@Test public void testContentAsBytes() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1"))); this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8"))); }
@Test void springMvcTest(WebApplicationContext wac) throws Exception { webAppContextSetup(wac).build() .perform(get("/person/42").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.name", is("Dilbert"))); }
@Test public void testContentAsString() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().string("Hello world!")); this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01")); // Hamcrest matchers... this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)).andExpect(content().string(equalTo("Hello world!"))); this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string(equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"))); }
@Test public void testCharacterEncoding() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().encoding("ISO-8859-1")) .andExpect(content().string(containsString("world"))); this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().encoding("UTF-8")) .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8"))); }
@Test public void person() throws Exception { this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void person() throws Exception { this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Before public void setup() { this.mockMvc = standaloneSetup(new MusicController()) .defaultRequest(get("/").accept(MediaType.APPLICATION_JSON)) .alwaysExpect(status().isOk()) .alwaysExpect(content().contentType("application/json;charset=UTF-8")) .build(); }
@Before public void setup() { this.mockMvc = standaloneSetup(new MusicController()) .defaultRequest(get("/").accept(MediaType.APPLICATION_XML, MediaType.parseMediaType("application/xml;charset=UTF-8"))) .alwaysExpect(status().isOk()) .alwaysExpect(content().contentType(MediaType.parseMediaType("application/xml;charset=UTF-8"))) .build(); }
@Before public void setup() throws Exception { this.mockMvc = standaloneSetup(new MusicController()) .defaultRequest(get("/").accept(MediaType.APPLICATION_XML, MediaType.parseMediaType("application/xml;charset=UTF-8"))) .alwaysExpect(status().isOk()) .alwaysExpect(content().contentType(MediaType.parseMediaType("application/xml;charset=UTF-8"))) .build(); }
@Test public void queryParameter() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.name").value("George")); }
@Test public void json() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.name").value("Lee")); }
@Test public void testFeedWithLinefeedChars() throws Exception { // Map<String, String> namespace = Collections.singletonMap("ns", ""); standaloneSetup(new BlogFeedController()).build() .perform(get("/blog.atom").accept(MediaType.APPLICATION_ATOM_XML)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_ATOM_XML)) .andExpect(xpath("//feed/title").string("Test Feed")) .andExpect(xpath("//feed/icon").string("http://www.example.com/favicon.ico")); }
.andExpect(forwardedUrl("person/show")); mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.person.name").value("Corea")); mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_XML))
@Test public void testContentType() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().contentType(MediaType.valueOf("text/plain;charset=ISO-8859-1"))) .andExpect(content().contentType("text/plain;charset=ISO-8859-1")) .andExpect(content().contentTypeCompatibleWith("text/plain")) .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN)); this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().contentType(MediaType.valueOf("text/plain;charset=UTF-8"))) .andExpect(content().contentType("text/plain;charset=UTF-8")) .andExpect(content().contentTypeCompatibleWith("text/plain")) .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN)); }
@Test // SPR-16067, SPR-16695 public void filterWrapsRequestResponseAndPerformsAsyncDispatch() throws Exception { MockMvc mockMvc = standaloneSetup(new PersonController()) .addFilters(new WrappingRequestResponseFilter(), new ShallowEtagHeaderFilter()) .build(); MvcResult mvcResult = mockMvc.perform(get("/persons/1").accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(new Person("Lukas"))) .andReturn(); mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(header().longValue("Content-Length", 53)) .andExpect(header().string("ETag", "\"0e37becb4f0c90709cb2e1efcc61eaa00\"")) .andExpect(content().string("{\"name\":\"Lukas\",\"someDouble\":0.0,\"someBoolean\":false}")); }