compression_spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 if (acceptEncoding.match(/\bbr\b/)) {
  26. res.setHeader('Content-Encoding', 'br');
  27. raw.pipe(zlib.createBrotliCompress()).pipe(res);
  28. } else {
  29. raw.pipe(res);
  30. }
  31. res.setHeader('Content-Type', 'application/json')
  32. if (req.headers['with-bad']) {
  33. res.end('foo'); // end, no deflate data
  34. } else {
  35. raw.end(jsonData)
  36. }
  37. })
  38. server.listen(port);
  39. });
  40. after(function(done){
  41. server.close(done);
  42. })
  43. describe('and client requests no compression', function() {
  44. it('should have the body decompressed', function(done){
  45. needle.get('localhost:' + port, function(err, response, body){
  46. should.ifError(err);
  47. body.should.have.property('foo', 'bar');
  48. response.bytes.should.equal(jsonData.length);
  49. done();
  50. })
  51. })
  52. })
  53. describe('and client requests gzip compression', function() {
  54. it('should have the body decompressed', function(done){
  55. needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'gzip'}}, function(err, response, body){
  56. should.ifError(err);
  57. body.should.have.property('foo', 'bar');
  58. response.bytes.should.not.equal(jsonData.length);
  59. done();
  60. })
  61. })
  62. })
  63. describe('and client requests deflate compression', function() {
  64. it('should have the body decompressed', function(done){
  65. needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate'}}, function(err, response, body){
  66. should.ifError(err);
  67. body.should.have.property('foo', 'bar');
  68. response.bytes.should.not.equal(jsonData.length);
  69. done();
  70. })
  71. })
  72. it('should rethrow errors from decompressors', function(done){
  73. needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate', 'With-Bad': 'true'}}, function(err, response, body) {
  74. should.exist(err);
  75. err.message.should.equal("incorrect header check");
  76. err.code.should.equal("Z_DATA_ERROR")
  77. done();
  78. })
  79. })
  80. })
  81. describe('and client requests brotli compression', function() {
  82. it('should have the body decompressed', function(done){
  83. // Skip this test if Brotli is not supported
  84. if (typeof zlib.BrotliDecompress !== 'function') {
  85. return done();
  86. }
  87. needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'br'}}, function(err, response, body){
  88. should.ifError(err);
  89. body.should.have.property('foo', 'bar');
  90. response.bytes.should.not.equal(jsonData.length);
  91. done();
  92. })
  93. })
  94. })
  95. })
  96. })