Search in sources :

Example 71 with Request

use of org.eclipse.jetty.server.Request in project buck by facebook.

the class ResponsesTest method testWriteSuccessfulResponse.

@Test
public void testWriteSuccessfulResponse() throws IOException {
    String content = "<html>Hello, world!</html>";
    Request baseRequest = createMock(Request.class);
    baseRequest.setHandled(true);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("text/html; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    replayAll();
    Responses.writeSuccessfulResponse(content, MediaType.HTML_UTF_8, baseRequest, response);
    verifyAll();
    assertEquals(content, stringWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) Request(org.eclipse.jetty.server.Request) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 72 with Request

use of org.eclipse.jetty.server.Request in project buck by facebook.

the class TemplateHandlerTest method testHandleMalformedRequest.

@Test
public void testHandleMalformedRequest() throws IOException, ServletException {
    TemplateHandlerDelegate delegate = new TemplateHandlerDelegate() {

        @Override
        public ImmutableSet<String> getTemplates() {
            return ImmutableSet.of("example.soy");
        }

        @Override
        public String getTemplateForRequest(Request baseRequest) {
            return "example.hello";
        }

        @Override
        public SoyMapData getDataForRequest(Request baseRequest) throws IOException {
            // Returning null should cause a 500 to be returned.
            return null;
        }
    };
    String target = "target";
    Request baseRequest = createMock(Request.class);
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(500);
    response.setContentType("text/plain; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    replayAll();
    TemplateHandler handler = new TemplateHandler(delegate);
    handler.handle(target, baseRequest, request, response);
    verifyAll();
    assertEquals("ERROR", stringWriter.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 73 with Request

use of org.eclipse.jetty.server.Request in project buck by facebook.

the class TemplateHandlerTest method testHandleSimpleRequest.

@Test
public void testHandleSimpleRequest() throws IOException, ServletException {
    TemplateHandlerDelegate delegate = new TemplateHandlerDelegate() {

        @Override
        public ImmutableSet<String> getTemplates() {
            return ImmutableSet.of("example.soy");
        }

        @Override
        public String getTemplateForRequest(Request baseRequest) {
            return "example.hello";
        }

        @Override
        public SoyMapData getDataForRequest(Request baseRequest) throws IOException {
            return new SoyMapData("name", "Michael");
        }
    };
    String target = "target";
    Request baseRequest = createMock(Request.class);
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("text/html; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    replayAll();
    TemplateHandler handler = new TemplateHandler(delegate);
    handler.handle(target, baseRequest, request, response);
    verifyAll();
    assertEquals("Hello, Michael!", stringWriter.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SoyMapData(com.google.template.soy.data.SoyMapData) StringWriter(java.io.StringWriter) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 74 with Request

use of org.eclipse.jetty.server.Request in project buck by facebook.

the class TraceDataHandlerTest method testHandleGetWithMultipleTraceCallback.

@Test
public void testHandleGetWithMultipleTraceCallback() throws IOException, ServletException {
    Request baseRequest = createMock(Request.class);
    expect(baseRequest.getMethod()).andReturn("GET");
    expect(baseRequest.getPathInfo()).andReturn("/abcdef");
    expect(baseRequest.getParameter("callback")).andReturn("my.callback");
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("application/javascript; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    BuildTraces buildTraces = createMock(BuildTraces.class);
    Iterable<InputStream> traces = Arrays.asList(new ByteArrayInputStream("{\"foo\":\"bar\"}".getBytes(Charsets.UTF_8)), new ByteArrayInputStream("{\"baz\":\"blech\"}".getBytes(Charsets.UTF_8)));
    expect(buildTraces.getInputsForTraces("abcdef")).andReturn(traces);
    TraceDataHandler traceDataHandler = new TraceDataHandler(buildTraces);
    replayAll();
    traceDataHandler.handle("/trace/abcdef?callback=my.callback", baseRequest, request, response);
    verifyAll();
    assertEquals("my.callback([{\"foo\":\"bar\"},{\"baz\":\"blech\"}]);\n", stringWriter.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) BuildTraces(com.facebook.buck.util.trace.BuildTraces) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 75 with Request

use of org.eclipse.jetty.server.Request in project buck by facebook.

the class TraceDataHandlerTest method testHandleGet.

@Test
public void testHandleGet() throws IOException, ServletException {
    Request baseRequest = createMock(Request.class);
    expect(baseRequest.getMethod()).andReturn("GET");
    expect(baseRequest.getPathInfo()).andReturn("/abcdef");
    expect(baseRequest.getParameter("callback")).andReturn(null);
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("application/javascript; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    BuildTraces buildTraces = createMock(BuildTraces.class);
    Iterable<InputStream> traces = Arrays.asList(new ByteArrayInputStream("{\"foo\":\"bar\"}".getBytes(Charsets.UTF_8)));
    expect(buildTraces.getInputsForTraces("abcdef")).andReturn(traces);
    TraceDataHandler traceDataHandler = new TraceDataHandler(buildTraces);
    replayAll();
    traceDataHandler.handle("/trace/abcdef", baseRequest, request, response);
    verifyAll();
    assertEquals("[{\"foo\":\"bar\"}]", stringWriter.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) BuildTraces(com.facebook.buck.util.trace.BuildTraces) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

Request (org.eclipse.jetty.server.Request)297 HttpServletRequest (javax.servlet.http.HttpServletRequest)250 HttpServletResponse (javax.servlet.http.HttpServletResponse)225 Test (org.junit.Test)177 IOException (java.io.IOException)169 ServletException (javax.servlet.ServletException)150 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)147 CountDownLatch (java.util.concurrent.CountDownLatch)66 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)64 InterruptedIOException (java.io.InterruptedIOException)45 Server (org.eclipse.jetty.server.Server)41 InputStream (java.io.InputStream)37 AtomicReference (java.util.concurrent.atomic.AtomicReference)35 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Response (org.eclipse.jetty.client.api.Response)27 Result (org.eclipse.jetty.client.api.Result)27 ServletInputStream (javax.servlet.ServletInputStream)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ServletOutputStream (javax.servlet.ServletOutputStream)23 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)23