forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-defaults.js
More file actions
129 lines (111 loc) · 3.85 KB
/
Copy pathtest-defaults.js
File metadata and controls
129 lines (111 loc) · 3.85 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
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
var server = require('./server')
, assert = require('assert')
, request = require('../index')
;
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'], null);
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('/patch', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.headers['content-type'], null);
assert.equal(req.method, 'PATCH')
resp.writeHead(200, {'Content-Type': 'application/json'});
resp.end(JSON.stringify({foo:'bar'}));
});
// test post(string, object, function)
request.defaults({headers:{foo:"bar"}}).patch(s.url + '/patch', {json: true}, function (e, r, b){
if (e) throw e;
assert.deepEqual('bar', b.foo);
counter += 1;
});
s.on('/post-body', 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) with body
request.defaults({headers:{foo:"bar"}}).post(s.url + '/post-body', {json: true, body:{bar:"baz"}}, 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;
});
s.on('/get_custom', function(req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.headers.x, 'y');
resp.writeHead(200, {'Content-Type': 'text/plain'});
resp.end();
});
// test custom request handler function
var defaultRequest = request.defaults({
headers:{foo:"bar"}
, body: 'TESTING!'
}, function(uri, options, callback) {
var params = request.initParams(uri, options, callback);
options = params.options;
options.headers.x = 'y';
return request(params.uri, params.options, params.callback);
});
var msg = 'defaults test failed. head request should throw earlier';
assert.throws(function() {
defaultRequest.head(s.url + '/get_custom', function(e, r, b) {
throw new Error(msg);
});
counter+=1;
}, msg);
defaultRequest.get(s.url + '/get_custom', function(e, r, b) {
if(e) throw e;
counter += 1;
console.log(counter.toString() + " tests passed.");
s.close();
});
})