Skip to content

Commit 10a4120

Browse files
committed
Merge branch 'Yoplitein-vibe724fix' into develop
2 parents f1b304d + b03ab41 commit 10a4120

File tree

8 files changed

+61
-60
lines changed

8 files changed

+61
-60
lines changed

dub.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"dependencies": {
1111
"vibe-d": {
12-
"version": "~master",
12+
"version": ">=0.7.21",
1313
"optional": true
1414
}
1515
},
@@ -35,7 +35,7 @@
3535
{
3636
"name": "unittest-vibed",
3737
"dependencies": {
38-
"vibe-d": "~master"
38+
"vibe-d": ">=0.7.21"
3939
},
4040
"targetType": "executable",
4141
"mainSourceFile": "src/app.d",

src/temple/package.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public:
212212
private import vibe.stream.wrapper : StreamOutputRange;
213213

214214
void render(vibe.core.stream.OutputStream os, TempleContext tc = null) {
215-
static assert(isOutputRange!(vibe.core.stream.OutputStream, string));
215+
static assert(isOutputRange!(vibe.stream.wrapper.StreamOutputRange, string));
216216

217217
auto sor = StreamOutputRange(os);
218218
this.render(sor, tc);

src/temple/tests/capture.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module temple.tests.capture;
22

3+
version(TempleUnittest):
4+
35
import temple.tests.common;
46
unittest
57
{

src/temple/tests/common.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module temple.tests.common;
22

3-
version(unittest):
3+
4+
version(TempleUnittest):
5+
46
public import std.stdio, std.file : readText;
57
public import
68
temple,

src/temple/tests/filter.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module temple.tests.filter;
22

3+
version(TempleUnittest):
4+
35
import temple.tests.common;
46

57
private struct SafeDemoFilter

src/temple/tests/todos.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module temple.tests.todos;
22

3+
version(TempleUnittest):
4+
35
/**
46
* Use cases that can hopefully be made to work at some time in the future.
57
* These might not work for a number of reasons, such as CTFE, Phobos, DMD, or

src/temple/tests/vibe.d

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,84 @@
11
module temple.tests.vibe;
22

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):
104
version(Have_vibe_d):
115

126
private {
137
import temple.tests.common;
148
import vibe.http.server;
159
import vibe.core.stream;
10+
import vibe.stream.memory;
1611
import core.time;
1712
}
1813

1914
/*
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
4416
*/
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;
5719

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 .. $];
6123

62-
string rendered() {
63-
return appender.data();
64-
}
24+
return lines.join("\n");
6525
}
6626

6727
unittest {
68-
auto resp = new DummyHTTPServerResponse();
28+
auto output = new MemoryOutputStream();
29+
auto resp = createTestHTTPServerResponse(output);
6930
resp.renderTemple!`
7031
Something here
7132
<p>Something more here</p>
7233
<%= "<p>Escape me!</p>" %>
7334
`;
35+
resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream
7436

75-
assert(isSameRender(resp.rendered, `
37+
assert(isSameRender(output.rendered, `
7638
Something here
7739
<p>Something more here</p>
7840
&lt;p&gt;Escape me!&lt;/p&gt;
7941
`));
8042
}
8143

8244
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+
&lt;escaped&gt;
59+
`));
60+
}
61+
62+
unittest {
63+
auto output = new MemoryOutputStream();
64+
auto resp = createTestHTTPServerResponse(output);
8465
resp.renderTempleFile!"test12_vibe1.emd";
66+
resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream
8567

86-
assert(isSameRender(resp.rendered, `
68+
assert(isSameRender(output.rendered, `
8769
Rendering with renderTempleFile in temple.vibe
8870
<p>Don't escape</p>
8971
&lt;p&gt;Do escape&lt;/p&gt;
9072
`));
9173
}
9274

9375
unittest {
94-
auto resp = new DummyHTTPServerResponse();
76+
auto output = new MemoryOutputStream();
77+
auto resp = createTestHTTPServerResponse(output);
9578
resp.renderTempleLayoutFile!("test13_vibelayout.emd", "test13_vibepartial.emd");
79+
resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream
9680

97-
assert(isSameRender(resp.rendered, `
81+
assert(isSameRender(output.rendered, `
9882
&lt;div&gt;escaped header&lt;/div&gt;
9983
<div>header div</div>
10084
header

src/temple/vibe.d

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private {
99
import vibe.http.server;
1010
import vibe.textfilter.html;
1111
import std.stdio;
12+
import std.variant;
1213
}
1314

1415
struct TempleHtmlFilter {
@@ -21,13 +22,21 @@ struct TempleHtmlFilter {
2122
filterHTMLEscape(stream, unsafe);
2223
}
2324

25+
static void temple_filter(ref TempleOutputStream stream, Variant variant) {
26+
temple_filter(stream, variant.toString);
27+
}
28+
2429
static string temple_filter(SafeString safe) {
2530
return safe.payload;
2631
}
2732

2833
static SafeString safe(string str) {
2934
return SafeString(str);
3035
}
36+
37+
static SafeString safe(Variant variant) {
38+
return SafeString(variant.toString);
39+
}
3140
}
3241

3342
private enum SetupContext = q{
@@ -50,7 +59,7 @@ void renderTemple(string temple, Ctx = TempleContext)
5059
{
5160
mixin(SetupContext);
5261

53-
auto t = Temple!(temple, TempleHtmlFilter);
62+
auto t = compile_temple!(temple, TempleHtmlFilter);
5463
t.render(res.bodyWriter, context);
5564
}
5665

0 commit comments

Comments
 (0)