Search in sources :

Example 96 with ServletServerHttpRequest

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

the class RequestResponseBodyAdviceChainTests method setup.

@BeforeEach
public void setup() {
    this.body = "body";
    this.contentType = MediaType.TEXT_PLAIN;
    this.converterType = StringHttpMessageConverter.class;
    this.paramType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), 0);
    this.returnType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), -1);
    this.request = new ServletServerHttpRequest(new MockHttpServletRequest());
    this.response = new ServletServerHttpResponse(new MockHttpServletResponse());
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) MethodParameter(org.springframework.core.MethodParameter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 97 with ServletServerHttpRequest

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

the class RequestResponseBodyAdviceChainTests method requestBodyAdvice.

@SuppressWarnings("unchecked")
@Test
public void requestBodyAdvice() throws IOException {
    RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
    ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
    List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
    RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
    HttpInputMessage wrapped = new ServletServerHttpRequest(new MockHttpServletRequest());
    given(requestAdvice.supports(this.paramType, String.class, this.converterType)).willReturn(true);
    given(requestAdvice.beforeBodyRead(eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(wrapped);
    assertThat(chain.beforeBodyRead(this.request, this.paramType, String.class, this.converterType)).isSameAs(wrapped);
    String modified = "body++";
    given(requestAdvice.afterBodyRead(eq(this.body), eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(modified);
    assertThat(chain.afterBodyRead(this.body, this.request, this.paramType, String.class, this.converterType)).isEqualTo(modified);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 98 with ServletServerHttpRequest

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

the class ResponseEntityExceptionHandlerTests method noHandlerFoundException.

@Test
public void noHandlerFoundException() {
    ServletServerHttpRequest req = new ServletServerHttpRequest(new MockHttpServletRequest("GET", "/resource"));
    Exception ex = new NoHandlerFoundException(req.getMethod().toString(), req.getServletRequest().getRequestURI(), req.getHeaders());
    testException(ex);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) ServletException(jakarta.servlet.ServletException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) Test(org.junit.jupiter.api.Test)

Example 99 with ServletServerHttpRequest

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

the class AbstractHttpSockJsSession method disableShallowEtagHeaderFilter.

private void disableShallowEtagHeaderFilter(ServerHttpRequest request) {
    if (request instanceof ServletServerHttpRequest) {
        ServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        ShallowEtagHeaderFilter.disableContentCaching(servletRequest);
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletRequest(jakarta.servlet.ServletRequest)

Example 100 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest 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);
    } catch (HandshakeFailureException ex) {
        failure = ex;
    } catch (Exception ex) {
        failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
    } finally {
        if (failure != null) {
            chain.applyAfterHandshake(request, response, failure);
            response.close();
            throw failure;
        }
        response.close();
    }
}
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) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException)

Aggregations

ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)111 Test (org.junit.jupiter.api.Test)39 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)39 HttpRequest (org.springframework.http.HttpRequest)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)13 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)10 IOException (java.io.IOException)8 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 HttpHeaders (org.springframework.http.HttpHeaders)7 MediaType (org.springframework.http.MediaType)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 HttpInputMessage (org.springframework.http.HttpInputMessage)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 URI (java.net.URI)4 ResponseEntity (org.springframework.http.ResponseEntity)4