compression_spec.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var should = require('should'),
  2. needle = require('./../'),
  3. http = require('http'),
  4. zlib = require('zlib'),
  5. stream = require('stream'),
  6. port = 11123,
  7. server;
  8. describe('compression', function(){
  9. require.bind(null, 'zlib').should.not.throw()
  10. var jsonData = '{"foo":"bar"}';
  11. describe('when server supports compression', function(){
  12. before(function(){
  13. server = http.createServer(function(req, res) {
  14. var raw = new stream.PassThrough();
  15. var acceptEncoding = req.headers['accept-encoding'];
  16. if (!acceptEncoding) {
  17. acceptEncoding = '';
  18. }
  19. if (acceptEncoding.match(/\bdeflate\b/)) {
  20. res.setHeader('Content-Encoding', 'deflate');
  21. raw.pipe(zlib.createDeflate()).pipe(res);
  22. } else if (acceptEncoding.match(/\bgzip\b/)) {
  23. res.setHeader('Content-Encoding', 'gzip');
  24. raw.pipe(zlib.createGzip()).pipe(res);
  25. } else {
  26. raw.pipe(res);
  27. }
  28. res.setHeader('Content-Type', 'application/json')
  29. if (req.headers['with-bad']) {
  30. res.end('foo'); // end, no deflate data
  31. } else {
  32. raw.end(jsonData)
  33. }
  34. })
  35. server.listen(port);
  36. });
  37. after(function(done){
  38. server.close(done);
  39. })
  40. describe('and client requests no compression', function() {
  41. it('should have the body decompressed', function(done){
  42. needle.get('localhost:' + port, function(err, response, body){
  43. should.ifError(err);
  44. body.should.have.property('foo', 'bar');
  45. response.bytes.should.equal(jsonData.length);
  46. done();
  47. })
  48. })
  49. })
  50. describe('and client requests gzip compression', function() {
  51. it('should have the body decompressed', function(done){
  52. needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'gzip'}}, function(err, response, body){
  53. should.ifError(err);
  54. body.should.have.property('foo', 'bar');
  55. response.bytes.should.not.equal(jsonData.length);
  56. done();
  57. })
  58. })
  59. })
  60. describe('and client requests deflate compression', function() {
  61. it('should have the body decompressed', function(done){
  62. needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate'}}, function(err, response, body){
  63. should.ifError(err);
  64. body.should.have.property('foo', 'bar');
  65. response.bytes.should.not.equal(jsonData.length);
  66. done();
  67. })
  68. })
  69. it('should rethrow errors from decompressors', function(done){
  70. needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate', 'With-Bad': 'true'}}, function(err, response, body) {
  71. should.exist(err);
  72. err.message.should.equal("incorrect header check");
  73. err.code.should.equal("Z_DATA_ERROR")
  74. done();
  75. })
  76. })
  77. })
  78. })
  79. })