Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: tomas/needle
base: 37d86f687d
...
head fork: tomas/needle
compare: 2655d01c48
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 108 additions and 49 deletions.
  1. +1 −1  lib/needle.js
  2. +0 −48 test/parsing.js
  3. +107 −0 test/parsing_spec.js
View
2  lib/needle.js
@@ -45,7 +45,7 @@ var parsers = {
} catch(e) {
err = e;
}
- callback(err, data);
+ callback(err, str);
}
};
View
48 test/parsing.js
@@ -1,48 +0,0 @@
-var needle = require('./..'),
- should = require('should'),
- xml2js_present;
-
-try {
- require('xml2js');
- xml2js_present = true;
-} catch(e) {
- xml2js_present = false;
-}
-
-
-describe('parsing', function() {
-
- var url, opts = {};
-
- if (!xml2js_present)
- return;
-
- describe('when xml2js is present', function(){
-
- describe('and parse option is true', function(){
-
- before(function() {
- opts.parse = true;
- })
-
- describe('and a valid XML is requested', function(){
-
- url = 'https://news.ycombinator.com/rss';
-
- it('parses it', function(done) {
-
- needle.get(url, opts, function(err, resp, body){
- resp.statusCode.should.equal(200);
- should.exist(resp.body.rss);
- done();
- })
-
- });
-
- });
-
- });
-
- });
-
-})
View
107 test/parsing_spec.js
@@ -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(){
+
+ })
+
+ })
+
+
+ })
+
+
+})

No commit comments for this range

Something went wrong with that request. Please try again.