test-gzip.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var request = require('../index')
  2. , http = require('http')
  3. , assert = require('assert')
  4. , zlib = require('zlib')
  5. if (!zlib.Gunzip.prototype.setEncoding) {
  6. try {
  7. require('stringstream')
  8. } catch (e) {
  9. console.error('stringstream must be installed to run this test.')
  10. console.error('skipping this test. please install stringstream and run again if you need to test this feature.')
  11. process.exit(0)
  12. }
  13. }
  14. var testContent = 'Compressible response content.\n'
  15. , testContentGzip
  16. var server = http.createServer(function (req, res) {
  17. res.statusCode = 200
  18. res.setHeader('Content-Type', 'text/plain')
  19. if (/\bgzip\b/i.test(req.headers['accept-encoding'])) {
  20. zlib.gzip(testContent, function (err, data) {
  21. assert.ifError(err)
  22. testContentGzip = data
  23. res.setHeader('Content-Encoding', 'gzip')
  24. res.end(data)
  25. })
  26. } else {
  27. res.end(testContent)
  28. }
  29. })
  30. server.listen(6767, function (err) {
  31. assert.ifError(err)
  32. var headers, options
  33. // Transparently supports gzip decoding to callbacks
  34. options = { url: 'http://localhost:6767/foo', gzip: true }
  35. request.get(options, function (err, res, body) {
  36. assert.ifError(err)
  37. assert.strictEqual(res.headers['content-encoding'], 'gzip')
  38. assert.strictEqual(body, testContent)
  39. })
  40. // Transparently supports gzip decoding to pipes
  41. options = { url: 'http://localhost:6767/foo', gzip: true }
  42. var chunks = []
  43. request.get(options)
  44. .on('data', function (chunk) { chunks.push(chunk) })
  45. .on('end', function () {
  46. assert.strictEqual(Buffer.concat(chunks).toString(), testContent)
  47. })
  48. .on('error', function (err) { assert.ifError(err) })
  49. // Does not request gzip if user specifies Accepted-Encodings
  50. headers = { 'Accept-Encoding': null }
  51. options = {
  52. url: 'http://localhost:6767/foo',
  53. headers: headers,
  54. gzip: true
  55. }
  56. request.get(options, function (err, res, body) {
  57. assert.ifError(err)
  58. assert.strictEqual(res.headers['content-encoding'], undefined)
  59. assert.strictEqual(body, testContent)
  60. })
  61. // Does not decode user-requested encoding by default
  62. headers = { 'Accept-Encoding': 'gzip' }
  63. options = { url: 'http://localhost:6767/foo', headers: headers }
  64. request.get(options, function (err, res, body) {
  65. assert.ifError(err)
  66. assert.strictEqual(res.headers['content-encoding'], 'gzip')
  67. assert.strictEqual(body, testContentGzip.toString())
  68. })
  69. // Supports character encoding with gzip encoding
  70. headers = { 'Accept-Encoding': 'gzip' }
  71. options = {
  72. url: 'http://localhost:6767/foo',
  73. headers: headers,
  74. gzip: true,
  75. encoding: "utf8"
  76. }
  77. var strings = []
  78. request.get(options)
  79. .on('data', function (string) {
  80. assert.strictEqual(typeof string, "string")
  81. strings.push(string)
  82. })
  83. .on('end', function () {
  84. assert.strictEqual(strings.join(""), testContent)
  85. // Shutdown server after last test
  86. server.close()
  87. })
  88. .on('error', function (err) { assert.ifError(err) })
  89. })