forked from oJshua/browser-request
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
152 lines (139 loc) · 3.38 KB
/
test.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var test = require('tape');
var request = require('./');
test('try a normal GET', function(t) {
var url = 'https://raw.githubusercontent.com/iriscouch/browser-request/master/package.json';
request(url, function(err, resp) {
t.equal(resp.statusCode, 200);
t.equal(!!resp.body.match(/browser-request/), true);
t.end();
});
});
test('try a normal POST', function(t) {
var url = 'http://httpbin.org/post';
request({
url: url,
method: 'POST',
body: {
string: 'data',
boolean: true,
number: 999,
object: {
foo: 'bar'
},
array: [1, 2, 3],
float: 0.2
}
}, function(err, resp, body) {
t.equal(resp.statusCode, 200);
t.equal(!!resp.body.match(/object Object/), true);
t.end();
});
});
test('try a normal PUT', function(t) {
var url = 'http://httpbin.org/put';
request({
url: url,
method: 'PUT',
body: {
string: 'data',
boolean: true,
number: 999,
object: {
foo: 'bar'
},
array: [1, 2, 3],
float: 0.2
}
}, function(err, resp, body) {
t.equal(resp.statusCode, 200);
t.equal(!!resp.body.match(/object Object/), true);
t.end();
});
});
test('try a normal DELETE', function(t) {
var url = 'http://httpbin.org/delete';
request({
url: url,
method: 'DELETE',
body: {
string: 'data',
boolean: true,
number: 999,
object: {
foo: 'bar'
},
array: [1, 2, 3],
float: 0.2
}
}, function(err, resp) {
t.equal(resp.statusCode, 200);
t.equal(!!resp.body.match(/object Object/), true);
t.end();
});
});
test('try a 404 GET', function(t) {
var url = 'http://httpbin.org/status/404';
request(url, function(err, resp) {
t.equal(resp.statusCode, 0);
t.end();
});
});
test('try a 500 GET', function(t) {
var url = 'http://httpbin.org/status/500';
request(url, function(err, resp) {
t.equal(resp.statusCode, 0);
t.end();
});
});
test('try a 202 GET', function(t) {
var url = 'http://httpbin.org/status/202';
request(url, function(err, resp) {
t.equal(resp.statusCode, 202);
t.end();
});
});
test('try a followRedirect request', function(t) {
var url = 'http://httpbin.org/redirect/1';
request({
url: url,
json: true,
followRedirect: true
}, function(err, resp, body) {
t.equal(body.url, "http://httpbin.org/get");
t.end();
});
});
test('try a CORS GET', function(t) {
var url = 'https://www.googleapis.com/plus/v1/activities';
request(url, function(err, resp) {
t.equal(resp.statusCode, 400);
t.equal(!!resp.body.match(/Required parameter/), true);
t.end();
});
});
test('blob true', function(t) {
var url = 'https://placeholdit.imgix.net/~text?txtsize=19&bg=000000&txtclr=ffffff&txt=200%C3%97200&w=200&h=200';
request({
url: url,
blob: true
}, function(err, resp, blob) {
t.equal(resp.statusCode, 200);
t.equal(!!blob.type.match(/^image/), true);
t.end();
});
});
test('try to abort a GET', function(t) {
var url = './package.json';
var req = request(url, function(err) {
t.equal(err.toString(), 'Error: Network connection error');
t.end();
});
req.abort();
});
test('try to abort a CORS GET', function(t) {
var url = 'http://httpbin.org/get';
request(url, function(err) {
t.equal(err.toString(), 'Error: CORS request rejected: ' + url);
t.end();
}).abort();
});