Java Code Examples for org.springframework.test.web.servlet.MvcResult#getRequest()
The following examples show how to use
org.springframework.test.web.servlet.MvcResult#getRequest() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringMockRamlMessageTest.java From raml-tester with Apache License 2.0 | 6 votes |
@Test public void multipart() throws Exception { final MvcResult result = mockMvc.perform(fileUpload("http://test.com/path?param=val") .file("file", new byte[]{}) .characterEncoding("utf-8") .content("contentä") .param("param2", "val2") .header("head", "hval")).andReturn(); final SpringMockRamlRequest ramlRequest = new SpringMockRamlRequest(result.getRequest()); assertEquals("contentä", new String(ramlRequest.getContent(), "utf-8")); assertEquals("multipart/form-data", ramlRequest.getContentType()); final Values formValues = new Values().addValue("param", "val").addValue("param2", "val2").addValue("file", new FileValue()); assertEquals(formValues, ramlRequest.getFormValues()); final Values headerValues = new Values().addValue("head", "hval").addValue("Content-Type", "multipart/form-data;charset=utf-8"); assertEquals(headerValues, ramlRequest.getHeaderValues()); assertEquals("POST", ramlRequest.getMethod()); assertEquals(new Values().addValue("param", "val"), ramlRequest.getQueryValues()); assertEquals("http://test.com/path", ramlRequest.getRequestUrl(null, false)); assertEquals("http://x.y/path", ramlRequest.getRequestUrl("http://x.y", false)); }
Example 2
Source File: PrintingResultHandler.java From spring-analysis-note with MIT License | 5 votes |
protected void printAsyncResult(MvcResult result) throws Exception { HttpServletRequest request = result.getRequest(); this.printer.printValue("Async started", request.isAsyncStarted()); Object asyncResult = null; try { asyncResult = result.getAsyncResult(0); } catch (IllegalStateException ex) { // Not set } this.printer.printValue("Async result", asyncResult); }
Example 3
Source File: PrintingResultHandler.java From java-technology-stack with MIT License | 5 votes |
protected void printAsyncResult(MvcResult result) throws Exception { HttpServletRequest request = result.getRequest(); this.printer.printValue("Async started", request.isAsyncStarted()); Object asyncResult = null; try { asyncResult = result.getAsyncResult(0); } catch (IllegalStateException ex) { // Not set } this.printer.printValue("Async result", asyncResult); }
Example 4
Source File: MvcTest.java From ManagementSystem with Apache License 2.0 | 5 votes |
@Test public void testPage() throws Exception { //模拟请求拿到返回值 MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn","1")).andReturn(); //请求成功后,请求中会有pageInfo,我们可以取出pageInfo进行校验 MockHttpServletRequest request = result.getRequest(); PageInfo pi = (PageInfo) request.getAttribute("pageInfo"); System.out.println("当前页码:" + pi.getPageNum()); System.out.println("总页码:" + pi.getPages()); System.out.println("总记录数:" + pi.getTotal()); System.out.println("连续显示的页码:"); int[] nums = pi.getNavigatepageNums(); for (int i : nums){ System.out.print(" " + i); } //获取员工数据 List<Employee> list = pi.getList(); for (Employee employee : list){ System.out.println("ID" + employee.getEmpId()+"name" + employee.getEmpName()); } }
Example 5
Source File: RequestResultMatchers.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Assert whether asynchronous processing started, usually as a result of a * controller method returning {@link Callable} or {@link DeferredResult}. * <p>The test will await the completion of a {@code Callable} so that * {@link #asyncResult(Matcher)} can be used to assert the resulting value. * Neither a {@code Callable} nor a {@code DeferredResult} will complete * processing all the way since a {@link MockHttpServletRequest} does not * perform asynchronous dispatches. */ public ResultMatcher asyncStarted() { return new ResultMatcher() { @Override public void match(MvcResult result) { HttpServletRequest request = result.getRequest(); assertAsyncStarted(request); } }; }
Example 6
Source File: RequestResultMatchers.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Assert that asynchronous processing was not started. * @see #asyncStarted() */ public ResultMatcher asyncNotStarted() { return new ResultMatcher() { @Override public void match(MvcResult result) { HttpServletRequest request = result.getRequest(); assertEquals("Async started", false, request.isAsyncStarted()); } }; }
Example 7
Source File: RequestResultMatchers.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Assert the result from asynchronous processing with the given matcher. * <p>This method can be used when a controller method returns {@link Callable} * or {@link WebAsyncTask}. */ public <T> ResultMatcher asyncResult(final Matcher<T> matcher) { return new ResultMatcher() { @Override @SuppressWarnings("unchecked") public void match(MvcResult result) { HttpServletRequest request = result.getRequest(); assertAsyncStarted(request); assertThat("Async result", (T) result.getAsyncResult(), matcher); } }; }
Example 8
Source File: RequestResultMatchers.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Assert the result from asynchronous processing. * <p>This method can be used when a controller method returns {@link Callable} * or {@link WebAsyncTask}. The value matched is the value returned from the * {@code Callable} or the exception raised. */ public <T> ResultMatcher asyncResult(final Object expectedResult) { return new ResultMatcher() { @Override public void match(MvcResult result) { HttpServletRequest request = result.getRequest(); assertAsyncStarted(request); assertEquals("Async result", expectedResult, result.getAsyncResult()); } }; }
Example 9
Source File: PrintingResultHandler.java From spring4-understanding with Apache License 2.0 | 5 votes |
protected void printAsyncResult(MvcResult result) throws Exception { HttpServletRequest request = result.getRequest(); this.printer.printValue("Async started", request.isAsyncStarted()); Object asyncResult = null; try { asyncResult = result.getAsyncResult(0); } catch (IllegalStateException ex) { // Not set } this.printer.printValue("Async result", asyncResult); }
Example 10
Source File: SpringMockRamlMessageTest.java From raml-tester with Apache License 2.0 | 5 votes |
@Test public void simple() throws Exception { final MvcResult result = mockMvc.perform(get("http://test.com/path?param=val") .characterEncoding("utf-8") .content("contentä") .contentType(MediaType.TEXT_PLAIN) .param("param2", "val2") .header("head", "hval")).andReturn(); final SpringMockRamlRequest ramlRequest = new SpringMockRamlRequest(result.getRequest()); assertEquals("contentä", new String(ramlRequest.getContent(), "utf-8")); assertEquals("text/plain", ramlRequest.getContentType()); final Values formValues = new Values().addValue("param", "val").addValue("param2", "val2"); assertEquals(formValues, ramlRequest.getFormValues()); final Values headerValues = new Values().addValue("head", "hval").addValue("Content-Type", "text/plain;charset=utf-8"); assertEquals(headerValues, ramlRequest.getHeaderValues()); assertEquals("GET", ramlRequest.getMethod()); assertEquals(new Values().addValue("param", "val"), ramlRequest.getQueryValues()); assertEquals("http://test.com/path", ramlRequest.getRequestUrl(null, false)); assertEquals("http://x.y/path", ramlRequest.getRequestUrl("http://x.y", false)); final SpringMockRamlResponse ramlResponse = new SpringMockRamlResponse(result.getResponse()); assertEquals("responsö", new String(ramlResponse.getContent(), "iso-8859-1")); assertEquals("text/dummy;charset=ISO-8859-1", ramlResponse.getContentType()); final Values resHeaderValues = new Values() .addValue("head", "resValue") .addValue("Content-Length", Integer.toString("responsö".length())) .addValue("Content-Type", "text/dummy;charset=ISO-8859-1"); assertEquals(resHeaderValues, ramlResponse.getHeaderValues()); assertEquals(202, ramlResponse.getStatus()); }
Example 11
Source File: SpringMockRamlMessageTest.java From raml-tester with Apache License 2.0 | 5 votes |
@Test public void pathInfoNull() throws Exception { final MvcResult result = mockMvc.perform(get("http://test.com/servlet").servletPath("/servlet")).andReturn(); final SpringMockRamlRequest ramlRequest = new SpringMockRamlRequest(result.getRequest()); assertEquals("http://base", ramlRequest.getRequestUrl("http://base", false)); assertEquals("http://base/servlet", ramlRequest.getRequestUrl("http://base", true)); }
Example 12
Source File: MyResultHandler.java From springrestdoc with MIT License | 4 votes |
@Override public void handle(MvcResult result) throws Exception { MockHttpServletRequest request = result.getRequest(); MockHttpServletResponse response = result.getResponse(); logger.error("HTTP method: {}, status code: {}", request.getMethod(), response.getStatus()); }
Example 13
Source File: MockMvcRequestBuilders.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * Create a {@link RequestBuilder} for an async dispatch from the * {@link MvcResult} of the request that started async processing. * <p>Usage involves performing a request that starts async processing first: * <pre class="code"> * MvcResult mvcResult = this.mockMvc.perform(get("/1")) * .andExpect(request().asyncStarted()) * .andReturn(); * </pre> * <p>And then performing the async dispatch re-using the {@code MvcResult}: * <pre class="code"> * this.mockMvc.perform(asyncDispatch(mvcResult)) * .andExpect(status().isOk()) * .andExpect(content().contentType(MediaType.APPLICATION_JSON)) * .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); * </pre> * @param mvcResult the result from the request that started async processing */ public static RequestBuilder asyncDispatch(final MvcResult mvcResult) { // There must be an async result before dispatching mvcResult.getAsyncResult(); return new RequestBuilder() { @Override public MockHttpServletRequest buildRequest(ServletContext servletContext) { MockHttpServletRequest request = mvcResult.getRequest(); request.setDispatcherType(DispatcherType.ASYNC); request.setAsyncStarted(false); return request; } }; }
Example 14
Source File: MockMvcRequestBuilders.java From spring-analysis-note with MIT License | 3 votes |
/** * Create a {@link RequestBuilder} for an async dispatch from the * {@link MvcResult} of the request that started async processing. * <p>Usage involves performing a request that starts async processing first: * <pre class="code"> * MvcResult mvcResult = this.mockMvc.perform(get("/1")) * .andExpect(request().asyncStarted()) * .andReturn(); * </pre> * <p>And then performing the async dispatch re-using the {@code MvcResult}: * <pre class="code"> * this.mockMvc.perform(asyncDispatch(mvcResult)) * .andExpect(status().isOk()) * .andExpect(content().contentType(MediaType.APPLICATION_JSON)) * .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); * </pre> * @param mvcResult the result from the request that started async processing */ public static RequestBuilder asyncDispatch(final MvcResult mvcResult) { // There must be an async result before dispatching mvcResult.getAsyncResult(); return servletContext -> { MockHttpServletRequest request = mvcResult.getRequest(); request.setDispatcherType(DispatcherType.ASYNC); request.setAsyncStarted(false); return request; }; }
Example 15
Source File: MockMvcRequestBuilders.java From java-technology-stack with MIT License | 3 votes |
/** * Create a {@link RequestBuilder} for an async dispatch from the * {@link MvcResult} of the request that started async processing. * <p>Usage involves performing a request that starts async processing first: * <pre class="code"> * MvcResult mvcResult = this.mockMvc.perform(get("/1")) * .andExpect(request().asyncStarted()) * .andReturn(); * </pre> * <p>And then performing the async dispatch re-using the {@code MvcResult}: * <pre class="code"> * this.mockMvc.perform(asyncDispatch(mvcResult)) * .andExpect(status().isOk()) * .andExpect(content().contentType(MediaType.APPLICATION_JSON)) * .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); * </pre> * @param mvcResult the result from the request that started async processing */ public static RequestBuilder asyncDispatch(final MvcResult mvcResult) { // There must be an async result before dispatching mvcResult.getAsyncResult(); return servletContext -> { MockHttpServletRequest request = mvcResult.getRequest(); request.setDispatcherType(DispatcherType.ASYNC); request.setAsyncStarted(false); return request; }; }