@Test public void testRequestAttributeMatcher() throws Exception { String producibleMediaTypes = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE; this.mockMvc.perform(get("/1")) .andExpect(request().attribute(producibleMediaTypes, hasItem(MediaType.APPLICATION_JSON))) .andExpect(request().attribute(producibleMediaTypes, not(hasItem(MediaType.APPLICATION_XML)))); }
@Test public void testRequestAttributeEqualTo() throws Exception { this.mockMvc.perform(get("/main/1").servletPath("/main")) .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/{id}")) .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/1")) .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, equalTo("/{id}"))) .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, equalTo("/1"))); }
@Test public void testSessionAttributeMatcher() throws Exception { this.mockMvc.perform(get("/")) .andExpect(request().sessionAttribute("locale", notNullValue())); }
@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 sessionAttributesAreClearedBetweenInvocations() throws Exception { this.mvc.perform(get("/")) .andExpect(content().string(HELLO)) .andExpect(request().sessionAttribute(FOO, nullValue())); this.mvc.perform(get("/").sessionAttr(FOO, BAR)) .andExpect(content().string(HELLO)) .andExpect(request().sessionAttribute(FOO, BAR)); this.mvc.perform(get("/")) .andExpect(content().string(HELLO)) .andExpect(request().sessionAttribute(FOO, nullValue())); }
private MvcResult getMvcResult(ResultActions perform, boolean isAsyncRequest) throws Exception { MvcResult mvcResult; if (isAsyncRequest) { MvcResult startedAsyncRequestProcessing = perform.andExpect(MockMvcResultMatchers.request().asyncStarted()).andReturn(); startedAsyncRequestProcessing.getAsyncResult(config.getAsyncConfig().timeoutInMs()); mvcResult = mockMvc.perform(asyncDispatch(startedAsyncRequestProcessing)).andReturn(); } else { mvcResult = perform.andReturn(); } return mvcResult; }
@Test // SPR-12735 public void printAsyncResult() throws Exception { StringWriter writer = new StringWriter(); MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true")) .andDo(print(writer)) .andExpect(request().asyncStarted()) .andReturn(); assertTrue(writer.toString().contains("Async started = true")); writer = new StringWriter(); this.asyncController.onMessage("Joe"); this.mockMvc.perform(asyncDispatch(mvcResult)) .andDo(print(writer)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); assertTrue(writer.toString().contains("Async started = false")); }
@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 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")); }
@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-16869 public void sseWithFlux() throws Exception { MockMvc mockMvc = MockMvcBuilders.standaloneSetup(ReactiveController.class).build(); MvcResult mvcResult = mockMvc.perform(get("/spr16869")) .andExpect(request().asyncStarted()) .andExpect(status().isOk()) .andReturn(); mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(content().string("data:event0\n\ndata:event1\n\ndata:event2\n\n")); }
@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}")); }
@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 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 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 failedLoginWhenConfiguredWithCustomAuthenticationFailureThenForwardsAccordingly() throws Exception { this.spring.configLocations(this.xml("WithAuthenticationFailureForwardUrl")).autowire(); this.mvc.perform(post("/login") .param("username", "bob") .param("password", "invalidpassword")) .andExpect(status().isOk()) .andExpect(forwardedUrl("/failure_forward_url")) .andExpect(request().attribute(WebAttributes.AUTHENTICATION_EXCEPTION, not(nullValue()))); }