0bf59a04ad0c746987a35446dc3921fc15201e0f.svn-base
1.94 KB
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
var server = require('./server')
, assert = require('assert')
, request = require('../main.js')
;
var s = server.createServer();
s.listen(s.port, function () {
var counter = 0;
s.on('/get', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.method, 'GET')
resp.writeHead(200, {'Content-Type': 'text/plain'});
resp.end('TESTING!');
});
// test get(string, function)
request.defaults({headers:{foo:"bar"}})(s.url + '/get', function (e, r, b){
if (e) throw e;
assert.deepEqual("TESTING!", b);
counter += 1;
});
s.on('/post', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.headers['content-type'], 'application/json');
assert.equal(req.method, 'POST')
resp.writeHead(200, {'Content-Type': 'application/json'});
resp.end(JSON.stringify({foo:'bar'}));
});
// test post(string, object, function)
request.defaults({headers:{foo:"bar"}}).post(s.url + '/post', {json: true}, function (e, r, b){
if (e) throw e;
assert.deepEqual('bar', b.foo);
counter += 1;
});
s.on('/del', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.method, 'DELETE')
resp.writeHead(200, {'Content-Type': 'application/json'});
resp.end(JSON.stringify({foo:'bar'}));
});
// test .del(string, function)
request.defaults({headers:{foo:"bar"}, json:true}).del(s.url + '/del', function (e, r, b){
if (e) throw e;
assert.deepEqual('bar', b.foo);
counter += 1;
});
s.on('/head', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.method, 'HEAD')
resp.writeHead(200, {'Content-Type': 'text/plain'});
resp.end();
});
// test head.(object, function)
request.defaults({headers:{foo:"bar"}}).head({uri: s.url + '/head'}, function (e, r, b){
if (e) throw e;
counter += 1;
console.log(counter.toString() + " tests passed.")
s.close()
});
})