rejectedsoftware/vibe.d — pull request #556 by dymk (2014-03-05 08:42:47+00:00)
Title: Remove final from HTTPServerResponse and Request

This allows HTTPServerResponse and HTTPServerRequest to be subclassed, which allows much easier unittesting as `bodyWriter` can be overridden, which is as close to mocking/stubbing methods as we're going to get I think.

I'm bringing this up, because I'm needing to subclass HTTPServerResponse with a dummy class rig that looks like this:

```d
/*
* Stub of ConnectionStream to satisfy HTTPServerResponse
*/
private final class NullConnStream : ConnectionStream {
/// InputStream
@property bool empty() { return true; }
@property ulong leastSize() { return 0; }
@property bool dataAvailableForRead() { return false; };
const(ubyte)[] peek() { return []; };
void read(ubyte[] dst) {};

/// OutputStream
void write(in ubyte[] bytes) {}
void flush() {}
void finalize() {}
void write(InputStream stream, ulong nbytes = 0) {};

// ConnectionStream
@property bool connected() const { return false; }
void close() {}
bool waitForData(Duration timeout = 0.seconds) { return false; }
}

/*
* Stub of HTTPServerResponse to override bodyWriter
*/
private final class DummyHTTPServerResponse : HTTPServerResponse {
private:
AppenderOutputStream appender;

public:
this() {
super(
new NullConnStream(),
new NullConnStream(),
null, null);
appender = new AppenderOutputStream();
}

override @property OutputStream bodyWriter() {
return appender;
}

string rendered() {
return appender.data();
}
}
```

in order to grab just the body that was written to the stream, in order to test that the body being written is correct. So, then I can use this rig like this:

```d
unittest {
auto resp = new DummyHTTPServerResponse();
resp.renderTemple!`
Something here

Something more here

<%= "

Escape me!

" %>
`;

assert(isSameRender(resp.rendered, `
Something here

Something more here

<p>Escape me!</p>
`));
}
```

And, in general, it opens up the possibility for testing the request/response cycle from a higher level (although I don't believe there is currently any way to test the content of responses period).