UNPKG

2.34 kBJavaScriptView Raw
1var assert = require ('assert'),
2 path = require('path'),
3 nock = require('nock');
4
5describe ('dependency easyRequest', function(){
6
7 var options, easyRequest;
8
9 beforeEach(function(done){
10 nock.cleanAll();
11 options = { url : 'http://invalid/' };
12 easyRequest = require('../lib/job-dependencies/easyRequest/dependency')();
13 done();
14 });
15
16 describe ('JSON', function(){
17 it('should handle non 200 status code', function(done){
18 nock('http://invalid').get('/').reply(404, {});
19
20 easyRequest.JSON(options, function(err, data){
21 assert.ok(err);
22 done();
23 });
24 });
25
26 it('should handle empty responses', function(done){
27 nock('http://invalid').get('/').reply(500, "");
28
29 easyRequest.JSON(options, function(err, data){
30 assert.ok(err);
31 done();
32 });
33 });
34
35 it('should handle non JSON responses', function(done){
36 nock('http://invalid').get('/').reply(200, "this is not json, is it?");
37
38 easyRequest.JSON(options, function(err, data){
39 assert.ok(err);
40 assert.equal('invalid json response', err);
41 done();
42 });
43 });
44
45 it('should handle successful responses', function(done){
46 nock('http://invalid').get('/').reply(200, { attr : 'some data'});
47
48 easyRequest.JSON(options, function(err, data){
49 assert.ifError(err);
50 assert.equal('some data', data.attr);
51 done();
52 });
53 });
54 });
55
56 describe ('HTML', function(){
57 it('should handle non 200 status code', function(done){
58 nock('http://invalid').get('/').reply(404, "not found");
59
60 easyRequest.HTML(options, function(err, data){
61 assert.ok(err);
62 done();
63 });
64 });
65
66 it('should handle empty responses', function(done){
67 nock('http://invalid').get('/').reply(500, "");
68
69 easyRequest.HTML(options, function(err, data){
70 assert.ok(err);
71 done();
72 });
73 });
74
75 it('should handle successful responses', function(done){
76 nock('http://invalid').get('/').reply(200, "<h2>hello</h2>");
77
78 easyRequest.HTML(options, function(err, data){
79 assert.ifError(err);
80 assert.equal('<h2>hello</h2>', data);
81 done();
82 });
83 });
84 });
85
86});