ResponseCachingOutputStream

A byte output stream implementation that caches HTTP header and body content as it's written, so that you can fetch and inspect the contents later. This is mainly intended as a helper for unit tests that involve checking the raw response body for request handlers.

To use this, create an instance and supply it to an HttpResponseBuilder when building a mocked request context:

auto sOut = new ResponseCachingOutputStream();
auto ctx = new HttpRequestContextBuilder()
  .request().withMethod(Method.GET).withUrl("/users").and()
  .response().withOutputStream(sOut).and()
  .build();
myHandlerFunction(ctx);
assert(sOut.getBody() == "expected body content");
class ResponseCachingOutputStream : OutputStream!ubyte {}

Examples

void mockHandler(ref HttpRequestContext ctx) {
    ctx.response.writeBodyString("Hello world!");
}

ResponseCachingOutputStream stream = new ResponseCachingOutputStream();
HttpRequestContext ctx = new HttpRequestContextBuilder()
    .response()
        .withOutputStream(stream)
        .and()
    .build();
mockHandler(ctx);
assert(stream.getBody() == "Hello world!", stream.getBody());