1 var server = require('./server')
2 , assert = require('assert')
3 , request = require('../index')
6 var s = server.createServer();
8 s.listen(s.port, function () {
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'});
17 // test get(string, function)
18 request.defaults({headers:{foo:"bar"}})(s.url + '/get', function (e, r, b){
20 assert.deepEqual("TESTING!", b);
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'}));
32 // test post(string, object, function)
33 request.defaults({headers:{foo:"bar"}}).post(s.url + '/post', {json: true}, function (e, r, b){
35 assert.deepEqual('bar', b.foo);
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'}));
47 // test post(string, object, function)
48 request.defaults({headers:{foo:"bar"}}).patch(s.url + '/patch', {json: true}, function (e, r, b){
50 assert.deepEqual('bar', b.foo);
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'}));
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){
65 assert.deepEqual('bar', b.foo);
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'}));
76 // test .del(string, function)
77 request.defaults({headers:{foo:"bar"}, json:true}).del(s.url + '/del', function (e, r, b){
79 assert.deepEqual('bar', b.foo);
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'});
90 // test head.(object, function)
91 request.defaults({headers:{foo:"bar"}}).head({uri: s.url + '/head'}, function (e, r, b){
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'});
103 // test custom request handler function
104 var defaultRequest = request.defaults({
107 }, function(uri, options, callback) {
108 var params = request.initParams(uri, options, callback);
109 options = params.options;
110 options.headers.x = 'y';
112 return request(params.uri, params.options, params.callback);
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);
123 defaultRequest.get(s.url + '/get_custom', function(e, r, b) {
126 console.log(counter.toString() + " tests passed.");