test-defaults.js 4.2 KB

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