/
node-static-test.js
180 lines (176 loc) · 5.75 KB
/
node-static-test.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var vows = require('vows')
, request = require('request')
, assert = require('assert')
, static = require('../../lib/node-static');
var fileServer = new(static.Server)(__dirname + '/../fixtures', {serverInfo: 'custom-server-name'});
var suite = vows.describe('node-static');
var TEST_PORT = 8080;
var TEST_SERVER = 'http://localhost:' + TEST_PORT;
var server;
var callback;
suite.addBatch({
'once an http server is listening with a callback': {
topic: function () {
server = require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response, function(err, result) {
if (callback)
callback(request, response, err, result);
else
request.end();
});
});
}).listen(TEST_PORT, this.callback)
},
'should be listening' : function(){
/* This test is necessary to ensure the topic execution.
* A topic without tests will be not executed */
assert.isTrue(true);
}
},
}).addBatch({
'streaming a 404 page': {
topic: function(){
callback = function(request, response, err, result) {
if (err) {
response.writeHead(err.status, err.headers);
setTimeout(function() {
response.end('Custom 404 Stream.')
}, 100);
}
}
request.get(TEST_SERVER + '/not-found', this.callback);
},
'should respond with 404' : function(error, response, body){
assert.equal(response.statusCode, 404);
},
'should respond with the streamed content': function(error, response, body){
callback = null;
assert.equal(body, 'Custom 404 Stream.');
}
}
}).addBatch({
'once an http server is listening without a callback': {
topic: function () {
server.close();
server = require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response);
});
}).listen(TEST_PORT, this.callback)
},
'should be listening' : function(){
/* This test is necessary to ensure the topic execution.
* A topic without tests will be not executed */
assert.isTrue(true);
}
}
}).addBatch({
'requesting a file not found': {
topic : function(){
request.get(TEST_SERVER + '/not-found', this.callback);
},
'should respond with 404' : function(error, response, body){
assert.equal(response.statusCode, 404);
}
}
}).addBatch({
'serving hello.txt': {
topic : function(){
request.get(TEST_SERVER + '/hello.txt', this.callback);
},
'should respond with 200' : function(error, response, body){
assert.equal(response.statusCode, 200);
},
'should respond with text/plain': function(error, response, body){
assert.equal(response.headers['content-type'], 'text/plain');
},
'should respond with hello world': function(error, response, body){
assert.equal(body, 'hello world');
}
}
}).addBatch({
'serving directory index': {
topic : function(){
request.get(TEST_SERVER, this.callback);
},
'should respond with 200' : function(error, response, body){
assert.equal(response.statusCode, 200);
},
'should respond with text/html': function(error, response, body){
assert.equal(response.headers['content-type'], 'text/html');
}
}
}).addBatch({
'serving index.html from the cache': {
topic : function(){
request.get(TEST_SERVER + '/index.html', this.callback);
},
'should respond with 200' : function(error, response, body){
assert.equal(response.statusCode, 200);
},
'should respond with text/html': function(error, response, body){
assert.equal(response.headers['content-type'], 'text/html');
}
}
}).addBatch({
'requesting with If-None-Match': {
topic : function(){
var _this = this;
request.get(TEST_SERVER + '/index.html', function(error, response, body){
request({
method: 'GET',
uri: TEST_SERVER + '/index.html',
headers: {'if-none-match': response.headers['etag']}
},
_this.callback);
});
},
'should respond with 304' : function(error, response, body){
assert.equal(response.statusCode, 304);
}
},
'requesting with If-None-Match and If-Modified-Since': {
topic : function(){
var _this = this;
request.get(TEST_SERVER + '/index.html', function(error, response, body){
var modified = Date.parse(response.headers['last-modified']);
var oneDayLater = new Date(modified + (24 * 60 * 60 * 1000)).toUTCString();
var nonMatchingEtag = '1111222233334444';
request({
method: 'GET',
uri: TEST_SERVER + '/index.html',
headers: {
'if-none-match': nonMatchingEtag,
'if-modified-since': oneDayLater
}
},
_this.callback);
});
},
'should respond with a 200': function(error, response, body){
assert.equal(response.statusCode, 200);
}
}
}).addBatch({
'requesting HEAD': {
topic : function(){
request.head(TEST_SERVER + '/index.html', this.callback);
},
'should respond with 200' : function(error, response, body){
assert.equal(response.statusCode, 200);
},
'head must has no body' : function(error, response, body){
assert.isUndefined(body);
}
}
}).addBatch({
'requesting headers': {
topic : function(){
request.head(TEST_SERVER + '/index.html', this.callback);
},
'should respond with node-static/0.6.0' : function(error, response, body){
assert.equal(response.headers['server'], 'custom-server-name');
}
}
}).export(module);