1 | var assert = require('assert');
|
2 | var server = require('simple-server-setup');
|
3 | var Client = require('..');
|
4 | var Request = require('../lib/Request');
|
5 | var Response = require('../lib/Response');
|
6 |
|
7 | var HTTPS_SERVER_OPTIONS = {
|
8 | secure: true,
|
9 | key: __dirname+'/server.key',
|
10 | cert: __dirname+'/server.cert'
|
11 | };
|
12 |
|
13 | var HTTPS_CLIENT_OPTIONS = {
|
14 | https_ignore_errors: true
|
15 | };
|
16 |
|
17 | describe('Client', function() {
|
18 |
|
19 | describe('constructor', function() {
|
20 |
|
21 | it('should create a new instance with the new keyword', function() {
|
22 | var client = new Client();
|
23 | assert(client instanceof Client);
|
24 | });
|
25 |
|
26 | it('should create a new instance with a function', function() {
|
27 | var client = Client();
|
28 | assert(client instanceof Client);
|
29 | });
|
30 |
|
31 | });
|
32 |
|
33 | describe('.send()', function() {
|
34 |
|
35 | it('should fetch a HTTP resource', function(done) {
|
36 |
|
37 | var srv = server.create(function(app) {
|
38 | app.get('/', function(req, res) {
|
39 | res.send('HTTP');
|
40 | });
|
41 | });
|
42 |
|
43 | srv.on('configured', function() {
|
44 |
|
45 | Client(HTTPS_CLIENT_OPTIONS).get(srv.url, function(error, response) {
|
46 | assert.equal(response.getStatus(), 200);
|
47 | assert.equal(error, undefined);
|
48 | srv.close(done);
|
49 | });
|
50 |
|
51 | });
|
52 |
|
53 | });
|
54 |
|
55 | it('should fetch a HTTPS resource', function(done) {
|
56 |
|
57 | var srv = server.create(HTTPS_SERVER_OPTIONS, function(app) {
|
58 | app.get('/', function(req, res) {
|
59 | res.send('HTTPS');
|
60 | });
|
61 | });
|
62 |
|
63 | srv.on('configured', function() {
|
64 |
|
65 | Client(HTTPS_CLIENT_OPTIONS).get(srv.url, function(error, response) {
|
66 | assert.equal(response.getStatus(), 200);
|
67 | assert.equal(error, undefined);
|
68 | srv.close(done);
|
69 | });
|
70 |
|
71 | });
|
72 |
|
73 | });
|
74 |
|
75 | it('should emit `before`', function(done) {
|
76 |
|
77 | var srv = server.create(function(app) {
|
78 | app.get('/', function(req, res) {
|
79 | res.send('SERVER');
|
80 | });
|
81 | });
|
82 |
|
83 | srv.on('configured', function() {
|
84 | var event;
|
85 |
|
86 | Client()
|
87 | .on('before', function(e) {event=e.getName();})
|
88 | .get(srv.url, function(error, response) {
|
89 | assert.equal(event, 'before');
|
90 | srv.close(done);
|
91 | })
|
92 | ;
|
93 |
|
94 | });
|
95 |
|
96 | });
|
97 |
|
98 | it('`before` event should be stoppable', function(done) {
|
99 |
|
100 | var srv = server.create(function(app) {
|
101 | app.get('/', function(req, res) {
|
102 | res.send('SERVER');
|
103 | });
|
104 | });
|
105 |
|
106 | srv.on('configured', function() {
|
107 | var p1, p2;
|
108 |
|
109 | Client()
|
110 | .on('before', function(e) {p1=true;e.stopPropagation();})
|
111 | .on('before', function(e) {p2=true;})
|
112 | .get(srv.url, function(error, response) {
|
113 | assert(p1);
|
114 | assert(!p2);
|
115 | srv.close(done);
|
116 | })
|
117 | ;
|
118 |
|
119 | });
|
120 |
|
121 | });
|
122 |
|
123 | it('`before` event should be preventable');
|
124 | it('should emit `after`', function(done) {
|
125 |
|
126 | var srv = server.create(function(app) {
|
127 | app.get('/', function(req, res) {
|
128 | res.send('SERVER');
|
129 | });
|
130 | });
|
131 |
|
132 | srv.on('configured', function() {
|
133 | var event;
|
134 |
|
135 | Client()
|
136 | .on('after', function(e) {event=e.getName();})
|
137 | .get(srv.url, function(error, response) {
|
138 | assert.equal(event, 'after');
|
139 | srv.close(done);
|
140 | })
|
141 | ;
|
142 |
|
143 | });
|
144 |
|
145 | });
|
146 |
|
147 | it('`after` event should be stoppable', function(done) {
|
148 |
|
149 | var srv = server.create(function(app) {
|
150 | app.get('/', function(req, res) {
|
151 | res.send('SERVER');
|
152 | });
|
153 | });
|
154 |
|
155 | srv.on('configured', function() {
|
156 | var p1, p2;
|
157 |
|
158 | Client()
|
159 | .on('after', function(e) {p1=true;e.stopPropagation();})
|
160 | .on('after', function(e) {p2=true;})
|
161 | .get(srv.url, function(error, response) {
|
162 | assert(p1);
|
163 | assert(!p2);
|
164 | srv.close(done);
|
165 | })
|
166 | ;
|
167 |
|
168 | });
|
169 |
|
170 | });
|
171 |
|
172 | it('should emit `sent`', function(done) {
|
173 |
|
174 | var srv = server.create(function(app) {
|
175 | app.get('/', function(req, res) {
|
176 | res.send('SERVER');
|
177 | });
|
178 | });
|
179 |
|
180 | srv.on('configured', function() {
|
181 | var called;
|
182 |
|
183 | Client()
|
184 | .on('sent', function() {called=true})
|
185 | .get(srv.url, function(error, response) {
|
186 | assert(called);
|
187 | srv.close(done);
|
188 | })
|
189 | ;
|
190 |
|
191 | });
|
192 |
|
193 | });
|
194 |
|
195 | it('should emit `received`', function(done) {
|
196 |
|
197 | var srv = server.create(function(app) {
|
198 | app.get('/', function(req, res) {
|
199 | res.send('SERVER');
|
200 | });
|
201 | });
|
202 |
|
203 | srv.on('configured', function() {
|
204 | var called;
|
205 |
|
206 | Client()
|
207 | .on('received', function() {called=true})
|
208 | .get(srv.url, function(error, response) {
|
209 | assert(called);
|
210 | srv.close(done);
|
211 | })
|
212 | ;
|
213 |
|
214 | });
|
215 |
|
216 | });
|
217 |
|
218 | it('should emit `error`', function(done) {
|
219 | var called;
|
220 |
|
221 | Client()
|
222 | .on('error', function() {called=true})
|
223 | .get('http://does.not.exist', function(error, response) {
|
224 | assert(called);
|
225 | done();
|
226 | })
|
227 | ;
|
228 |
|
229 | });
|
230 |
|
231 | it('should emit `error` if a plugin fails `before`', function(done) {
|
232 |
|
233 | var srv = server.create(function(app) {
|
234 | app.get('/', function(req, res) {
|
235 | res.send('');
|
236 | });
|
237 | });
|
238 |
|
239 | srv.on('configured', function() {
|
240 |
|
241 | Client()
|
242 | .use(function(client) {
|
243 | client.on('before', function(event, next) {
|
244 | next(new Error('Plugin error'));
|
245 | });
|
246 | })
|
247 | .get(srv.url)
|
248 | .on('error', function(err) {
|
249 | assert.equal(err.message, 'Plugin error');
|
250 | srv.close(done);
|
251 | })
|
252 | .send()
|
253 | ;
|
254 |
|
255 | });
|
256 | });
|
257 |
|
258 | it('should emit `error` if a plugin fails `after`', function(done) {
|
259 |
|
260 | var srv = server.create(function (app) {
|
261 | app.get('/', function (req, res) {
|
262 | res.send('');
|
263 | });
|
264 | });
|
265 |
|
266 | srv.on('configured', function () {
|
267 |
|
268 | Client()
|
269 | .use(function (client) {
|
270 | client.on('after', function (event, next) {
|
271 | next(new Error('Plugin error'));
|
272 | });
|
273 | })
|
274 | .get(srv.url)
|
275 | .send()
|
276 | .on('error', function (err) {
|
277 | assert.equal(err.message, 'Plugin error');
|
278 | srv.close(done);
|
279 | })
|
280 | ;
|
281 |
|
282 | });
|
283 |
|
284 | });
|
285 |
|
286 | it('should still have events registered on the `Response` before it is injected');
|
287 | it('should still call methods on the `Response` before it is injected');
|
288 |
|
289 | });
|
290 |
|
291 | describe('.request()', function() {
|
292 |
|
293 | describe('Callback-style', function(done) {
|
294 |
|
295 | it('should return a `Client`', function(done) {
|
296 |
|
297 | var client = Client().get('http://localhost/', function(err, res) {
|
298 | done();
|
299 | })
|
300 | ;
|
301 |
|
302 | assert(client instanceof Client);
|
303 |
|
304 | });
|
305 |
|
306 | it('should handle different length arguments');
|
307 | it('should handle different length arguments');
|
308 |
|
309 | it('should call the callback on success');
|
310 | it('should call the callback on error');
|
311 |
|
312 | it('should call the callback with the global context');
|
313 |
|
314 | it('should pass an error if it cannot connect to the server');
|
315 |
|
316 | });
|
317 |
|
318 | describe('OOP-style', function() {
|
319 |
|
320 | it('should return a `Request`', function() {
|
321 | var request = Client().get('http://localhost/');
|
322 | assert(request instanceof Request);
|
323 | });
|
324 |
|
325 | it('should add a `.send()` method to the request', function() {
|
326 | var request = Client().get('http://localhost/');
|
327 | assert(typeof(request.send), 'function');
|
328 | });
|
329 |
|
330 | });
|
331 |
|
332 | });
|
333 |
|
334 | });
|