-
Notifications
You must be signed in to change notification settings - Fork 4
/
handle-tag.js
93 lines (86 loc) · 3.06 KB
/
handle-tag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict';
const assert = require('assert');
const http = require('http');
const sinon = require('sinon');
const Tailor = require('../index');
const PassThrough = require('stream').PassThrough;
describe('Handle tag', () => {
let server;
let tailor;
const mockTemplate = sinon.stub();
const mockHandleTag = sinon.stub();
beforeEach(done => {
tailor = new Tailor({
fetchTemplate: (request, parseTemplate) => {
const template = mockTemplate(request);
if (template) {
return parseTemplate(template);
} else {
return Promise.reject('Error fetching template');
}
},
pipeDefinition: () => Buffer.from(''),
handledTags: ['x-tag'],
handleTag: mockHandleTag
});
server = http.createServer(tailor.requestHandler);
server.listen(8080, 'localhost', done);
});
afterEach(done => {
mockTemplate.reset();
mockHandleTag.reset();
server.close(done);
});
it('calls handleTag for a tag in handledTags', done => {
mockTemplate.returns('<x-tag foo="bar"><strong>test</strong></x-tag>');
mockHandleTag.returns('');
http.get('http://localhost:8080/template', response => {
const request = mockHandleTag.args[0][0];
const tag = mockHandleTag.args[0][1];
assert.equal(request.url, '/template');
assert.deepEqual(tag, {
name: 'x-tag',
attributes: {
foo: 'bar'
}
});
response.resume();
response.on('end', done);
});
});
it('replaces the original tag with stream or string content', done => {
const st = new PassThrough();
mockTemplate.returns('<x-tag foo="bar"></x-tag>');
mockHandleTag.onCall(0).returns(st);
mockHandleTag.onCall(1).returns('');
http.get('http://localhost:8080/template', response => {
let data = '';
response.on('data', chunk => (data += chunk));
response.on('end', () => {
assert.equal(
data,
'<html><head></head><body><foo></foo></body></html>'
);
done();
});
});
st.write('<foo>');
st.end('</foo>');
});
it('should let us inject arbitrary html inside the tags', done => {
mockTemplate.returns('<x-tag foo="bar"><div>test</div></x-tag>');
mockHandleTag.onCall(0).returns('<hello>');
mockHandleTag.onCall(1).returns('</hello>');
http.get('http://localhost:8080/template', response => {
let data = '';
response.on('data', chunk => (data += chunk));
response.on('end', () => {
assert.equal(
data,
'<html><head></head><body><hello><div>test</div></hello></body></html>'
);
done();
});
});
});
});