forked from http-party/http-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server-test.js
70 lines (68 loc) · 2.21 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
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,
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');
}
},
'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');
}
}
}
}).export(module);