@Test public void testBadJsonVarConcat() throws Exception { expectRequestWithUris(Lists.<Uri>newArrayList(), "bad code;"); servlet.doGet(request, recorder); verify(); String results = "/* ---- Error 400, Bad json variable name bad code; ---- */\r\n"; assertEquals(results, recorder.getResponseAsString()); assertEquals(400, recorder.getHttpStatusCode()); }
@Test public void errorsAreEscaped() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.error("busted<script>alert(document.domain)</script>", HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); control.replay(); servlet.doGet(request, recorder); assertEquals("busted<script>alert(document.domain)</script>", recorder.getResponseAsString()); assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, recorder.getHttpStatusCode()); }
@Test public void errorsAreEscaped() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.error("busted<script>alert(document.domain)</script>", HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); control.replay(); servlet.doGet(request, recorder); assertEquals("busted<script>alert(document.domain)</script>", recorder.getResponseAsString()); assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, recorder.getHttpStatusCode()); }
@Test public void testServlet() throws Exception { OAuthCallbackServlet servlet = new OAuthCallbackServlet(); replay(); servlet.doGet(this.request, this.recorder); verify(); assertEquals("text/html; charset=UTF-8", this.recorder.getContentType()); String body = this.recorder.getResponseAsString(); Assert.assertNotSame("body is " + body, body.indexOf("window.close()"), -1); }
@Test public void normalResponse() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); control.replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_OK, recorder.getHttpStatusCode()); assertEquals("private,max-age=" + GadgetRenderingServlet.DEFAULT_CACHE_TTL, recorder.getHeader("Cache-Control")); assertEquals("working", recorder.getResponseAsString()); }
@Test public void errorsPassedThrough() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.error("busted", HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); control.replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, recorder.getHttpStatusCode()); assertNull("Cache-Control header passed where it should not be.", recorder.getHeader("Cache-Control")); assertEquals("busted", recorder.getResponseAsString()); }
@Test public void normalResponse() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); control.replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_OK, recorder.getHttpStatusCode()); assertEquals("private,max-age=" + GadgetRenderingServlet.DEFAULT_CACHE_TTL, recorder.getHeader("Cache-Control")); assertEquals("working", recorder.getResponseAsString()); }
@Test public void outputEncodingIsUtf8() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok(NON_ASCII_STRING)); control.replay(); servlet.doGet(request, recorder); assertEquals("UTF-8", recorder.getCharacterEncoding()); assertEquals("text/html", recorder.getContentType()); assertEquals(NON_ASCII_STRING, recorder.getResponseAsString()); }
@Test public void errorsPassedThrough() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.error("busted", HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); control.replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, recorder.getHttpStatusCode()); assertNull("Cache-Control header passed where it should not be.", recorder.getHeader("Cache-Control")); assertEquals("busted", recorder.getResponseAsString()); }
@Test public void outputEncodingIsUtf8() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok(NON_ASCII_STRING)); control.replay(); servlet.doGet(request, recorder); assertEquals("UTF-8", recorder.getCharacterEncoding()); assertEquals("text/html", recorder.getContentType()); assertEquals(NON_ASCII_STRING, recorder.getResponseAsString()); }
@Test public void testDoGetException() throws Exception { setupGet(); expect(pipeline.execute(internalRequest)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); }
@Test public void testDoPostException() throws Exception { setupPost(); expect(pipeline.execute(internalRequest)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doPost(request, recorder); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); } }
@Test public void testDoGetException() throws Exception { setupGet(); expect(pipeline.execute(internalRequest)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); }
@Test public void testDoGetException() throws Exception { setupGet(); expect(pipeline.execute(internalRequest)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); }
@Test public void testDoPostException() throws Exception { setupPost(); expect(pipeline.execute(internalRequest)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doPost(request, recorder); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); } }
@Test public void testDoPostException() throws Exception { setupPost(); expect(pipeline.execute(internalRequest)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doPost(request, recorder); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); } }
@Test public void testDoGetException() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(proxyHandler.fetch(proxyUri)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doGet(request, recorder); verify(); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); }
@Test public void testDoGetException() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(proxyHandler.fetch(proxyUri)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doGet(request, recorder); verify(); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); }
@Test public void testDoPostNormalWithLockedDomainUnsafe() throws Exception { setupRequest(BASIC_SYNTAX_URL, false); replay(); servlet.doGet(request, recorder); verify(); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains("wrong domain", recorder.getResponseAsString()); }
@Test public void testDoGetNormalWithLockedDomainUnsafe() throws Exception { setupRequest(BASIC_SYNTAX_URL, false); replay(); servlet.doGet(request, recorder); verify(); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains("wrong domain", recorder.getResponseAsString()); }