Search in sources :

Example 81 with HttpServletRequest

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

the class ProducesRequestConditionTests method matchParseErrorWithNegation.

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

Example 82 with HttpServletRequest

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

the class AbstractRequestLoggingFilter method doFilterInternal.

/**
 * Forwards the request to the next filter in the chain and delegates down to the subclasses
 * to perform the actual request logging both before and after the request is processed.
 * @see #beforeRequest
 * @see #afterRequest
 */
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    boolean isFirstRequest = !isAsyncDispatch(request);
    HttpServletRequest requestToUse = request;
    if (isIncludePayload() && isFirstRequest && !(request instanceof ContentCachingRequestWrapper)) {
        requestToUse = new ContentCachingRequestWrapper(request, getMaxPayloadLength());
    }
    boolean shouldLog = shouldLog(requestToUse);
    if (shouldLog && isFirstRequest) {
        beforeRequest(requestToUse, getBeforeMessage(requestToUse));
    }
    try {
        filterChain.doFilter(requestToUse, response);
    } finally {
        if (shouldLog && !isAsyncStarted(requestToUse)) {
            afterRequest(requestToUse, getAfterMessage(requestToUse));
        }
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 83 with HttpServletRequest

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

the class ForwardedHeaderFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    if (this.removeOnly) {
        ForwardedHeaderRemovingRequest wrappedRequest = new ForwardedHeaderRemovingRequest(request);
        filterChain.doFilter(wrappedRequest, response);
    } else {
        HttpServletRequest wrappedRequest = new ForwardedHeaderExtractingRequest(request);
        HttpServletResponse wrappedResponse = this.relativeRedirects ? RelativeRedirectResponseWrapper.wrapIfNecessary(response, HttpStatus.SEE_OTHER) : new ForwardedHeaderExtractingResponse(response, wrappedRequest);
        filterChain.doFilter(wrappedRequest, wrappedResponse);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse)

Example 84 with HttpServletRequest

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

the class FormTag method processAction.

/**
 * Process the action through a {@link RequestDataValueProcessor} instance
 * if one is configured or otherwise returns the action unmodified.
 */
private String processAction(String action) {
    RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
    ServletRequest request = this.pageContext.getRequest();
    if (processor != null && request instanceof HttpServletRequest) {
        action = processor.processAction((HttpServletRequest) request, action, getHttpMethod());
    }
    return action;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor)

Example 85 with HttpServletRequest

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method pathVarsInModel.

@PathPatternsParameterizedTest
void pathVarsInModel(boolean usePathPatterns) throws Exception {
    final Map<String, Object> pathVars = new HashMap<>();
    pathVars.put("hotel", "42");
    pathVars.put("booking", 21);
    pathVars.put("other", "other");
    WebApplicationContext wac = initDispatcherServlet(ViewRenderingController.class, usePathPatterns, context -> {
        RootBeanDefinition beanDef = new RootBeanDefinition(ModelValidatingViewResolver.class);
        beanDef.getConstructorArgumentValues().addGenericArgumentValue(pathVars);
        context.registerBeanDefinition("viewResolver", beanDef);
    });
    HttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42;q=1,2/bookings/21-other;q=3;r=R");
    getServlet().service(request, new MockHttpServletResponse());
    ModelValidatingViewResolver resolver = wac.getBean(ModelValidatingViewResolver.class);
    assertThat(resolver.validatedAttrCount).isEqualTo(3);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) WebApplicationContext(org.springframework.web.context.WebApplicationContext) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

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