forked from lightsofapollo/superagent-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_test.js
191 lines (159 loc) · 4.97 KB
/
index_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var assert = require('assert');
var Promise = require('es6-promise').Promise
var request = require('./')(require('superagent'), Promise);
var http = require('http');
var debug = require('debug')('test:index');
function respondWith(res, status, body) {
res.writeHead(status, {
'Content-Type': 'text/plain',
'Content-Length': body.length
});
res.end(body);
}
describe('superagent-promise', function() {
// start the server
var server;
var baseURL;
var successBody = 'woot';
var errorBody = 'Not Found';
var connections = {};
before(function(done) {
server = http.createServer(function(req, res) {
if (/success$/.test(req.url)) {
debug('Responding with 200');
respondWith(res, 200, successBody);
return;
} else if(/NotFound$/.test(req.url)) {
debug('Responding with 404');
respondWith(res, 404, errorBody);
} else if(/error$/.test(req.url)) {
debug('Responding with 200, but mismatching Content-Length');
res.writeHead(200, {
'Content-Length': successBody.length - 2,
'Content-Type': 'text/plain'
});
res.end(successBody);
} else if (/redirect/.test(req.url)) {
debug('Responding with a 302 redirect');
var url = baseURL + '/success';
res.writeHead(303, {
'Location': url
});
res.end();
}
});
var i = 0;
// for some reason the checks on convenience methods open connections
// that don't get closed so we have to do some clean up
server.on('connection', function(conn) {
conn.__connCount = i;
connections[i++] = conn;
conn.on('close', function() {
delete connections[conn.__connCount];
})
});
server.listen(0, function() {
var addr = server.address();
debug('server up at', addr);
baseURL = 'http://' + addr.address + ':' + addr.port;
done();
});
});
after(function(done) {
for (var conn in connections) {
connections[conn].destroy();
}
server.close(function() {
debug('server down');
done();
});
server = undefined;
});
describe('convenience methods', function() {
[
'head',
'options',
'get',
'post',
'put',
'patch',
'del'
].forEach(function(method) {
describe('#'+method, function() {
it('should have `then` and `end`', function() {
var promiseRequest = request[method](baseURL);
assert(promiseRequest.then instanceof Function);
assert(promiseRequest.end instanceof Function);
});
it('`end` should return a promise', function() {
var p = request[method](baseURL).end();
assert(p instanceof Promise);
});
it('`then` should return a promise', function() {
var p = request[method](baseURL).then(function() { });
assert(p instanceof Promise);
});
})
});
})
describe('#end', function() {
it('should succeed on 200', function(done) {
var url = baseURL + '/success';
request('GET', url).end().then(function(res) {
assert.equal(res.text, successBody);
}).then(done).catch(done);
});
it('should fail on 404', function(done) {
var url = baseURL + '/NotFound';
request('GET', url).end().then(undefined, function(err) {
assert.equal(err.status, 404)
assert.equal(err.response.text, errorBody);
}).then(done).catch(done);
});
it('should fail if content length is mismatched', function(done) {
var url = baseURL + '/error';
request('GET', url).end().then(function(res) {
done(new Error('Got response for mismatched Content-Length'))
}, function(err) {
assert.ok(err);
done();
});
});
it('should follow redirects', function() {
var url = baseURL + '/redirect';
return request('GET', url).end().then(function(res) {
assert.equal(res.text, successBody);
});
});
});
describe('#then', function() {
it('should succeed on 200', function(done) {
var url = baseURL + '/success';
request('GET', url).then(function(res) {
assert.equal(res.text, successBody);
}).then(done).catch(done);
});
it('issue 404 request', function(done) {
var url = baseURL + '/NotFound';
request('GET', url).then(undefined, function(err) {
assert.equal(err.status, 404)
assert.equal(err.response.text, errorBody);
}).then(done).catch(done);
});
it('test error', function(done) {
var url = baseURL + '/error';
request('GET', url).then(function(res) {
done(new Error('error should not should not succeed'));
}, function(err) {
assert.ok(err);
done();
});
});
it('issue request w. redirect', function() {
var url = baseURL + '/redirect';
return request('GET', url).then(function(res) {
assert.equal(res.text, successBody);
});
});
});
});