return (RESPONSE_TYPE)responseHolder.response().forException(e).build(); return (RESPONSE_TYPE)responseHolder.response().forException(e).build(); return (RESPONSE_TYPE)responseHolder.response().forException(e).build();
final Object content = mimeType.startsWith("text/") ? macroExpander.get().filter(file.asText("UTF-8"), mimeType) : file.asBytes(); responseHolder.response().forRequest(request) .withBody(content) .withContentType(mimeType) .withLatestModifiedTime(file.getLatestModificationTime()) .withExpirationTime(duration) .put(); return BREAK;
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_output_a_ResourceFile_when_Etag_not_Matching() throws Exception { headers.put(HEADER_IF_NONE_MATCH, "\"xxxx\""); final ResponseBuilder<?> builder = underTest.response().fromFile(resourceFile) .forRequest(request); assertContents(builder, "ResourceFileOutput.txt"); }
+ "</body>\n" + "</html>", request.getBaseUrl(), request.getOriginalRelativeUri()); responseHolder.response().withContentType("text/html") .withStatus(503) .withExpirationTime(new Duration(0)) .withBody(page) .put(); return BREAK;
final String redirect = mediaUri.asString(); log.info(">>>> permanently redirecting to {}", redirect); responseHolder.response().permanentRedirect(redirect).put(); return BREAK; log.info(">>>> serving contents of {} ...", file.getPath().asString()); responseHolder.response().fromFile(file) .withExpirationTime(duration) .forRequest(request) .put(); return BREAK;
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_properly_output_an_internal_error() throws Exception { final IOException e = new IOException("foo bar"); final ResponseBuilder<?> builder = underTest.response().forException(e); assertContents(builder, "InternalErrorOutput.txt"); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_properly_output_a_ResourceFile_with_ExpirationTime() throws Exception { final ResponseBuilder<?> builder = underTest.response().fromFile(resourceFile) .withExpirationTime(Duration.standardDays(7)); assertContents(builder, "ResourceFileOutputWithExpirationTime.txt"); }
/******************************************************************************************************************* * ******************************************************************************************************************/ private void assertContents (final @Nonnull ResponseBuilder<?> builder, final String fileName) throws Exception { final File actualFile = new File("target/test-results/" + fileName); final File expectedFile = new File("src/test/resources/expected-results/" + fileName); actualFile.getParentFile().mkdirs(); Files.write((byte[])builder.build(), actualFile); FileComparisonUtils.assertSameContents(expectedFile, actualFile); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_properly_output_a_ResourceFile() throws Exception { final ResponseBuilder<?> builder = underTest.response().fromFile(resourceFile); assertContents(builder, "ResourceFileOutput.txt"); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_properly_output_a_PermanentRedirect() throws Exception { final ResponseBuilder<?> builder = underTest.response().permanentRedirect("http://acme.com"); assertContents(builder, "PermanentRedirectOutput.txt"); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_call_some_RequestProcessors_when_RuntimeException() throws Exception { final RuntimeException e = new RuntimeException("Purportedly thrown exception"); mockRequestProcessor3.setThrowable(e); commonExceptionTestSequence(); verify(responseBuilder).forException(same(e)); // inOrder.verifyNoMoreInteractions(); }
when(responseHolder.get()).thenReturn(response); when(responseHolder.response()).thenReturn(responseBuilder); when(responseBuilder.forException(any(NotFoundException.class))).thenReturn(responseBuilder); when(responseBuilder.forException(any(HttpStatusException.class))).thenReturn(responseBuilder); when(responseBuilder.forException(any(Throwable.class))).thenReturn(responseBuilder); when(responseBuilder.build()).thenReturn(response);
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_output_NotModified_when_Etag_Matching() throws Exception { headers.put(HEADER_IF_NONE_MATCH, "\"1341242553456\""); final ResponseBuilder<?> builder = underTest.response().fromFile(resourceFile) .forRequest(request); assertContents(builder, "ResourceFileNotModifiedOutput.txt"); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_roperly_output_NotFound() throws Exception { final NotFoundException e = new NotFoundException("foo bar"); final ResponseBuilder<?> builder = underTest.response().forException(e); assertContents(builder, "NotFoundExceptionOutput.txt"); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_output_a_ResourceFile_when_IfModifiedSince_less_recent_than_ModifiedTime() throws Exception { for (int deltaSeconds = -10; deltaSeconds < 0; deltaSeconds++) { final DateTime ifModifiedSinceTime = resourceLatestModifiedTime.plusSeconds(deltaSeconds); headers.put(HEADER_IF_MODIFIED_SINCE, toString(ifModifiedSinceTime)); final ResponseBuilder<?> builder = underTest.response().fromFile(resourceFile) .forRequest(request); assertContents(builder, "ResourceFileOutput.txt"); } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_call_some_RequestProcessors_when_NotFoundException() throws Exception { final NotFoundException e = new NotFoundException(); mockRequestProcessor3.setThrowable(e); commonExceptionTestSequence(); verify(responseBuilder).forException(same(e)); // inOrder.verifyNoMoreInteractions(); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_output_NotModified_when_IfModifiedSince_more_recent_than_or_equal_to_ModifiedTime() throws Exception { // corner case: same time should return NotModified for (int deltaSeconds = 0; deltaSeconds < 10; deltaSeconds++) { final DateTime ifModifiedSinceTime = resourceLatestModifiedTime.plusSeconds(deltaSeconds); headers.put(HEADER_IF_MODIFIED_SINCE, toString(ifModifiedSinceTime)); final ResponseBuilder<?> builder = underTest.response().fromFile(resourceFile) .forRequest(request); assertContents(builder, "ResourceFileNotModifiedOutput.txt"); } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_call_some_RequestProcessors_when_HttpStatusException_with_generic_Http_status() throws Exception { final HttpStatusException e = new HttpStatusException(SC_NOT_ACCEPTABLE); mockRequestProcessor3.setThrowable(e); commonExceptionTestSequence(); verify(responseBuilder).forException(same(e)); // inOrder.verifyNoMoreInteractions(); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_call_some_RequestProcessors_when_HttpStatusException_with_SC_FOUND() throws Exception { final HttpStatusException e = new HttpStatusException(SC_FOUND); mockRequestProcessor3.setThrowable(e); commonExceptionTestSequence(); verify(responseBuilder).forException(same(e)); // inOrder.verifyNoMoreInteractions(); }