@Test public void requestParameter() { this.builder.param("foo", "bar", "baz"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); Map<String, String[]> parameterMap = request.getParameterMap(); assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo")); }
@Test public void mergeParameter() throws Exception { String paramName = "PARENT"; String paramValue = "VALUE"; String paramValue2 = "VALUE2"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/").param(paramName, paramValue, paramValue2)) .build(); MockHttpServletRequest performedRequest = mockMvc.perform(requestBuilder).andReturn().getRequest(); assertThat(asList(performedRequest.getParameterValues(paramName)), contains(paramValue, paramValue2)); }
@Test public void filterWithExactMapping() throws Exception { standaloneSetup(new PersonController()) .addFilter(new RedirectFilter(), "/p", "/persons").build() .perform(post("/persons").param("name", "Andy")) .andExpect(redirectedUrl("/login")); }
@Test public void filterMappedBySuffix() throws Exception { standaloneSetup(new PersonController()) .addFilter(new RedirectFilter(), "*.html").build() .perform(post("/persons.html").param("name", "Andy")) .andExpect(redirectedUrl("/login")); }
@Test public void filtersProcessRequest() throws Exception { standaloneSetup(new PersonController()) .addFilters(new ContinueFilter(), new RedirectFilter()).build() .perform(post("/persons").param("name", "Andy")) .andExpect(redirectedUrl("/login")); }
@Test public void streaming() throws Exception { this.mockMvc.perform(get("/1").param("streaming", "true")) .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) // fetch async result similar to "asyncDispatch" builder .andExpect(status().isOk()) .andExpect(content().string("name=Joe")); }
@Test public void streamingSlow() throws Exception { this.mockMvc.perform(get("/1").param("streamingSlow", "true")) .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) .andExpect(status().isOk()) .andExpect(content().string("name=Joe&someBoolean=true")); }
@Test // SPR-13079 public void deferredResultWithDelayedError() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().is5xxServerError()) .andExpect(content().string("Delayed Error")); }
@Before public void setup() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setCookieDomain("domain"); localeResolver.setCookieHttpOnly(true); this.mockMvc = standaloneSetup(new SimpleController()) .addInterceptors(new LocaleChangeInterceptor()) .setLocaleResolver(localeResolver) .defaultRequest(get("/").param("locale", "en_US")) .alwaysExpect(status().isOk()) .build(); }
@Test public void requestParametersAreClearedBetweenInvocations() throws Exception { this.mvc.perform(get("/")) .andExpect(content().string(HELLO)); this.mvc.perform(get("/").param(ENIGMA, "")) .andExpect(content().string(ENIGMA)); this.mvc.perform(get("/")) .andExpect(content().string(HELLO)); }
@Test public void streamingJson() throws Exception { this.mockMvc.perform(get("/1").param("streamingJson", "true")) .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}")); }
@Test // SPR-12597 public void completableFutureWithImmediateValue() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFutureWithImmediateValue", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void deferredResult() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.asyncController.onMessage("Joe"); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void listenableFuture() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("listenableFuture", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.asyncController.onMessage("Joe"); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void saveSpecial() throws Exception { this.mockMvc.perform(post("/people").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void save() throws Exception { this.mockMvc.perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void deferredResultWithImmediateValue() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithImmediateValue", "true")) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(new Person("Joe"))) .andReturn(); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void callable() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("callable", "true")) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(new Person("Joe"))) .andReturn(); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void whenFiltersCompleteMvcProcessesRequest() throws Exception { standaloneSetup(new PersonController()) .addFilters(new ContinueFilter()).build() .perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/person/1")) .andExpect(model().size(1)) .andExpect(model().attributeExists("id")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void filterSkipped() throws Exception { standaloneSetup(new PersonController()) .addFilter(new RedirectFilter(), "/p", "/person").build() .perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/person/1")) .andExpect(model().size(1)) .andExpect(model().attributeExists("id")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }