@Test public void shouldThrowsResultExceptionIfAnIOExceptionWhenSendError() throws Exception { doThrow(new IOException()).when(response).sendError(anyInt()); try { httpResult.sendError(SC_INTERNAL_SERVER_ERROR); fail("should throw ResultException"); } catch (ResultException e) { verify(response, only()).sendError(anyInt()); } }
@Test public void shouldNotThrowNullPointersOnHttpResult() throws Exception { result.use(http()) .addDateHeader(null, 0) .addHeader(null, null) .addIntHeader(null, 0); result.use(http()).sendError(0); result.use(http()).sendError(0, ""); result.use(http()).setStatusCode(0); } @Test
@Test public void shouldThrowResultExceptionIfAnIOExceptionWhenWriteStringBody() throws Exception { doThrow(new IOException()).when(response).getWriter(); try { httpResult.body("The text"); fail("should throw ResultException"); } catch (ResultException e) { } }
@Test public void shouldSetStatusCode() throws Exception { httpResult.setStatusCode(SC_INTERNAL_SERVER_ERROR); verify(response).setStatus(SC_INTERNAL_SERVER_ERROR); }
public static class RandomComponent { public void random() { } }
@Test public void onHttpResultShouldThrowExceptionsOnSetStatus() throws Exception { exception.expect(ValidationException.class); HttpResult httpResult = mock(HttpResult.class); when(result.use(HttpResult.class)).thenReturn(httpResult); factory.instanceFor(HttpResult.class, errors).setStatusCode(200); }
@Override public void deny(InvalidInputException e) { result.use(Results.http()).sendError(SC_BAD_REQUEST, e.getMessage()); } }
@Test public void shouldThrowResultExceptionIfAnIOExceptionWhenWriteInputStreamBody() throws Exception { doThrow(new IOException()).when(response).getOutputStream(); InputStream in = new ByteArrayInputStream("the text".getBytes()); try { httpResult.body(in); fail("should throw ResultException"); } catch (ResultException e) { } }
@Test public void shouldThrowResultExceptionIfAnIOExceptionWhenSendErrorWithMessage() throws Exception { doThrow(new IOException()).when(response).sendError(anyInt(), anyString()); try { httpResult.sendError(SC_INTERNAL_SERVER_ERROR, "A simple message"); fail("should throw ResultException"); } catch (ResultException e) { verify(response, only()).sendError(anyInt(), anyString()); } }
@Test public void shouldThrowResultExceptionIfAnIOExceptionWhenWriteReaderBody() throws Exception { doThrow(new IOException()).when(response).getWriter(); Reader reader = new StringReader("the text"); try { httpResult.body(reader); fail("should throw ResultException"); } catch (ResultException e) { } } }
@Test public void onHttpResultShouldThrowExceptionsOnSendError() throws Exception { exception.expect(ValidationException.class); HttpResult httpResult = mock(HttpResult.class); when(result.use(HttpResult.class)).thenReturn(httpResult); factory.instanceFor(HttpResult.class, errors).sendError(404); }
@Test public void test() throws Exception { result.use(Results.http()).body("content"); Assert.assertEquals("content", result.getBody()); } }
@Override public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) throws InterceptionException { if (user.isSignedIn()) { stack.next(method, instance); } else if (isAjaxRequest()) { result.use(http()).sendError(SC_UNAUTHORIZED); } else { result.redirectTo("/?urlAfterLogin=" + getReferer()); } }