1 module temple.tests.vibe; 2 3 version(TempleUnittest): 4 version(Have_vibe_d): 5 6 private { 7 import temple.tests.common; 8 import vibe.http.server; 9 import vibe.core.stream; 10 import vibe.stream.memory; 11 import core.time; 12 } 13 14 /* 15 * Drops HTTP headers from the stream output and uses proper newlines 16 */ 17 private string rendered(MemoryOutputStream output) { 18 import std.string: split, join; 19 20 string data = cast(string)output.data; 21 string[] lines = data.split("\r\n"); 22 lines = lines[4 .. $]; 23 24 return lines.join("\n"); 25 } 26 27 unittest { 28 auto output = new MemoryOutputStream(); 29 auto resp = createTestHTTPServerResponse(output); 30 resp.renderTemple!` 31 Something here 32 <p>Something more here</p> 33 <%= "<p>Escape me!</p>" %> 34 `; 35 resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream 36 37 assert(isSameRender(output.rendered, ` 38 Something here 39 <p>Something more here</p> 40 <p>Escape me!</p> 41 `)); 42 } 43 44 unittest { 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); 65 resp.renderTempleFile!"test12_vibe1.emd"; 66 resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream 67 68 assert(isSameRender(output.rendered, ` 69 Rendering with renderTempleFile in temple.vibe 70 <p>Don't escape</p> 71 <p>Do escape</p> 72 `)); 73 } 74 75 unittest { 76 auto output = new MemoryOutputStream(); 77 auto resp = createTestHTTPServerResponse(output); 78 resp.renderTempleLayoutFile!("test13_vibelayout.emd", "test13_vibepartial.emd"); 79 resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream 80 81 assert(isSameRender(output.rendered, ` 82 <div>escaped header</div> 83 <div>header div</div> 84 header 85 <span>partial</span> 86 <p>Escaped paragraph in partial</p> 87 footer 88 <div>footer div</div> 89 `)); 90 }