source: trip-planner-front/node_modules/needle/test/parsing_spec.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 14.8 KB
RevLine 
[6a3a178]1var should = require('should'),
2 needle = require('./../'),
3 http = require('http'),
4 port = 11111,
5 server;
6
7describe('parsing', function(){
8
9 describe('when response is an JSON string', function(){
10
11 var json_string = '{"foo":"bar"}';
12
13 before(function(done){
14 server = http.createServer(function(req, res) {
15 res.setHeader('Content-Type', 'application/json');
16 res.end(json_string);
17 }).listen(port, done);
18 });
19
20 after(function(done){
21 server.close(done);
22 })
23
24 describe('and parse option is not passed', function() {
25
26 describe('with default parse_response', function() {
27
28 before(function() {
29 needle.defaults().parse_response.should.eql('all')
30 })
31
32 it('should return object', function(done){
33 needle.get('localhost:' + port, function(err, response, body){
34 should.ifError(err);
35 body.should.have.property('foo', 'bar');
36 done();
37 })
38 })
39
40 })
41
42 describe('and default parse_response is set to false', function() {
43
44 it('does NOT return object when disabled using .defaults', function(done){
45 needle.defaults({ parse_response: false })
46
47 needle.get('localhost:' + port, function(err, response, body) {
48 should.not.exist(err);
49 body.should.be.an.instanceof(String)
50 body.toString().should.eql('{"foo":"bar"}');
51
52 needle.defaults({ parse_response: 'all' });
53 done();
54 })
55 })
56
57
58 })
59
60 })
61
62 describe('and parse option is true', function() {
63
64 describe('and JSON is valid', function() {
65
66 it('should return object', function(done) {
67 needle.get('localhost:' + port, { parse: true }, function(err, response, body){
68 should.not.exist(err);
69 body.should.have.property('foo', 'bar')
70 done();
71 })
72 })
73
74 it('should have a .parser = json property', function(done) {
75 needle.get('localhost:' + port, { parse: true }, function(err, resp) {
76 should.not.exist(err);
77 resp.parser.should.eql('json');
78 done();
79 })
80 })
81
82 });
83
84 describe('and response is empty', function() {
85
86 var old_json_string;
87
88 before(function() {
89 old_json_string = json_string;
90 json_string = "";
91 });
92
93 after(function() {
94 json_string = old_json_string;
95 });
96
97 it('should return an empty string', function(done) {
98 needle.get('localhost:' + port, { parse: true }, function(err, resp) {
99 should.not.exist(err);
100 resp.body.should.equal('');
101 done();
102 })
103 })
104
105 })
106
107 describe('and JSON is invalid', function() {
108
109 var old_json_string;
110
111 before(function() {
112 old_json_string = json_string;
113 json_string = "this is not going to work";
114 });
115
116 after(function() {
117 json_string = old_json_string;
118 });
119
120 it('does not throw', function(done) {
121 (function(){
122 needle.get('localhost:' + port, { parse: true }, done);
123 }).should.not.throw();
124 });
125
126 it('does NOT return object', function(done) {
127 needle.get('localhost:' + port, { parse: true }, function(err, response, body) {
128 should.not.exist(err);
129 body.should.be.a.String;
130 body.toString().should.eql('this is not going to work');
131 done();
132 })
133 })
134
135 });
136
137 })
138
139 describe('and parse option is false', function() {
140
141 it('does NOT return object', function(done){
142 needle.get('localhost:' + port, { parse: false }, function(err, response, body) {
143 should.not.exist(err);
144 body.should.be.an.instanceof(String)
145 body.toString().should.eql('{"foo":"bar"}');
146 done();
147 })
148 })
149
150 it('should NOT have a .parser = json property', function(done) {
151 needle.get('localhost:' + port, { parse: false }, function(err, resp) {
152 should.not.exist(err);
153 should.not.exist(resp.parser);
154 done();
155 })
156 })
157
158 })
159
160 describe('and parse option is "xml"', function() {
161
162 it('does NOT return object', function(done){
163 needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) {
164 should.not.exist(err);
165 body.should.be.an.instanceof(String)
166 body.toString().should.eql('{"foo":"bar"}');
167 done();
168 })
169 })
170
171 it('should NOT have a .parser = json property', function(done) {
172 needle.get('localhost:' + port, { parse: 'xml' }, function(err, resp) {
173 should.not.exist(err);
174 should.not.exist(resp.parser);
175 done();
176 })
177 })
178
179 })
180
181 });
182
183 describe('when response is JSON \'false\'', function(){
184
185 var json_string = 'false';
186
187 before(function(done){
188 server = http.createServer(function(req, res) {
189 res.setHeader('Content-Type', 'application/json');
190 res.end(json_string);
191 }).listen(port, done);
192 });
193
194 after(function(done){
195 server.close(done);
196 })
197
198 describe('and parse option is not passed', function() {
199
200 it('should return object', function(done){
201 needle.get('localhost:' + port, function(err, response, body){
202 should.ifError(err);
203 body.should.equal(false);
204 done();
205 })
206 })
207
208 })
209
210 describe('and parse option is true', function() {
211
212 describe('and JSON is valid', function() {
213
214 it('should return object', function(done){
215 needle.get('localhost:' + port, { parse: true }, function(err, response, body){
216 should.not.exist(err);
217 body.should.equal(false)
218 done();
219 })
220 })
221
222 });
223
224 describe('and response is empty', function() {
225
226 var old_json_string;
227
228 before(function() {
229 old_json_string = json_string;
230 json_string = "";
231 });
232
233 after(function() {
234 json_string = old_json_string;
235 });
236
237 it('should return an empty string', function(done) {
238 needle.get('localhost:' + port, { parse: true }, function(err, resp) {
239 should.not.exist(err);
240 resp.body.should.equal('');
241 done();
242 })
243 })
244
245 })
246
247 describe('and JSON is invalid', function() {
248
249 var old_json_string;
250
251 before(function() {
252 old_json_string = json_string;
253 json_string = "this is not going to work";
254 });
255
256 after(function() {
257 json_string = old_json_string;
258 });
259
260 it('does not throw', function(done) {
261 (function(){
262 needle.get('localhost:' + port, { parse: true }, done);
263 }).should.not.throw();
264 });
265
266 it('does NOT return object', function(done) {
267 needle.get('localhost:' + port, { parse: true }, function(err, response, body) {
268 should.not.exist(err);
269 body.should.be.a.String;
270 body.toString().should.eql('this is not going to work');
271 done();
272 })
273 })
274
275 });
276
277 })
278
279 describe('and parse option is false', function() {
280
281 it('does NOT return object', function(done){
282 needle.get('localhost:' + port, { parse: false }, function(err, response, body) {
283 should.not.exist(err);
284 body.should.be.an.instanceof(String)
285 body.toString().should.eql('false');
286 done();
287 })
288 })
289
290 })
291
292 describe('and parse option is "xml"', function() {
293
294 it('does NOT return object', function(done){
295 needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) {
296 should.not.exist(err);
297 body.should.be.an.instanceof(String)
298 body.toString().should.eql('false');
299 done();
300 })
301 })
302
303 })
304
305
306 });
307
308 describe('when response is an invalid XML string', function(){
309
310 before(function(done){
311 server = http.createServer(function(req, res) {
312 res.writeHeader(200, {'Content-Type': 'application/xml'})
313 res.end("<post><body there11post>")
314 }).listen(port, done);
315 });
316
317 after(function(done){
318 server.close(done);
319 })
320
321 describe('and parse_response is true', function(){
322
323 it('should return original string', function(done) {
324 needle.get('localhost:' + port, { parse_response: true }, function(err, response, body) {
325 should.not.exist(err);
326 body.should.eql('<post><body there11post>')
327 should.not.exist(body.name);
328 done();
329 })
330 })
331
332 it('should not have a .parser = xml property', function(done) {
333 needle.get('localhost:' + port, { parse_response: true }, function(err, resp) {
334 should.not.exist(err);
335 should.not.exist(resp.parser);
336 done();
337 })
338 })
339
340 })
341
342 describe('and parse response is false', function(){
343
344 it('should return valid object', function(done) {
345 needle.get('localhost:' + port, { parse_response: false }, function(err, response, body){
346 should.not.exist(err);
347 body.toString().should.eql('<post><body there11post>')
348 done();
349 })
350 })
351
352 it('should not have a .parser property', function(done) {
353 needle.get('localhost:' + port, { parse_response: false }, function(err, resp) {
354 should.not.exist(err);
355 should.not.exist(resp.parser)
356 done();
357 })
358 })
359
360 })
361
362 })
363
364 describe('when response is a valid XML string', function(){
365
366 before(function(done) {
367 server = http.createServer(function(req, res) {
368 res.writeHeader(200, {'Content-Type': 'application/xml'})
369 res.end("<post><p>hello</p><p><![CDATA[world]]></p></post>")
370 }).listen(port, done);
371 });
372
373 after(function(done) {
374 server.close(done);
375 })
376
377 describe('and parse_response is true', function(){
378
379 it('should return valid object', function(done) {
380 needle.get('localhost:' + port, { parse_response: true }, function(err, response, body) {
381 should.not.exist(err);
382 body.name.should.eql('post')
383 body.children[0].name.should.eql('p')
384 body.children[0].value.should.eql('hello')
385
386 body.children[1].name.should.eql('p')
387 body.children[1].value.should.eql('world')
388 done();
389 })
390 })
391
392 it('should have a .parser = xml property', function(done) {
393 needle.get('localhost:' + port, { parse_response: true }, function(err, resp) {
394 should.not.exist(err);
395 resp.parser.should.eql('xml');
396 done();
397 })
398 })
399
400 })
401
402 describe('and parse response is false', function(){
403
404 it('should return valid object', function(done) {
405 needle.get('localhost:' + port, { parse_response: false }, function(err, response, body){
406 should.not.exist(err);
407 body.toString().should.eql('<post><p>hello</p><p><![CDATA[world]]></p></post>')
408 done();
409 })
410 })
411
412 it('should not have a .parser property', function(done) {
413 needle.get('localhost:' + port, { parse_response: false }, function(err, resp) {
414 should.not.exist(err);
415 should.not.exist(resp.parser)
416 done();
417 })
418 })
419
420 })
421
422 })
423
424 describe('valid XML, using xml2js', function() {
425
426 var parsers, origParser;
427
428 before(function(done) {
429 var xml2js = require('xml2js')
430 parsers = require('../lib/parsers');
431 origParser = parsers['application/xml'];
432
433 var customParser = require('xml2js').parseString;
434 parsers.use('xml2js', ['application/xml'], function(buff, cb) {
435 var opts = { explicitRoot: true, explicitArray: false };
436 customParser(buff, opts, cb);
437 })
438
439 server = http.createServer(function(req, res) {
440 res.writeHeader(200, {'Content-Type': 'application/xml'})
441 res.end("<post><p>hello</p><p>world</p></post>")
442 }).listen(port, done);
443 });
444
445 after(function(done) {
446 parsers['application/xml'] = origParser;
447 server.close(done);
448 })
449
450 describe('and parse_response is true', function(){
451
452 it('should return valid object', function(done) {
453 needle.get('localhost:' + port, { parse_response: true }, function(err, response, body) {
454 should.not.exist(err);
455 body.should.eql({ post: { p: ['hello', 'world' ]}})
456 done();
457 })
458 })
459
460 it('should have a .parser = xml property', function(done) {
461 needle.get('localhost:' + port, { parse_response: true }, function(err, resp) {
462 should.not.exist(err);
463 resp.parser.should.eql('xml2js');
464 done();
465 })
466 })
467
468 })
469
470 describe('and parse response is false', function(){
471
472 it('should return valid object', function(done) {
473 needle.get('localhost:' + port, { parse_response: false }, function(err, response, body){
474 should.not.exist(err);
475 body.toString().should.eql('<post><p>hello</p><p>world</p></post>')
476 done();
477 })
478 })
479
480 it('should not have a .parser property', function(done) {
481 needle.get('localhost:' + port, { parse_response: false }, function(err, resp) {
482 should.not.exist(err);
483 should.not.exist(resp.parser)
484 done();
485 })
486 })
487
488 })
489
490 })
491
492 describe('when response is a JSON API flavored JSON string', function () {
493
494 var json_string = '{"data":[{"type":"articles","id":"1","attributes":{"title":"Needle","body":"The leanest and most handsome HTTP client in the Nodelands."}}],"included":[{"type":"people","id":"42","attributes":{"name":"Tomás"}}]}';
495
496 before(function(done){
497 server = http.createServer(function(req, res) {
498 res.setHeader('Content-Type', 'application/vnd.api+json');
499 res.end(json_string);
500 }).listen(port, done);
501 });
502
503 after(function(done){
504 server.close(done);
505 });
506
507 describe('and parse option is not passed', function() {
508
509 describe('with default parse_response', function() {
510
511 before(function() {
512 needle.defaults().parse_response.should.eql('all')
513 })
514
515 it('should return object', function(done){
516 needle.get('localhost:' + port, function(err, response, body){
517 should.ifError(err);
518 body.should.deepEqual({
519 "data": [{
520 "type": "articles",
521 "id": "1",
522 "attributes": {
523 "title": "Needle",
524 "body": "The leanest and most handsome HTTP client in the Nodelands."
525 }
526 }],
527 "included": [
528 {
529 "type": "people",
530 "id": "42",
531 "attributes": {
532 "name": "Tomás"
533 }
534 }
535 ]
536 });
537 done();
538 });
539 });
540
541 });
542
543 })
544
545 });
546
547})
Note: See TracBrowser for help on using the repository browser.