forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.meta.js
52 lines (44 loc) · 1.14 KB
/
http.meta.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
var http = require('http');
var server = http.createServer(function(req, res){
var accept = req.headers.accept || ''
, json = ~accept.indexOf('json');
switch (req.url) {
case '/':
res.end('hello');
break;
case '/users':
if (json) {
res.end('["tobi","loki","jane"]');
} else {
res.end('tobi, loki, jane');
}
break;
}
})
server.listen(8889);
function get(url, body, header) {
return function(done){
http.get({ path: url, port: 8889, headers: header }, function(res){
var buf = '';
res.should.have.status(200);
res.setEncoding('utf8');
res.on('data', function(chunk){ buf += chunk });
res.on('end', function(){
buf.should.equal(body);
done();
});
})
}
}
describe('http requests', function(){
describe('GET /', function(){
it('should respond with hello',
get('/', 'hello'))
})
describe('GET /users', function(){
it('should respond with users',
get('/users', 'tobi, loki, jane'))
it('should respond with users',
get('/users', '["tobi","loki","jane"]', { Accept: 'application/json' }))
})
})