Name: js-handler/node_modules/nodeunit/test/test-httputil.js 
1:
var nodeunit = require('../lib/nodeunit');
2:
var httputil = require('../lib/utils').httputil;
3:
 
4:
exports.testHttpUtilBasics = function (test) {
5:
 
6:
    test.expect(6);
7:
    
8:
    httputil(function (req, resp) {
9:
        test.equal(req.method, 'PUT');
10:
        test.equal(req.url, '/newpair');
11:
        test.equal(req.headers.foo, 'bar');
12:
        
13:
        resp.writeHead(500, {'content-type': 'text/plain'});
14:
        resp.end('failed');
15:
    }, function (server, client) {
16:
        client.fetch('PUT', '/newpair', {'foo': 'bar'}, function (resp) {
17:
            test.equal(resp.statusCode, 500);
18:
            test.equal(resp.headers['content-type'], 'text/plain');
19:
            test.equal(resp.body, 'failed');            
20:
              
21:
            server.close();
22:
            test.done();
23:
        });
24:
    });
25:
};
26:
 
27:
exports.testHttpUtilJsonHandling = function (test) {
28:
 
29:
    test.expect(9);
30:
    
31:
    httputil(function (req, resp) {
32:
        test.equal(req.method, 'GET');
33:
        test.equal(req.url, '/');
34:
        test.equal(req.headers.foo, 'bar');
35:
        
36:
        var testdata = {foo1: 'bar', foo2: 'baz'};
37:
        
38:
        resp.writeHead(200, {'content-type': 'application/json'});
39:
        resp.end(JSON.stringify(testdata));
40:
        
41:
    }, function (server, client) {
42:
        client.fetch('GET', '/', {'foo': 'bar'}, function (resp) {
43:
            test.equal(resp.statusCode, 200);
44:
            test.equal(resp.headers['content-type'], 'application/json');
45:
            
46:
            test.ok(resp.bodyAsObject);
47:
            test.equal(typeof resp.bodyAsObject, 'object');
48:
            test.equal(resp.bodyAsObject.foo1, 'bar');
49:
            test.equal(resp.bodyAsObject.foo2, 'baz');
50:
            
51:
            server.close();
52:
            test.done();
53:
        });
54:
    });
55:
};