fs: report correct path when EEXIST
[platform/upstream/nodejs.git] / deps / npm / node_modules / request / tests / test-defaults.js
1 var server = require('./server')
2   , assert = require('assert')
3   , request = require('../index')
4   ;
5
6 var s = server.createServer();
7
8 s.listen(s.port, function () {
9   var counter = 0;
10   s.on('/get', function (req, resp) {
11     assert.equal(req.headers.foo, 'bar');
12     assert.equal(req.method, 'GET')
13     resp.writeHead(200, {'Content-Type': 'text/plain'});
14     resp.end('TESTING!');
15   });
16
17   // test get(string, function)
18   request.defaults({headers:{foo:"bar"}})(s.url + '/get', function (e, r, b){
19     if (e) throw e;
20     assert.deepEqual("TESTING!", b);
21     counter += 1;
22   });
23
24   s.on('/post', function (req, resp) {
25     assert.equal(req.headers.foo, 'bar');
26     assert.equal(req.headers['content-type'], null);
27     assert.equal(req.method, 'POST')
28     resp.writeHead(200, {'Content-Type': 'application/json'});
29     resp.end(JSON.stringify({foo:'bar'}));
30   });
31
32   // test post(string, object, function)
33   request.defaults({headers:{foo:"bar"}}).post(s.url + '/post', {json: true}, function (e, r, b){
34     if (e) throw e;
35     assert.deepEqual('bar', b.foo);
36     counter += 1;
37   });
38
39   s.on('/patch', function (req, resp) {
40     assert.equal(req.headers.foo, 'bar');
41     assert.equal(req.headers['content-type'], null);
42     assert.equal(req.method, 'PATCH')
43     resp.writeHead(200, {'Content-Type': 'application/json'});
44     resp.end(JSON.stringify({foo:'bar'}));
45   });
46
47   // test post(string, object, function)
48   request.defaults({headers:{foo:"bar"}}).patch(s.url + '/patch', {json: true}, function (e, r, b){
49     if (e) throw e;
50     assert.deepEqual('bar', b.foo);
51     counter += 1;
52   });
53
54   s.on('/post-body', function (req, resp) {
55     assert.equal(req.headers.foo, 'bar');
56     assert.equal(req.headers['content-type'], 'application/json');
57     assert.equal(req.method, 'POST')
58     resp.writeHead(200, {'Content-Type': 'application/json'});
59     resp.end(JSON.stringify({foo:'bar'}));
60   });
61
62   // test post(string, object, function) with body
63   request.defaults({headers:{foo:"bar"}}).post(s.url + '/post-body', {json: true, body:{bar:"baz"}}, function (e, r, b){
64     if (e) throw e;
65     assert.deepEqual('bar', b.foo);
66     counter += 1;
67   });
68
69   s.on('/del', function (req, resp) {
70     assert.equal(req.headers.foo, 'bar');
71     assert.equal(req.method, 'DELETE')
72     resp.writeHead(200, {'Content-Type': 'application/json'});
73     resp.end(JSON.stringify({foo:'bar'}));
74   });
75
76   // test .del(string, function)
77   request.defaults({headers:{foo:"bar"}, json:true}).del(s.url + '/del', function (e, r, b){
78     if (e) throw e;
79     assert.deepEqual('bar', b.foo);
80     counter += 1;
81   });
82
83   s.on('/head', function (req, resp) {
84     assert.equal(req.headers.foo, 'bar');
85     assert.equal(req.method, 'HEAD')
86     resp.writeHead(200, {'Content-Type': 'text/plain'});
87     resp.end();
88   });
89
90   // test head.(object, function)
91   request.defaults({headers:{foo:"bar"}}).head({uri: s.url + '/head'}, function (e, r, b){
92     if (e) throw e;
93     counter += 1;
94   });
95
96   s.on('/get_custom', function(req, resp) {
97     assert.equal(req.headers.foo, 'bar');
98     assert.equal(req.headers.x, 'y');
99     resp.writeHead(200, {'Content-Type': 'text/plain'});
100     resp.end();
101   });
102
103   // test custom request handler function
104   var defaultRequest = request.defaults({
105     headers:{foo:"bar"}
106     , body: 'TESTING!'
107   }, function(uri, options, callback) {
108     var params = request.initParams(uri, options, callback);
109     options = params.options;
110     options.headers.x = 'y';
111
112     return request(params.uri, params.options, params.callback);
113   });
114
115   var msg = 'defaults test failed. head request should throw earlier';
116   assert.throws(function() {
117     defaultRequest.head(s.url + '/get_custom', function(e, r, b) {
118       throw new Error(msg);
119     });
120     counter+=1;
121   }, msg);
122
123   defaultRequest.get(s.url + '/get_custom', function(e, r, b) {
124     if(e) throw e;
125     counter += 1;
126     console.log(counter.toString() + " tests passed.");
127     s.close();
128   });
129 })