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); } }
/** * Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP * method. * @param httpMethod the HTTP method (GET, POST, etc) * @param uri the URL * @return the builder for the request */ public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, URI uri) { return MockMvcRequestBuilders.request(httpMethod, uri); }
request = MockMvcRequestBuilders.request(method, uri, pathParams); } else if (method != POST) { throw new IllegalArgumentException("Currently multi-part file data uploading only works for " + POST);
@Test public void invalidWhenCsrfElementEnabledThenForbidden() throws Exception { this.spring.configLocations( this.xml("CsrfEnabled") ).autowire(); this.mvc.perform(request("INVALID", new URI("/csrf"))) .andExpect(status().isForbidden()) .andExpect(csrfCreated()); }
@Test public void invalidWhenDefaultConfigurationThenForbiddenSinceCsrfIsEnabled() throws Exception { this.spring.configLocations( this.xml("AutoConfig") ).autowire(); this.mvc.perform(request("INVALID", new URI("/csrf"))) .andExpect(status().isForbidden()) .andExpect(csrfCreated()); }
@Override public ClientHttpResponse executeInternal() throws IOException { try { MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString()); requestBuilder.content(getBodyAsBytes()); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); if (status.value() >= 400) { requestBuilder = request(HttpMethod.GET, "/error") .requestAttr(RequestDispatcher.ERROR_STATUS_CODE, status.value())
/** * Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP * method. The url template will be captured and made available for documentation. * @param httpMethod the HTTP method * @param urlTemplate a URL template; the resulting URL will be encoded * @param urlVariables zero or more URL variables * @return the builder for the request */ public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) { return MockMvcRequestBuilders.request(httpMethod, urlTemplate, urlVariables) .requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate); }
@Test public void traceWhenCsrfElementEnabledThenOk() throws Exception { this.spring.configLocations( this.xml("shared-controllers"), this.xml("CsrfEnabled") ).autowire(); MockMvc traceEnabled = MockMvcBuilders .webAppContextSetup((WebApplicationContext) this.spring.getContext()) .apply(springSecurity()) .addDispatcherServletCustomizer(dispatcherServlet -> dispatcherServlet.setDispatchTraceRequest(true)) .build(); traceEnabled.perform(request(HttpMethod.TRACE, "/csrf-in-header")) .andExpect(csrfInHeader()); }
@Test public void traceWhenDefaultConfigurationThenCsrfIsEnabled() throws Exception { this.spring.configLocations( this.xml("shared-controllers"), this.xml("AutoConfig") ).autowire(); MockMvc traceEnabled = MockMvcBuilders .webAppContextSetup((WebApplicationContext) this.spring.getContext()) .apply(springSecurity()) .addDispatcherServletCustomizer(dispatcherServlet -> dispatcherServlet.setDispatchTraceRequest(true)) .build(); traceEnabled.perform(request(HttpMethod.TRACE, "/csrf-in-header")) .andExpect(csrfInHeader()); }
/** * Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP * method. * @param httpMethod the HTTP method (GET, POST, etc) * @param uri the URL * @return the builder for the request */ public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, URI uri) { return MockMvcRequestBuilders.request(httpMethod, uri); }
/** * Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP * method. The url template will be captured and made available for documentation. * @param httpMethod the HTTP method * @param urlTemplate a URL template; the resulting URL will be encoded * @param urlVariables zero or more URL variables * @return the builder for the request */ public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) { return MockMvcRequestBuilders.request(httpMethod, urlTemplate, urlVariables) .requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate); }
@Override protected void callInternal(final HttpMethod method, String resourceUri, String content, String contentType) { if (resourceUri.endsWith("/")) { resourceUri = resourceUri.substring(0, resourceUri.length() - 1); } try { MockMvc mvc = MockMvcBuilders.webAppContextSetup(wac).build(); MockHttpServletRequestBuilder request = MockMvcRequestBuilders .request(method, new URI(resourceUri)).content(content) .contentType(contentType); request.requestAttr(RepositoryProvider.KEY, repoProvider); setLastResponse(mvc.perform(request).andReturn()); } catch (Exception e) { Throwables.propagate(e); } }
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); } }
@Override public ClientHttpResponse executeInternal() throws IOException { try { MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri); requestBuilder.content(getBodyAsBytes()); requestBuilder.headers(getHeaders()); MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn(); MockHttpServletResponse servletResponse = mvcResult.getResponse(); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); byte[] body = servletResponse.getContentAsByteArray(); HttpHeaders headers = getResponseHeaders(servletResponse); MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); clientResponse.getHeaders().putAll(headers); return clientResponse; } catch (Exception ex) { byte[] body = ex.toString().getBytes(UTF8_CHARSET); return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR); } } };
/** * Issue a request with the given {@link HttpMethod} to the provided resource URI. * * @param method the http method to use * @param resourceUri the uri to issue the request to */ @Override protected void callInternal(HttpMethod method, String resourceUri) { try { MockMvc mvc = MockMvcBuilders.webAppContextSetup(wac).build(); MockHttpServletRequestBuilder request = MockMvcRequestBuilders.request(method, new URI(resourceUri)); request.requestAttr(RepositoryProvider.KEY, repoProvider); if (HttpMethod.PUT.equals(method) || HttpMethod.POST.equals(method)) { // PUT and POST requests should have an entity. // Since this method has no content argument, fill the entity with an empty JSON // object. // This method is hit for setting up repositories for many tests, making PUT calls // to trigger the "init" // command. The INIT Web API command only accepts JSON and Web Form entities, so // we'll use JSON here. request.content("{}") .contentType(org.springframework.http.MediaType.APPLICATION_JSON); } setLastResponse(mvc.perform(request).andReturn()); } catch (Exception e) { Throwables.propagate(e); } }