forked from http-party/http-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server-test.js
201 lines (196 loc) · 6.31 KB
/
http-server-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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
var assert = require('assert'),
path = require('path'),
fs = require('fs'),
vows = require('vows'),
request = require('request'),
httpServer = require('../lib/http-server');
var root = path.join(__dirname, 'fixtures', 'root');
vows.describe('http-server').addBatch({
'When http-server is listening on 8080': {
topic: function () {
var server = httpServer.createServer({
root: root,
robots: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true'
}
});
server.listen(8080);
this.callback(null, server);
},
'it should serve files from root directory': {
topic: function () {
request('http://127.0.0.1:8080/file', this.callback);
},
'status code should be 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
topic: function (res, body) {
var self = this;
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
self.callback(err, data, body);
});
},
'should match content of served file': function (err, file, body) {
assert.equal(body.trim(), file.trim());
}
}
},
'when requesting non-existent file': {
topic: function () {
request('http://127.0.0.1:8080/404', this.callback);
},
'status code should be 404': function (res) {
assert.equal(res.statusCode, 404);
}
},
'when requesting /': {
topic: function () {
request('http://127.0.0.1:8080/', this.callback);
},
'should respond with index': function (err, res, body) {
assert.equal(res.statusCode, 200);
assert.include(body, '/file');
assert.include(body, '/canYouSeeMe');
}
},
'when robots options is activated': {
topic: function () {
request('http://127.0.0.1:8080/', this.callback);
},
'should respond with status code 200 to /robots.txt': function (res) {
assert.equal(res.statusCode, 200);
}
},
'and options include custom set http-headers': {
topic: function () {
request('http://127.0.0.1:8080/', this.callback);
},
'should respond with headers set in options': function (err, res, body) {
assert.equal(res.headers['access-control-allow-origin'], '*');
assert.equal(res.headers['access-control-allow-credentials'], 'true');
}
},
'When http-server is proxying from 8081 to 8080': {
topic: function () {
var proxyServer = httpServer.createServer({
proxy: 'http://127.0.0.1:8080/',
root: path.join(__dirname, 'fixtures')
});
proxyServer.listen(8081);
this.callback(null, proxyServer);
},
'it should serve files from the proxy server root directory': {
topic: function () {
request('http://127.0.0.1:8081/root/file', this.callback);
},
'status code should be the enpoint code 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
topic: function (res, body) {
var self = this;
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
self.callback(err, data, body);
});
},
'should match content of the served file': function (err, file, body) {
assert.equal(body.trim(), file.trim());
}
}
},
'it should fallback to the proxied server': {
topic: function () {
request('http://127.0.0.1:8081/file', this.callback);
},
'status code should be the enpoint code 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
topic: function (res, body) {
var self = this;
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
self.callback(err, data, body);
});
},
'should match content of the proxied served file': function (err, file, body) {
assert.equal(body.trim(), file.trim());
}
}
}
}
},
'When cors is enabled': {
topic: function () {
var server = httpServer.createServer({
root: root,
cors: true
});
server.listen(8082);
this.callback(null, server);
},
'and given OPTIONS request': {
topic: function () {
request({
method: 'OPTIONS',
uri: 'http://127.0.0.1:8082/',
headers: {
'Access-Control-Request-Method': 'GET',
Origin: 'http://example.com',
'Access-Control-Request-Headers': 'Foobar'
}
}, this.callback);
},
'status code should be 204': function (err, res, body) {
assert.equal(res.statusCode, 204);
}
}
},
'When using custom mime-types from a .types file': {
topic: function () {
var server = httpServer.createServer({
root: root,
mimeType: 'fixtures/root/mime.types'
});
server.listen(8083, '127.0.0.1', this.callback);
},
'and when asked for a .test file': {
topic: function () {
// give http-server an extra second before requesting
// to not get ECONNREFUSED
var cb = this.callback.bind(this);
setTimeout(function () {
request('http://127.0.0.1:8083/mime.test', cb);
}, 2000);
},
'it should answer with text/prs.test': function (err, res, body) {
assert.isNull(err);
assert.equal(res.headers['content-type'], 'text/prs.test; charset=UTF-8');
}
}
},
'When custom mime-types are added as an option': {
topic: function () {
var server = httpServer.createServer({
root: root,
mimeType: {
'text/prs.test2': ['test']
}
});
server.listen(8084);
return server;
},
'and when asked for a .test file': {
topic: function () {
request('http://127.0.0.1:8084/mime.test', this.callback);
},
'it should answer with text/prs.test2': function (err, res, body) {
assert.isNull(err);
assert.equal(res.headers['content-type'], 'text/prs.test2; charset=UTF-8');
}
}
}
}).export(module);