Permalink
Browse files

Added parsing spec.

  • Loading branch information...
1 parent f15a2fa commit 2655d01c4813329d81416805a5008cd73bcedb15 @tomas committed Mar 5, 2014
Showing with 107 additions and 0 deletions.
  1. +107 −0 test/parsing_spec.js
View
@@ -0,0 +1,107 @@
+var should = require('should'),
+ needle = require('./../'),
+ http = require('http'),
+ port = 11111,
+ server;
+
+describe('parsing', function(){
+
+ describe('when response is an JSON string', function(){
+
+ before(function(){
+ server = http.createServer(function(req, res) {
+ res.setHeaders({'Content-Type': 'application/json'})
+ res.end('{"foo":"bar"}')
+ }).listen(port);
+ });
+
+ after(function(){
+ server.close();
+ })
+
+ describe('and parse option is not passed', function() {
+
+ it('should return object', function(){
+ needle.get('localhost:' + port, function(err, response, body){
+ should.not.exist(err);
+ body.should.equal({foo: 'bar'});
+ })
+ })
+
+ })
+
+ describe('and parse option is true', function() {
+
+ it('should return object', function(){
+ needle.get('localhost:' + port, { parse: true }, function(err, response, body){
+ should.not.exist(err);
+ body.should.equal({foo: 'bar'});
+ })
+ })
+
+ })
+
+ describe('and parse option is false', function() {
+
+ it('does NOT return object', function(){
+ needle.get('localhost:' + port, { parse: false }, function(err, response, body) {
+ should.not.exist(err);
+ body.should.equal('{"foo":"bar"}');
+ })
+ })
+
+ })
+
+ });
+
+ describe('when response is an XML string', function(){
+
+ before(function(){
+ server = http.createServer(function(req, res) {
+ res.writeHeader(200, {'Content-Type': 'application/xml'})
+ res.end("<post><body>hello there</body></post>")
+ }).listen(port);
+ });
+
+ after(function(){
+ server.close();
+ })
+
+ describe('and xml2js library is present', function(){
+
+ describe('and parse_response is true', function(){
+
+ it('should return JSON object', function(){
+ needle.get('localhost:' + port, function(err, response, body){
+ should.not.exist(err);
+ body.should.equal({post: {body: 'hello there'}});
+ })
+ })
+
+ })
+
+ describe('and parse response is not true', function(){
+
+ it('should return xml string', function(){
+
+
+
+ })
+
+ })
+
+ })
+
+ describe('and xml2js is not found', function(){
+
+ it('should return xml string', function(){
+
+ })
+
+ })
+
+
+ })
+
+
+})

0 comments on commit 2655d01

Please sign in to comment.