Search in sources :

Example 11 with ServerHttpRequest

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

the class WebUtilsTests method checkSameOrigin.

private boolean checkSameOrigin(String serverName, int port, String originHeader) {
    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.isSameOrigin(request);
}
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)

Example 12 with ServerHttpRequest

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

the class RequestPartServletServerHttpRequestTests method getContentType.

@Test
public void getContentType() throws Exception {
    MultipartFile part = new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8"));
    this.mockRequest.addFile(part);
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
    HttpHeaders headers = request.getHeaders();
    assertNotNull(headers);
    assertEquals(MediaType.APPLICATION_JSON, headers.getContentType());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) HttpHeaders(org.springframework.http.HttpHeaders) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 13 with ServerHttpRequest

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

the class RequestPartServletServerHttpRequestTests method getBody.

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

Example 14 with ServerHttpRequest

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

the class RequestPartServletServerHttpRequestTests method getBodyViaRequestParameterWithRequestEncoding.

@Test
public void getBodyViaRequestParameterWithRequestEncoding() throws Exception {
    MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {

        @Override
        public HttpHeaders getMultipartHeaders(String paramOrFileName) {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return headers;
        }
    };
    byte[] bytes = { (byte) 0xC4 };
    mockRequest.setParameter("part", new String(bytes, StandardCharsets.ISO_8859_1));
    mockRequest.setCharacterEncoding("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) Test(org.junit.Test)

Example 15 with ServerHttpRequest

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

the class WebSocketHttpRequestHandler method handleRequest.

@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
    ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    ServerHttpResponse response = new ServletServerHttpResponse(servletResponse);
    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, this.wsHandler);
    HandshakeFailureException failure = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug(servletRequest.getMethod() + " " + servletRequest.getRequestURI());
        }
        Map<String, Object> attributes = new HashMap<>();
        if (!chain.applyBeforeHandshake(request, response, attributes)) {
            return;
        }
        this.handshakeHandler.doHandshake(request, response, this.wsHandler, attributes);
        chain.applyAfterHandshake(request, response, null);
        response.close();
    } catch (HandshakeFailureException ex) {
        failure = ex;
    } catch (Throwable ex) {
        failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
    } finally {
        if (failure != null) {
            chain.applyAfterHandshake(request, response, failure);
            throw failure;
        }
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

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