parsing_spec.js 16 KB

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