Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / testswarm / node_modules / request / tests / test-defaults.js
1 var server = require('./server')
2   , assert = require('assert')
3   , request = require('../main.js')
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('/post-body', function (req, resp) {
40     assert.equal(req.headers.foo, 'bar');
41     assert.equal(req.headers['content-type'], 'application/json');
42     assert.equal(req.method, 'POST')
43     resp.writeHead(200, {'Content-Type': 'application/json'});
44     resp.end(JSON.stringify({foo:'bar'}));
45   });
46
47   // test post(string, object, function) with body
48   request.defaults({headers:{foo:"bar"}}).post(s.url + '/post-body', {json: true, body:{bar:"baz"}}, function (e, r, b){
49     if (e) throw e;
50     assert.deepEqual('bar', b.foo);
51     counter += 1;
52   });
53
54   s.on('/del', function (req, resp) {
55     assert.equal(req.headers.foo, 'bar');
56     assert.equal(req.method, 'DELETE')
57     resp.writeHead(200, {'Content-Type': 'application/json'});
58     resp.end(JSON.stringify({foo:'bar'}));
59   });
60
61   // test .del(string, function)
62   request.defaults({headers:{foo:"bar"}, json:true}).del(s.url + '/del', function (e, r, b){
63     if (e) throw e;
64     assert.deepEqual('bar', b.foo);
65     counter += 1;
66   });
67
68   s.on('/head', function (req, resp) {
69     assert.equal(req.headers.foo, 'bar');
70     assert.equal(req.method, 'HEAD')
71     resp.writeHead(200, {'Content-Type': 'text/plain'});
72     resp.end();
73   });
74
75   // test head.(object, function)
76   request.defaults({headers:{foo:"bar"}}).head({uri: s.url + '/head'}, function (e, r, b){
77     if (e) throw e;
78     counter += 1;
79   });
80
81   s.on('/get_custom', function(req, resp) {
82     assert.equal(req.headers.foo, 'bar');
83     assert.equal(req.headers.x, 'y');
84     resp.writeHead(200, {'Content-Type': 'text/plain'});
85     resp.end();
86   });
87
88   // test custom request handler function
89   var defaultRequest = request.defaults({
90     headers:{foo:"bar"}
91     , body: 'TESTING!'
92   }, function(uri, options, callback) {
93     var params = request.initParams(uri, options, callback);
94     options = params.options;
95     options.headers.x = 'y';
96
97     return request(params.uri, params.options, params.callback);
98   });
99
100   var msg = 'defaults test failed. head request should throw earlier';
101   assert.throws(function() {
102     defaultRequest.head(s.url + '/get_custom', function(e, r, b) {
103       throw new Error(msg);
104     });
105     counter+=1;
106   }, msg);
107
108   defaultRequest.get(s.url + '/get_custom', function(e, r, b) {
109     if(e) throw e;
110     counter += 1;
111     console.log(counter.toString() + " tests passed.");
112     s.close();
113   });
114 })