private MockHttpServletResponse getResponse(RequestBuilder requestBuilder) throws IOException { ResultActions resultActions; try { resultActions = this.mockMvc.perform(requestBuilder); } catch (Exception ex) { throw new IOException(ex); } return resultActions.andReturn().getResponse(); }
private ClientHttpResponse getClientHttpResponse( HttpMethod httpMethod, URI uri, HttpHeaders requestHeaders, byte[] requestBody) { try { MockHttpServletResponse servletResponse = this.mockMvc .perform(request(httpMethod, uri).content(requestBody).headers(requestHeaders)) .andReturn() .getResponse(); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); byte[] body = servletResponse.getContentAsByteArray(); MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse)); return clientResponse; } catch (Exception ex) { byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8); return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR); } }
@Test public void mergeCookie() throws Exception { String cookieName = "PARENT"; String cookieValue = "VALUE"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/").cookie(new Cookie(cookieName, cookieValue))) .build(); Cookie[] cookies = mockMvc.perform(requestBuilder).andReturn().getRequest().getCookies(); assertThat(cookies, notNullValue()); assertThat(cookies.length, equalTo(1)); Cookie cookie = cookies[0]; assertThat(cookie.getName(), equalTo(cookieName)); assertThat(cookie.getValue(), equalTo(cookieValue)); }
@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 mergeSession() throws Exception { String attrName = "PARENT"; String attrValue = "VALUE"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/").sessionAttr(attrName, attrValue)) .build(); assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getSession().getAttribute(attrName), equalTo(attrValue)); }
@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 // SPR-14584 public void mergeDoesNotCorruptPathInfoOnParent() throws Exception { String pathInfo = "/foo/bar"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/")) .build(); assertThat(mockMvc.perform(get(pathInfo)).andReturn().getRequest().getPathInfo(), equalTo(pathInfo)); mockMvc.perform(requestBuilder); assertThat(mockMvc.perform(get(pathInfo)).andReturn().getRequest().getPathInfo(), equalTo(pathInfo)); }
@Test public void mergeSessionNotInitialized() throws Exception { MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/")) .build(); assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getSession(false), 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 mergeRequestAttribute() throws Exception { String attrName = "PARENT"; String attrValue = "VALUE"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/").requestAttr(attrName, attrValue)) .build(); assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getAttribute(attrName), equalTo(attrValue)); }
@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 noHttpSession() throws Exception { MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new TestController()) .apply(sharedHttpSession()) .build(); String url = "/no-session"; MvcResult result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn(); HttpSession session = result.getRequest().getSession(false); assertNull(session); result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn(); session = result.getRequest().getSession(false); assertNull(session); url = "/session"; result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn(); session = result.getRequest().getSession(false); assertNotNull(session); assertEquals(1, session.getAttribute("counter")); }
@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 // 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-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 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 // 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}")); }