Search in sources :

Example 26 with HttpServletResponse

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

the class TestExpiresFilter method testUseMajorTypeExpiresConfiguration.

@Test
public void testUseMajorTypeExpiresConfiguration() 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/json; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };
    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 27 with HttpServletResponse

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

the class TestExpiresFilter method testUseDefaultConfiguration1.

@Test
public void testUseDefaultConfiguration1() 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.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 28 with HttpServletResponse

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

the class AsyncStockServlet method writeStock.

public void writeStock(AsyncContext actx, Stock stock) throws IOException {
    HttpServletResponse response = (HttpServletResponse) actx.getResponse();
    PrintWriter writer = response.getWriter();
    // make client parsing easier
    writer.write("STOCK#");
    writer.write(stock.getSymbol());
    writer.write("#");
    writer.write(stock.getValueAsString());
    writer.write("#");
    writer.write(stock.getLastChangeAsString());
    writer.write("#");
    writer.write(String.valueOf(stock.getCnt()));
    writer.write("\n");
    writer.flush();
    response.flushBuffer();
}
Also used : HttpServletResponse(jakarta.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Example 29 with HttpServletResponse

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

the class ResourceUrlEncodingFilterTests method testEncodeUrl.

private void testEncodeUrl(MockHttpServletRequest request, String url, String expected) throws ServletException, IOException {
    FilterChain chain = (req, res) -> {
        req.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.urlProvider);
        String result = ((HttpServletResponse) res).encodeURL(url);
        assertThat(result).isEqualTo(expected);
    };
    this.filter.doFilter(request, new MockHttpServletResponse(), chain);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) ClassPathResource(org.springframework.core.io.ClassPathResource) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 30 with HttpServletResponse

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

the class ServletServerHttpAsyncRequestControl method start.

@Override
public void start(long timeout) {
    Assert.state(!isCompleted(), "Async processing has already completed");
    if (isStarted()) {
        return;
    }
    HttpServletRequest servletRequest = this.request.getServletRequest();
    HttpServletResponse servletResponse = this.response.getServletResponse();
    this.asyncContext = servletRequest.startAsync(servletRequest, servletResponse);
    this.asyncContext.addListener(this);
    if (timeout != NO_TIMEOUT_VALUE) {
        this.asyncContext.setTimeout(timeout);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse)

Aggregations

HttpServletResponse (jakarta.servlet.http.HttpServletResponse)118 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)76 Test (org.junit.jupiter.api.Test)47 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)34 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)31 FilterChain (jakarta.servlet.FilterChain)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)16 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 ServletException (jakarta.servlet.ServletException)14 StandardCharsets (java.nio.charset.StandardCharsets)14 HttpServlet (jakarta.servlet.http.HttpServlet)13 IOException (java.io.IOException)12 HashMap (java.util.HashMap)12 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 Test (org.junit.Test)10 Authentication (org.springframework.security.core.Authentication)10 FileCopyUtils (org.springframework.util.FileCopyUtils)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 Collections (java.util.Collections)7