PageRenderTime 71ms CodeModel.GetById 21ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/node_modules/request/tests/test-defaults.js

https://bitbucket.org/gagginaspinnata/todo-app-with-angularjs
JavaScript | 99 lines | 80 code | 14 blank | 5 comment | 5 complexity | b50d7b779fa8db3aae39eecf6179bee5 MD5 | raw file
Possible License(s): Apache-2.0, MIT
 1var server = require('./server')
 2  , assert = require('assert')
 3  , request = require('../main.js')
 4  ;
 5
 6var s = server.createServer();
 7
 8s.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'], 'application/json');
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('/del', function (req, resp) {
40    assert.equal(req.headers.foo, 'bar');
41    assert.equal(req.method, 'DELETE')
42    resp.writeHead(200, {'Content-Type': 'application/json'});
43    resp.end(JSON.stringify({foo:'bar'}));
44  });
45
46  // test .del(string, function)
47  request.defaults({headers:{foo:"bar"}, json:true}).del(s.url + '/del', function (e, r, b){
48    if (e) throw e;
49    assert.deepEqual('bar', b.foo);
50    counter += 1;
51  });
52
53  s.on('/head', function (req, resp) {
54    assert.equal(req.headers.foo, 'bar');
55    assert.equal(req.method, 'HEAD')
56    resp.writeHead(200, {'Content-Type': 'text/plain'});
57    resp.end();
58  });
59
60  // test head.(object, function)
61  request.defaults({headers:{foo:"bar"}}).head({uri: s.url + '/head'}, function (e, r, b){
62    if (e) throw e;
63    counter += 1;
64  });
65
66  s.on('/get_custom', function(req, resp) {
67    assert.equal(req.headers.foo, 'bar');
68    assert.equal(req.headers.x, 'y');
69    resp.writeHead(200, {'Content-Type': 'text/plain'});
70    resp.end();
71  });
72
73  // test custom request handler function
74  var defaultRequest = request.defaults({
75    headers:{foo:"bar"}
76    , body: 'TESTING!'
77  }, function(uri, options, callback) {
78    var params = request.initParams(uri, options, callback);
79    options = params.options;
80    options.headers.x = 'y';
81
82    return request(params.uri, params.options, params.callback);
83  });
84
85  var msg = 'defaults test failed. head request should throw earlier';
86  assert.throws(function() {
87    defaultRequest.head(s.url + '/get_custom', function(e, r, b) {
88      throw new Error(msg);
89    });
90    counter+=1;
91  }, msg);
92
93  defaultRequest.get(s.url + '/get_custom', function(e, r, b) {
94    if(e) throw e;
95    counter += 1;
96    console.log(counter.toString() + " tests passed.");
97    s.close();
98  });
99})