Search in sources :

Example 11 with HttpServlet

use of jakarta.servlet.http.HttpServlet in project tomcat by apache.

the class TestExpiresFilter method testUseDefaultConfiguration2.

@Test
public void testUseDefaultConfiguration2() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("image/jpeg");
            response.addHeader("Cache-Control", "private");
            response.getWriter().print("Hello world");
        }
    };
    validate(servlet, Integer.valueOf(1 * 60));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 12 with HttpServlet

use of jakarta.servlet.http.HttpServlet in project tomcat by apache.

the class TestExpiresFilter method testEmptyContent.

/*
     * Test that a resource with empty content is also processed
     */
@Test
public void testEmptyContent() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/plain");
        // no content is written in the response
        }
    };
    validate(servlet, Integer.valueOf(7 * 60));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 13 with HttpServlet

use of jakarta.servlet.http.HttpServlet in project tomcat by apache.

the class TestExpiresFilter method testNullContentType.

@Test
public void testNullContentType() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType(null);
        }
    };
    validate(servlet, Integer.valueOf(1 * 60));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 14 with HttpServlet

use of jakarta.servlet.http.HttpServlet in project tomcat by apache.

the class TestExpiresFilter method testSkipBecauseCacheControlMaxAgeIsDefined.

@Test
public void testSkipBecauseCacheControlMaxAgeIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addHeader("Cache-Control", "private, max-age=232");
            response.getWriter().print("Hello world");
        }
    };
    validate(servlet, Integer.valueOf(232));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 15 with HttpServlet

use of jakarta.servlet.http.HttpServlet in project spring-security by spring-projects.

the class HttpSessionSecurityContextRepositoryTests method saveContextWhenSaveNewContextThenOriginalContextThenOriginalContextSaved.

@Test
public void saveContextWhenSaveNewContextThenOriginalContextThenOriginalContextSaved() throws Exception {
    HttpSessionSecurityContextRepository repository = new HttpSessionSecurityContextRepository();
    SecurityContextPersistenceFilter securityContextPersistenceFilter = new SecurityContextPersistenceFilter(repository);
    UserDetails original = User.withUsername("user").password("password").roles("USER").build();
    SecurityContext originalContext = createSecurityContext(original);
    UserDetails impersonate = User.withUserDetails(original).username("impersonate").build();
    SecurityContext impersonateContext = createSecurityContext(impersonate);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    Filter saveImpersonateContext = (request, response, chain) -> {
        SecurityContextHolder.setContext(impersonateContext);
        // ensure the response is committed to trigger save
        response.flushBuffer();
        chain.doFilter(request, response);
    };
    Filter saveOriginalContext = (request, response, chain) -> {
        SecurityContextHolder.setContext(originalContext);
        chain.doFilter(request, response);
    };
    HttpServlet servlet = new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("Hi");
        }
    };
    SecurityContextHolder.setContext(originalContext);
    MockFilterChain chain = new MockFilterChain(servlet, saveImpersonateContext, saveOriginalContext);
    securityContextPersistenceFilter.doFilter(mockRequest, mockResponse, chain);
    assertThat(mockRequest.getSession().getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY)).isEqualTo(originalContext);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) User(org.springframework.security.core.userdetails.User) ServletException(jakarta.servlet.ServletException) Transient(org.springframework.security.core.Transient) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Retention(java.lang.annotation.Retention) Filter(jakarta.servlet.Filter) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) ServletOutputStream(jakarta.servlet.ServletOutputStream) HttpSession(jakarta.servlet.http.HttpSession) TestAuthentication(org.springframework.security.authentication.TestAuthentication) TransientSecurityContext(org.springframework.security.core.context.TransientSecurityContext) BDDMockito.given(org.mockito.BDDMockito.given) UserDetails(org.springframework.security.core.userdetails.UserDetails) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) IOException(java.io.IOException) Target(java.lang.annotation.Target) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) ElementType(java.lang.annotation.ElementType) HttpServlet(jakarta.servlet.http.HttpServlet) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) AfterEach(org.junit.jupiter.api.AfterEach) AuthenticationTrustResolver(org.springframework.security.authentication.AuthenticationTrustResolver) Mockito.never(org.mockito.Mockito.never) SecurityContext(org.springframework.security.core.context.SecurityContext) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Authentication(org.springframework.security.core.Authentication) Collections(java.util.Collections) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) Mockito.reset(org.mockito.Mockito.reset) RetentionPolicy(java.lang.annotation.RetentionPolicy) Mockito.mock(org.mockito.Mockito.mock) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UserDetails(org.springframework.security.core.userdetails.UserDetails) Filter(jakarta.servlet.Filter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) TransientSecurityContext(org.springframework.security.core.context.TransientSecurityContext) SecurityContext(org.springframework.security.core.context.SecurityContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServlet (jakarta.servlet.http.HttpServlet)15 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)13 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)13 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 Test (org.junit.Test)10 Servlet (jakarta.servlet.Servlet)2 ServletException (jakarta.servlet.ServletException)2 IOException (java.io.IOException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)2 Test (org.testng.annotations.Test)2 Filter (jakarta.servlet.Filter)1 ServletOutputStream (jakarta.servlet.ServletOutputStream)1 ServletRequest (jakarta.servlet.ServletRequest)1 ServletResponse (jakarta.servlet.ServletResponse)1 HttpServletRequestWrapper (jakarta.servlet.http.HttpServletRequestWrapper)1 HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)1 HttpSession (jakarta.servlet.http.HttpSession)1 ElementType (java.lang.annotation.ElementType)1 Retention (java.lang.annotation.Retention)1