UNPKG

6.14 kBJavaScriptView Raw
1var assert = require('assert'),
2 nunjucks = require('nunjucks');
3
4var TemplatingMiddleware = require('../lib/TemplatingMiddleware');
5
6var MockRequest = require('./utils/MockRequest'),
7 MockResponse = require('./utils/MockResponse');
8
9var loader = new nunjucks.FileSystemLoader('./test/contents/test.ch/templates'),
10 env = new nunjucks.Environment(loader, {tags: {variableStart: '{$', variableEnd: '$}'}, dev: true});
11
12var container = {
13 get: function(key){
14 return env;
15 }
16};
17
18describe('Middleware', function(){
19 describe('request', function(){
20
21 var middleware = new TemplatingMiddleware(container);
22
23 describe('text/html', function(){
24 var request = new MockRequest('test.ch', 'test.nunjuck.html',
25 {'accept': [
26 {key:'text', value: 'html'}
27 ]}
28 );
29
30 var requestLanguage = new MockRequest('test.ch', 'test.language.nunjuck.html',
31 {'accept': [
32 {key:'text', value: 'html'}
33 ]}
34 );
35
36 var response = new MockResponse();
37 var responseLanguage = new MockResponse();
38
39 middleware.request(request, response, function(){
40 it('should append a rendering method to the response', function(){
41 assert('render' in response);
42 });
43
44 response.render(200, 'en', {}, {ciao: 'Hallo'}, function(err){
45 it('which resolves the template and renders it', function(){
46 assert.equal(err, null);
47 assert.equal('Hallo: Test succeeded.', response.data);
48 });
49
50 it('should set the content type and the status correctly', function(){
51 assert.equal('text/html; charset=utf8', response.contentType);
52 assert.equal(200, response.status);
53 });
54 });
55 });
56
57 middleware.request(requestLanguage, responseLanguage, function(){
58 it('should append a rendering method to the response', function(){
59 assert('render' in responseLanguage);
60 });
61
62 responseLanguage.render(200, 'it', {}, {ciao: 'Hallo'}, function(err){
63 it('which resolves the template and renders it with the language available', function(){
64 assert.equal(err, null);
65 assert.equal('Hallo: Test succeeded in it.', responseLanguage.data);
66 });
67
68 it('should set the content type and the status correctly', function(){
69 assert.equal('text/html; charset=utf8', responseLanguage.contentType);
70 assert.equal(200, responseLanguage.status);
71 });
72
73 it('should set the content language correctly', function(){
74 assert.equal('it', responseLanguage.getHeader('content-language'));
75 });
76 });
77 });
78 });
79
80 describe('application/json', function(){
81 var request1 = new MockRequest('test.ch', 'test.nunjuck.html',
82 {'accept': [
83 {key:'application', value: 'json'},
84 {key:'text', value: 'html'}
85 ]}
86 );
87
88 var response1 = new MockResponse();
89
90 middleware.request(request1, response1, function(){
91 response1.render(200, 'en', {}, {test: 'succeeded'}, function(err){
92 it('if the request has an accept value of application/json it should render it as json, and ignore the template', function(){
93 assert.equal(err, null);
94 assert.equal('{"test":"succeeded"}', response1.data);
95 });
96 it('should set the content type and the status correctly', function(){
97 assert.equal('application/json; charset=utf8', response1.contentType);
98 assert.equal(200, response1.status);
99 });
100 });
101 });
102 });
103
104 describe('text/plain', function(){
105 var request1 = new MockRequest('test.ch', 'test.nunjuck.html',
106 {'accept': [
107 {key:'text', value: 'plain'}
108 ]}
109 );
110
111 var response1 = new MockResponse();
112
113 middleware.request(request1, response1, function(){
114 response1.render(200, 'en', {}, {test: 'succeeded'}, function(err){
115 it('if the request has another accept type it should fallback to json', function(){
116 assert(err==null);
117 assert.equal('{"test":"succeeded"}', response1.data);
118 });
119
120 it('should set the content type and the status correctly', function(){
121 assert.equal('application/json; charset=utf8', response1.contentType);
122 assert.equal(200, response1.status);
123 });
124 });
125 });
126 });
127
128 describe('on errors', function(){
129 var request1 = new MockRequest('test.ch', 'test.nunjuck.inexistent.html',
130 {'accept': [
131 {key:'text', value: 'html'}
132 ]}
133 );
134
135 var response1 = new MockResponse();
136
137 middleware.request(request1, response1, function(){
138 response1.render(200, 'en', {}, {test: 'succeeded'}, function(err){
139 it('if there happens an error during the rendering it is passed to the callback', function(){
140 assert(!!err);
141 });
142 it('on error the response is equivalent to a server error', function(){
143 assert.equal(response1.status, 500);
144 });
145 });
146 });
147 });
148 });
149});
\No newline at end of file