Java Code Examples for org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder#requestAttr()
The following examples show how to use
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder#requestAttr() .
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: ServletErrorAttributesIT.java From errors-spring-boot-starter with Apache License 2.0 | 6 votes |
@Test @Parameters(method = "dataForDifferentErrorScenarios") public void getErrorAttributes_ShouldExtractAndHandlesErrorsProperly(Throwable exception, int statusCode, int expectedStatusCode, String expectedErrorCodes, Argument argument) throws Exception { MockHttpServletRequestBuilder request = get("/error") .requestAttr("javax.servlet.error.status_code", statusCode) .requestAttr("javax.servlet.error.request_uri", "/test"); if (exception != null) request.requestAttr("javax.servlet.error.exception", exception); ResultActions result = mvc.perform(request) .andExpect(status().is(expectedStatusCode)) .andExpect(jsonPath("$.errors[0].code").value(expectedErrorCodes)); if (argument != null) { String json = "$.errors[0].arguments." + argument.getName(); result.andExpect(jsonPath(json).value(argument.getValue())); } }
Example 2
Source File: StoreOperationControllerExceptionIntegrationTest.java From evernote-rest-webapp with Apache License 2.0 | 5 votes |
private void copyRequestAttributes(MockHttpServletRequestBuilder requestBuilder, MockHttpServletRequest request) { Enumeration<String> attributeNames = request.getAttributeNames(); while (attributeNames.hasMoreElements()) { String attrName = attributeNames.nextElement(); requestBuilder.requestAttr(attrName, request.getAttribute(attrName)); } }
Example 3
Source File: DapTestCommon.java From tds with BSD 3-Clause "New" or "Revised" License | 4 votes |
public HttpResponse execute(HttpRequestBase rq) throws IOException { URI uri = rq.getURI(); DapController controller = getController(uri); StandaloneMockMvcBuilder mvcbuilder = MockMvcBuilders.standaloneSetup(controller); mvcbuilder.setValidator(new TestServlet.NullValidator()); MockMvc mockMvc = mvcbuilder.build(); MockHttpServletRequestBuilder mockrb = MockMvcRequestBuilders.get(uri); // We need to use only the path part mockrb.servletPath(uri.getPath()); // Move any headers from rq to mockrb Header[] headers = rq.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Header h = headers[i]; mockrb.header(h.getName(), h.getValue()); } // Since the url has the query parameters, // they will automatically be parsed and added // to the rb parameters. // Finally set the resource dir mockrb.requestAttr("RESOURCEDIR", this.resourcepath); // Now invoke the servlet MvcResult result; try { result = mockMvc.perform(mockrb).andReturn(); } catch (Exception e) { throw new IOException(e); } // Collect the output MockHttpServletResponse res = result.getResponse(); byte[] byteresult = res.getContentAsByteArray(); // Convert to HttpResponse HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, res.getStatus(), ""); if (response == null) throw new IOException("HTTPMethod.executeMock: Response was null"); Collection<String> keys = res.getHeaderNames(); // Move headers to the response for (String key : keys) { List<String> values = res.getHeaders(key); for (String v : values) { response.addHeader(key, v); } } ByteArrayEntity entity = new ByteArrayEntity(byteresult); String sct = res.getContentType(); entity.setContentType(sct); response.setEntity(entity); return response; }