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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations