1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
'use strict';
var assert = require('assert');
var test = require('testit');
var Promise = require('promise');
test('./lib/handle-qs.js', function () {
var handleQs = require('../lib/handle-qs.js');
assert(handleQs('http://example.com/', {}) === 'http://example.com/');
assert(handleQs('http://example.com/?foo=bar', {}) === 'http://example.com/?foo=bar');
assert(handleQs('http://example.com/', {foo: 'bar'}) === 'http://example.com/?foo=bar');
assert(handleQs('http://example.com/', {foo: {bar: 'baz'}}) === 'http://example.com/?foo%5Bbar%5D=baz');
assert(handleQs('http://example.com/', {foo: 'bar', bing: 'bong'}) === 'http://example.com/?foo=bar&bing=bong');
assert(handleQs('http://example.com/?foo=bar', {bing: 'bong'}) === 'http://example.com/?foo=bar&bing=bong');
assert(handleQs('http://example.com/?foo=bar#ding', {bing: 'bong'}) === 'http://example.com/?foo=bar&bing=bong#ding');
});
require('./browser.js');
require('./server.js');
function testEnv(env) {
var request = require(env === 'browser' ? '../browser.js' : '../index.js');
test(env + ' - GET', function () {
return request('GET', 'http://example.com').then(function (res) {
assert(res.statusCode === 200);
assert(res.headers['foo'] === 'bar');
assert(res.body.toString() === 'body');
});
});
test(env + ' - GET query', function () {
return request('GET', 'http://example.com', {qs: {foo: 'baz'}}).then(function (res) {
assert(res.statusCode === 200);
assert(res.headers['foo'] === 'baz');
assert(res.body.toString() === 'body');
});
});
test(env + ' - GET -> .getBody("utf8")', function () {
return request('GET', 'http://example.com').getBody('utf8').then(function (body) {
assert(body === 'body');
});
});
test(env + ' - POST json', function () {
return request('POST', 'http://example.com', {json: {foo: 'baz'}}).then(function (res) {
assert(res.statusCode === 200);
assert(res.body.toString() === 'json body');
});
});
test(env + ' - invalid method', function () {
return request({}, 'http://example.com').then(function (res) {
throw new Error('Expected an error');
}, function (err) {
assert(err instanceof TypeError);
});
});
test(env + ' - invalid url', function () {
return request('GET', {}).then(function (res) {
throw new Error('Expected an error');
}, function (err) {
assert(err instanceof TypeError);
});
});
test(env + ' - invalid options', function () {
return request('GET', 'http://example.com', 'options').then(function (res) {
throw new Error('Expected an error');
}, function (err) {
assert(err instanceof TypeError);
});
});
test(env + ' - Callbacks', function () {
return new Promise(function (resolve, reject) {
return request('GET', 'http://example.com', function (err, res) {
if (err) return reject(err);
assert(res.statusCode === 200);
assert(res.headers['foo'] === 'bar');
assert(res.body.toString() === 'body');
resolve(null);
});
});
});
test(env + ' - Callbacks', function () {
return new Promise(function (resolve, reject) {
return request('GET', 'http://example.com', {}, function (err, res) {
if (err) return reject(err);
assert(res.statusCode === 200);
assert(res.headers['foo'] === 'bar');
assert(res.body.toString() === 'body');
resolve(null);
});
});
});
}
testEnv('browser');
testEnv('server');
|