|
1 | 1 | module temple.tests.vibe; |
2 | 2 |
|
3 | | -/** |
4 | | - * Tests here depend on vibe.d's HTTPServer{Request,Response} being nonfinal |
5 | | - * in order to mock methods on them. However, this would require a modification |
6 | | - * to the vibe.d library, so by default they're disabled. |
7 | | - */ |
8 | | - |
9 | | -version(none): |
| 3 | +version(TempleUnittest): |
10 | 4 | version(Have_vibe_d): |
11 | 5 |
|
12 | 6 | private { |
13 | 7 | import temple.tests.common; |
14 | 8 | import vibe.http.server; |
15 | 9 | import vibe.core.stream; |
| 10 | + import vibe.stream.memory; |
16 | 11 | import core.time; |
17 | 12 | } |
18 | 13 |
|
19 | 14 | /* |
20 | | - * Stub of ConnectionStream to satisfy HTTPServerResponse |
21 | | - */ |
22 | | -private final class NullConnStream : ConnectionStream { |
23 | | - /// InputStream |
24 | | - @property bool empty() { return true; } |
25 | | - @property ulong leastSize() { return 0; } |
26 | | - @property bool dataAvailableForRead() { return false; }; |
27 | | - const(ubyte)[] peek() { return []; }; |
28 | | - void read(ubyte[] dst) {}; |
29 | | - |
30 | | - /// OutputStream |
31 | | - void write(in ubyte[] bytes) {} |
32 | | - void flush() {} |
33 | | - void finalize() {} |
34 | | - void write(InputStream stream, ulong nbytes = 0) {}; |
35 | | - |
36 | | - // ConnectionStream |
37 | | - @property bool connected() const { return false; } |
38 | | - void close() {} |
39 | | - bool waitForData(Duration timeout = 0.seconds) { return false; } |
40 | | -} |
41 | | - |
42 | | -/* |
43 | | - * Stub of HTTPServerResponse to override bodyWriter |
| 15 | + * Drops HTTP headers from the stream output and uses proper newlines |
44 | 16 | */ |
45 | | -private final class DummyHTTPServerResponse : HTTPServerResponse { |
46 | | -private: |
47 | | - AppenderOutputStream appender; |
48 | | - |
49 | | -public: |
50 | | - this() { |
51 | | - super( |
52 | | - new NullConnStream(), |
53 | | - new NullConnStream(), |
54 | | - null, null); |
55 | | - appender = new AppenderOutputStream(); |
56 | | - } |
| 17 | +private string rendered(MemoryOutputStream output) { |
| 18 | + import std.string: split, join; |
57 | 19 |
|
58 | | - override @property vibe.core.stream.OutputStream bodyWriter() { |
59 | | - return appender; |
60 | | - } |
| 20 | + string data = cast(string)output.data; |
| 21 | + string[] lines = data.split("\r\n"); |
| 22 | + lines = lines[4 .. $]; |
61 | 23 |
|
62 | | - string rendered() { |
63 | | - return appender.data(); |
64 | | - } |
| 24 | + return lines.join("\n"); |
65 | 25 | } |
66 | 26 |
|
67 | 27 | unittest { |
68 | | - auto resp = new DummyHTTPServerResponse(); |
| 28 | + auto output = new MemoryOutputStream(); |
| 29 | + auto resp = createTestHTTPServerResponse(output); |
69 | 30 | resp.renderTemple!` |
70 | 31 | Something here |
71 | 32 | <p>Something more here</p> |
72 | 33 | <%= "<p>Escape me!</p>" %> |
73 | 34 | `; |
| 35 | + resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream |
74 | 36 |
|
75 | | - assert(isSameRender(resp.rendered, ` |
| 37 | + assert(isSameRender(output.rendered, ` |
76 | 38 | Something here |
77 | 39 | <p>Something more here</p> |
78 | 40 | <p>Escape me!</p> |
79 | 41 | `)); |
80 | 42 | } |
81 | 43 |
|
82 | 44 | unittest { |
83 | | - auto resp = new DummyHTTPServerResponse(); |
| 45 | + auto output = new MemoryOutputStream(); |
| 46 | + auto resp = createTestHTTPServerResponse(output); |
| 47 | + auto ctx = new TempleContext; |
| 48 | + ctx.abc = "<unescaped>"; |
| 49 | + ctx.def = "<escaped>"; |
| 50 | + resp.renderTemple!` |
| 51 | + <%= safe(var.abc) %> |
| 52 | + <%= var.def %> |
| 53 | + `(ctx); |
| 54 | + resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream |
| 55 | + |
| 56 | + assert(isSameRender(output.rendered, ` |
| 57 | + <unescaped> |
| 58 | + <escaped> |
| 59 | + `)); |
| 60 | +} |
| 61 | + |
| 62 | +unittest { |
| 63 | + auto output = new MemoryOutputStream(); |
| 64 | + auto resp = createTestHTTPServerResponse(output); |
84 | 65 | resp.renderTempleFile!"test12_vibe1.emd"; |
| 66 | + resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream |
85 | 67 |
|
86 | | - assert(isSameRender(resp.rendered, ` |
| 68 | + assert(isSameRender(output.rendered, ` |
87 | 69 | Rendering with renderTempleFile in temple.vibe |
88 | 70 | <p>Don't escape</p> |
89 | 71 | <p>Do escape</p> |
90 | 72 | `)); |
91 | 73 | } |
92 | 74 |
|
93 | 75 | unittest { |
94 | | - auto resp = new DummyHTTPServerResponse(); |
| 76 | + auto output = new MemoryOutputStream(); |
| 77 | + auto resp = createTestHTTPServerResponse(output); |
95 | 78 | resp.renderTempleLayoutFile!("test13_vibelayout.emd", "test13_vibepartial.emd"); |
| 79 | + resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream |
96 | 80 |
|
97 | | - assert(isSameRender(resp.rendered, ` |
| 81 | + assert(isSameRender(output.rendered, ` |
98 | 82 | <div>escaped header</div> |
99 | 83 | <div>header div</div> |
100 | 84 | header |
|
0 commit comments