Search in sources :

Example 66 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class HttpEntityMethodProcessorTests method handleReturnValueWithETagAndETagFilter.

// SPR-13423
@Test
public void handleReturnValueWithETagAndETagFilter() throws Exception {
    String eTagValue = "\"deadb33f8badf00d\"";
    String content = "body";
    Method method = getClass().getDeclaredMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    FilterChain chain = (req, res) -> {
        ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(eTagValue).body(content);
        try {
            ServletWebRequest requestToUse = new ServletWebRequest((HttpServletRequest) req, (HttpServletResponse) res);
            new HttpEntityMethodProcessor(Collections.singletonList(new StringHttpMessageConverter())).handleReturnValue(returnValue, returnType, mavContainer, requestToUse);
            assertThat(this.servletResponse.getContentAsString()).as("Response body was cached? It should be written directly to the raw response").isEqualTo(content);
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    };
    this.servletRequest.setMethod("GET");
    new ShallowEtagHeaderFilter().doFilter(this.servletRequest, this.servletResponse, chain);
    assertThat(this.servletResponse.getStatus()).isEqualTo(200);
    assertThat(this.servletResponse.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue);
    assertThat(this.servletResponse.getContentAsString()).isEqualTo(content);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayList(java.util.ArrayList) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MethodParameter(org.springframework.core.MethodParameter) Nullable(org.springframework.lang.Nullable) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) Method(java.lang.reflect.Method) ShallowEtagHeaderFilter(org.springframework.web.filter.ShallowEtagHeaderFilter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) FilterChain(jakarta.servlet.FilterChain) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Serializable(java.io.Serializable) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) WebDataBinder(org.springframework.web.bind.WebDataBinder) ResponseEntity(org.springframework.http.ResponseEntity) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ShallowEtagHeaderFilter(org.springframework.web.filter.ShallowEtagHeaderFilter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ResponseEntity(org.springframework.http.ResponseEntity) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 67 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method matchWildcard.

@Test
public void matchWildcard() {
    ProducesRequestCondition condition = new ProducesRequestCondition("text/*");
    HttpServletRequest request = createRequest("text/plain");
    assertThat(condition.getMatchingCondition(request)).isNotNull();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 68 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class RequestMethodsRequestConditionTests method getMatchingConditionWithEmptyConditions.

@Test
public void getMatchingConditionWithEmptyConditions() {
    RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition();
    for (RequestMethod method : RequestMethod.values()) {
        if (method != OPTIONS) {
            HttpServletRequest request = new MockHttpServletRequest(method.name(), "");
            assertThat(condition.getMatchingCondition(request)).isNotNull();
        }
    }
    testNoMatch(condition, OPTIONS);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 69 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method adaptRequestFromForwardedHeaders.

// SPR-16668
private void adaptRequestFromForwardedHeaders() throws Exception {
    MockFilterChain chain = new MockFilterChain();
    new ForwardedHeaderFilter().doFilter(this.request, new MockHttpServletResponse(), chain);
    HttpServletRequest adaptedRequest = (HttpServletRequest) chain.getRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(adaptedRequest));
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ForwardedHeaderFilter(org.springframework.web.filter.ForwardedHeaderFilter) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 70 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class RequestConditionHolderTests method compareEmpty.

@Test
public void compareEmpty() {
    HttpServletRequest request = new MockHttpServletRequest();
    RequestConditionHolder empty = new RequestConditionHolder(null);
    RequestConditionHolder empty2 = new RequestConditionHolder(null);
    RequestConditionHolder notEmpty = new RequestConditionHolder(new ParamsRequestCondition("name"));
    assertThat(empty.compareTo(empty2, request)).isEqualTo(0);
    assertThat(notEmpty.compareTo(empty, request)).isEqualTo(-1);
    assertThat(empty.compareTo(notEmpty, request)).isEqualTo(1);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletRequest (jakarta.servlet.http.HttpServletRequest)289 Test (org.junit.jupiter.api.Test)160 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)93 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)88 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)67 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)44 Authentication (org.springframework.security.core.Authentication)31 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)31 Test (org.junit.Test)28 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)26 IOException (java.io.IOException)21 ServletException (jakarta.servlet.ServletException)20 HttpServlet (jakarta.servlet.http.HttpServlet)19 HashMap (java.util.HashMap)17 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)16 FilterChain (jakarta.servlet.FilterChain)15 HttpSession (jakarta.servlet.http.HttpSession)14 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)14 ServletRequest (jakarta.servlet.ServletRequest)13