/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

  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. it('should return object', function(done){
  20. needle.get('localhost:' + port, function(err, response, body){
  21. should.ifError(err);
  22. body.should.have.property('foo', 'bar');
  23. done();
  24. })
  25. })
  26. })
  27. describe('and parse option is true', function() {
  28. describe('and JSON is valid', function() {
  29. it('should return object', function(done) {
  30. needle.get('localhost:' + port, { parse: true }, function(err, response, body){
  31. should.not.exist(err);
  32. body.should.have.property('foo', 'bar')
  33. done();
  34. })
  35. })
  36. it('should have a .parser = json property', function(done) {
  37. needle.get('localhost:' + port, { parse: true }, function(err, resp) {
  38. should.not.exist(err);
  39. resp.parser.should.eql('json');
  40. done();
  41. })
  42. })
  43. });
  44. describe('and response is empty', function() {
  45. var old_json_string;
  46. before(function() {
  47. old_json_string = json_string;
  48. json_string = "";
  49. });
  50. after(function() {
  51. json_string = old_json_string;
  52. });
  53. it('should return an empty string', function(done) {
  54. needle.get('localhost:' + port, { parse: true }, function(err, resp) {
  55. should.not.exist(err);
  56. resp.body.should.equal('');
  57. done();
  58. })
  59. })
  60. })
  61. describe('and JSON is invalid', function() {
  62. var old_json_string;
  63. before(function() {
  64. old_json_string = json_string;
  65. json_string = "this is not going to work";
  66. });
  67. after(function() {
  68. json_string = old_json_string;
  69. });
  70. it('does not throw', function(done) {
  71. (function(){
  72. needle.get('localhost:' + port, { parse: true }, done);
  73. }).should.not.throw();
  74. });
  75. it('does NOT return object', function(done) {
  76. needle.get('localhost:' + port, { parse: true }, function(err, response, body) {
  77. should.not.exist(err);
  78. body.should.be.a.String;
  79. body.toString().should.eql('this is not going to work');
  80. done();
  81. })
  82. })
  83. });
  84. })
  85. describe('and parse option is false', function() {
  86. it('does NOT return object', function(done){
  87. needle.get('localhost:' + port, { parse: false }, function(err, response, body) {
  88. should.not.exist(err);
  89. body.should.be.an.instanceof(Buffer)
  90. body.toString().should.eql('{"foo":"bar"}');
  91. done();
  92. })
  93. })
  94. it('should NOT have a .parser = json property', function(done) {
  95. needle.get('localhost:' + port, { parse: false }, function(err, resp) {
  96. should.not.exist(err);
  97. should.not.exist(resp.parser);
  98. done();
  99. })
  100. })
  101. })
  102. describe('and parse option is "xml"', function() {
  103. it('does NOT return object', function(done){
  104. needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) {
  105. should.not.exist(err);
  106. body.should.be.an.instanceof(Buffer)
  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: 'xml' }, function(err, resp) {
  113. should.not.exist(err);
  114. should.not.exist(resp.parser);
  115. done();
  116. })
  117. })
  118. })
  119. });
  120. describe('when response is JSON \'false\'', function(){
  121. var json_string = 'false';
  122. before(function(done){
  123. server = http.createServer(function(req, res) {
  124. res.setHeader('Content-Type', 'application/json');
  125. res.end(json_string);
  126. }).listen(port, done);
  127. });
  128. after(function(done){
  129. server.close(done);
  130. })
  131. describe('and parse option is not passed', function() {
  132. it('should return object', function(done){
  133. needle.get('localhost:' + port, function(err, response, body){
  134. should.ifError(err);
  135. body.should.equal(false);
  136. done();
  137. })
  138. })
  139. })
  140. describe('and parse option is true', function() {
  141. describe('and JSON is valid', function() {
  142. it('should return object', function(done){
  143. needle.get('localhost:' + port, { parse: true }, function(err, response, body){
  144. should.not.exist(err);
  145. body.should.equal(false)
  146. done();
  147. })
  148. })
  149. });
  150. describe('and response is empty', function() {
  151. var old_json_string;
  152. before(function() {
  153. old_json_string = json_string;
  154. json_string = "";
  155. });
  156. after(function() {
  157. json_string = old_json_string;
  158. });
  159. it('should return an empty string', function(done) {
  160. needle.get('localhost:' + port, { parse: true }, function(err, resp) {
  161. should.not.exist(err);
  162. resp.body.should.equal('');
  163. done();
  164. })
  165. })
  166. })
  167. describe('and JSON is invalid', function() {
  168. var old_json_string;
  169. before(function() {
  170. old_json_string = json_string;
  171. json_string = "this is not going to work";
  172. });
  173. after(function() {
  174. json_string = old_json_string;
  175. });
  176. it('does not throw', function(done) {
  177. (function(){
  178. needle.get('localhost:' + port, { parse: true }, done);
  179. }).should.not.throw();
  180. });
  181. it('does NOT return object', function(done) {
  182. needle.get('localhost:' + port, { parse: true }, function(err, response, body) {
  183. should.not.exist(err);
  184. body.should.be.a.String;
  185. body.toString().should.eql('this is not going to work');
  186. done();
  187. })
  188. })
  189. });
  190. })
  191. describe('and parse option is false', function() {
  192. it('does NOT return object', function(done){
  193. needle.get('localhost:' + port, { parse: false }, function(err, response, body) {
  194. should.not.exist(err);
  195. body.should.be.an.instanceof(Buffer)
  196. body.toString().should.eql('false');
  197. done();
  198. })
  199. })
  200. })
  201. describe('and parse option is "xml"', function() {
  202. it('does NOT return object', function(done){
  203. needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) {
  204. should.not.exist(err);
  205. body.should.be.an.instanceof(Buffer)
  206. body.toString().should.eql('false');
  207. done();
  208. })
  209. })
  210. })
  211. });
  212. describe('when response is an XML string', function(){
  213. before(function(done){
  214. server = http.createServer(function(req, res) {
  215. res.writeHeader(200, {'Content-Type': 'application/xml'})
  216. res.end("<post><body>hello there</body></post>")
  217. }).listen(port, done);
  218. });
  219. after(function(done){
  220. server.close(done);
  221. })
  222. describe('and xml2js library is present', function(){
  223. require.bind(null, 'xml2js').should.not.throw();
  224. describe('and parse_response is true', function(){
  225. it('should return valid object', function(done){
  226. needle.get('localhost:' + port, function(err, response, body){
  227. should.not.exist(err);
  228. body.post.should.have.property('body', 'hello there');
  229. done();
  230. })
  231. })
  232. it('should have a .parser = json property', function(done) {
  233. needle.get('localhost:' + port, function(err, resp) {
  234. should.not.exist(err);
  235. resp.parser.should.eql('xml');
  236. done();
  237. })
  238. })
  239. })
  240. describe('and parse response is not true', function(){
  241. it('should return xml string', function(){
  242. })
  243. })
  244. })
  245. describe('and xml2js is not found', function(){
  246. it('should return xml string', function(){
  247. })
  248. })
  249. })
  250. })