PageRenderTime 65ms CodeModel.GetById 16ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/nodemailer/node_modules/needle/test/parsing_spec.js

https://gitlab.com/viddeshshinde/email
JavaScript | 340 lines | 250 code | 90 blank | 0 comment | 0 complexity | 07059751ec8812a6b276d3743594c0be MD5 | raw file
  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      it('should return object', function(done){
 27        needle.get('localhost:' + port, function(err, response, body){
 28          should.ifError(err);
 29          body.should.have.property('foo', 'bar');
 30          done();
 31        })
 32      })
 33
 34    })
 35
 36    describe('and parse option is true', function() {
 37
 38      describe('and JSON is valid', function() {
 39
 40        it('should return object', function(done) {
 41          needle.get('localhost:' + port, { parse: true }, function(err, response, body){
 42            should.not.exist(err);
 43            body.should.have.property('foo', 'bar')
 44            done();
 45          })
 46        })
 47
 48        it('should have a .parser = json property', function(done) {
 49          needle.get('localhost:' + port, { parse: true }, function(err, resp) {
 50            should.not.exist(err);
 51            resp.parser.should.eql('json');
 52            done();
 53          })
 54        })
 55
 56      });
 57
 58      describe('and response is empty', function() {
 59
 60        var old_json_string;
 61
 62        before(function() {
 63          old_json_string = json_string;
 64          json_string = "";
 65        });
 66
 67        after(function() {
 68          json_string = old_json_string;
 69        });
 70
 71        it('should return an empty string', function(done) {
 72          needle.get('localhost:' + port, { parse: true }, function(err, resp) {
 73            should.not.exist(err);
 74            resp.body.should.equal('');
 75            done();
 76          })
 77        })
 78
 79      })
 80
 81      describe('and JSON is invalid', function() {
 82
 83        var old_json_string;
 84
 85        before(function() {
 86          old_json_string = json_string;
 87          json_string = "this is not going to work";
 88        });
 89
 90        after(function() {
 91          json_string = old_json_string;
 92        });
 93
 94        it('does not throw', function(done) {
 95          (function(){
 96            needle.get('localhost:' + port, { parse: true }, done);
 97          }).should.not.throw();
 98        });
 99
100        it('does NOT return object', function(done) {
101          needle.get('localhost:' + port, { parse: true }, function(err, response, body) {
102            should.not.exist(err);
103            body.should.be.a.String;
104            body.toString().should.eql('this is not going to work');
105            done();
106          })
107        })
108
109      });
110
111    })
112
113    describe('and parse option is false', function() {
114
115      it('does NOT return object', function(done){
116        needle.get('localhost:' + port, { parse: false }, function(err, response, body) {
117          should.not.exist(err);
118          body.should.be.an.instanceof(Buffer)
119          body.toString().should.eql('{"foo":"bar"}');
120          done();
121        })
122      })
123
124      it('should NOT have a .parser = json property', function(done) {
125        needle.get('localhost:' + port, { parse: false }, function(err, resp) {
126          should.not.exist(err);
127          should.not.exist(resp.parser);
128          done();
129        })
130      })
131
132    })
133
134    describe('and parse option is "xml"', function() {
135
136      it('does NOT return object', function(done){
137        needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) {
138          should.not.exist(err);
139          body.should.be.an.instanceof(Buffer)
140          body.toString().should.eql('{"foo":"bar"}');
141          done();
142        })
143      })
144
145      it('should NOT have a .parser = json property', function(done) {
146        needle.get('localhost:' + port, { parse: 'xml' }, function(err, resp) {
147          should.not.exist(err);
148          should.not.exist(resp.parser);
149          done();
150        })
151      })
152
153    })
154
155  });
156
157  describe('when response is JSON \'false\'', function(){
158
159    var json_string = 'false';
160
161    before(function(done){
162      server = http.createServer(function(req, res) {
163        res.setHeader('Content-Type', 'application/json');
164        res.end(json_string);
165      }).listen(port, done);
166    });
167
168    after(function(done){
169      server.close(done);
170    })
171
172    describe('and parse option is not passed', function() {
173
174      it('should return object', function(done){
175        needle.get('localhost:' + port, function(err, response, body){
176          should.ifError(err);
177          body.should.equal(false);
178          done();
179        })
180      })
181
182    })
183
184    describe('and parse option is true', function() {
185
186      describe('and JSON is valid', function() {
187
188        it('should return object', function(done){
189          needle.get('localhost:' + port, { parse: true }, function(err, response, body){
190            should.not.exist(err);
191            body.should.equal(false)
192            done();
193          })
194        })
195
196      });
197
198      describe('and response is empty', function() {
199
200        var old_json_string;
201
202        before(function() {
203          old_json_string = json_string;
204          json_string = "";
205        });
206
207        after(function() {
208          json_string = old_json_string;
209        });
210
211        it('should return an empty string', function(done) {
212          needle.get('localhost:' + port, { parse: true }, function(err, resp) {
213            should.not.exist(err);
214            resp.body.should.equal('');
215            done();
216          })
217        })
218
219      })
220
221      describe('and JSON is invalid', function() {
222
223        var old_json_string;
224
225        before(function() {
226          old_json_string = json_string;
227          json_string = "this is not going to work";
228        });
229
230        after(function() {
231          json_string = old_json_string;
232        });
233
234        it('does not throw', function(done) {
235          (function(){
236            needle.get('localhost:' + port, { parse: true }, done);
237          }).should.not.throw();
238        });
239
240        it('does NOT return object', function(done) {
241          needle.get('localhost:' + port, { parse: true }, function(err, response, body) {
242            should.not.exist(err);
243            body.should.be.a.String;
244            body.toString().should.eql('this is not going to work');
245            done();
246          })
247        })
248
249      });
250
251    })
252
253    describe('and parse option is false', function() {
254
255      it('does NOT return object', function(done){
256        needle.get('localhost:' + port, { parse: false }, function(err, response, body) {
257          should.not.exist(err);
258          body.should.be.an.instanceof(Buffer)
259          body.toString().should.eql('false');
260          done();
261        })
262      })
263
264    })
265
266    describe('and parse option is "xml"', function() {
267
268      it('does NOT return object', function(done){
269        needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) {
270          should.not.exist(err);
271          body.should.be.an.instanceof(Buffer)
272          body.toString().should.eql('false');
273          done();
274        })
275      })
276
277    })
278
279
280  });
281
282  describe('when response is an XML string', function(){
283
284    before(function(done){
285      server = http.createServer(function(req, res) {
286        res.writeHeader(200, {'Content-Type': 'application/xml'})
287        res.end("<post><body>hello there</body></post>")
288      }).listen(port, done);
289    });
290
291    after(function(done){
292      server.close(done);
293    })
294
295    describe('and xml2js library is present', function(){
296
297      require.bind(null, 'xml2js').should.not.throw();
298
299      describe('and parse_response is true', function(){
300
301        it('should return valid object', function(done){
302          needle.get('localhost:' + port, function(err, response, body){
303            should.not.exist(err);
304            body.post.should.have.property('body', 'hello there');
305            done();
306          })
307        })
308
309        it('should have a .parser = json property', function(done) {
310          needle.get('localhost:' + port, function(err, resp) {
311            should.not.exist(err);
312            resp.parser.should.eql('xml');
313            done();
314          })
315        })
316
317      })
318
319      describe('and parse response is not true', function(){
320
321        it('should return xml string', function(){
322
323        })
324
325      })
326
327    })
328
329    describe('and xml2js is not found', function(){
330
331      it('should return xml string', function(){
332
333      })
334
335    })
336
337
338  })
339
340})