Search in sources :

Example 6 with ServerHttpRequest

use of org.springframework.http.server.ServerHttpRequest in project spring-framework by spring-projects.

the class RequestPartServletServerHttpRequestTests method getURI.

@Test
public void getURI() throws Exception {
    this.mockRequest.addFile(new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8")));
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
    URI uri = new URI("http://example.com/path?query");
    this.mockRequest.setServerName(uri.getHost());
    this.mockRequest.setServerPort(uri.getPort());
    this.mockRequest.setRequestURI(uri.getPath());
    this.mockRequest.setQueryString(uri.getQuery());
    assertEquals(uri, request.getURI());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 7 with ServerHttpRequest

use of org.springframework.http.server.ServerHttpRequest in project spring-framework by spring-projects.

the class RequestPartServletServerHttpRequestTests method getMethod.

@Test
public void getMethod() throws Exception {
    this.mockRequest.addFile(new MockMultipartFile("part", "", "", "content".getBytes("UTF-8")));
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
    this.mockRequest.setMethod("POST");
    assertEquals(HttpMethod.POST, request.getMethod());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 8 with ServerHttpRequest

use of org.springframework.http.server.ServerHttpRequest in project spring-framework by spring-projects.

the class RequestPartServletServerHttpRequestTests method getBodyWithWrappedRequest.

// SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception {
    byte[] bytes = "content".getBytes("UTF-8");
    MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
    this.mockRequest.addFile(part);
    HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
    byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
    assertArrayEquals(bytes, result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 9 with ServerHttpRequest

use of org.springframework.http.server.ServerHttpRequest in project spring-framework by spring-projects.

the class RequestPartServletServerHttpRequestTests method getBodyViaRequestParameter.

// SPR-13096
@Test
public void getBodyViaRequestParameter() throws Exception {
    MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {

        @Override
        public HttpHeaders getMultipartHeaders(String paramOrFileName) {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(new MediaType("application", "octet-stream", StandardCharsets.ISO_8859_1));
            return headers;
        }
    };
    byte[] bytes = { (byte) 0xC4 };
    mockRequest.setParameter("part", new String(bytes, StandardCharsets.ISO_8859_1));
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
    byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
    assertArrayEquals(bytes, result);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 10 with ServerHttpRequest

use of org.springframework.http.server.ServerHttpRequest in project spring-framework by spring-projects.

the class WebUtilsTests method checkValidOrigin.

private boolean checkValidOrigin(String serverName, int port, String originHeader, List<String> allowed) {
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    servletRequest.setServerName(serverName);
    if (port != -1) {
        servletRequest.setServerPort(port);
    }
    request.getHeaders().set(HttpHeaders.ORIGIN, originHeader);
    return WebUtils.isValidOrigin(request, allowed);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest)

Aggregations

ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)19 Test (org.junit.Test)13 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)10 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)6 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)4 HttpHeaders (org.springframework.http.HttpHeaders)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 HttpConnection (org.springframework.boot.devtools.tunnel.server.HttpTunnelServer.HttpConnection)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 URI (java.net.URI)1 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 MediaType (org.springframework.http.MediaType)1 ServerHttpAsyncRequestControl (org.springframework.http.server.ServerHttpAsyncRequestControl)1