headers.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var Buffer = require('buffer').Buffer
  2. var fs = require('fs')
  3. var test = require('tape')
  4. var UAParser = require('ua-parser-js')
  5. var http = require('../..')
  6. test('headers', function (t) {
  7. http.get({
  8. path: '/testHeaders?Response-Header=bar&Response-Header-2=BAR2',
  9. headers: {
  10. 'Test-Request-Header': 'foo',
  11. 'Test-Request-Header-2': 'FOO2'
  12. }
  13. }, function (res) {
  14. var rawHeaders = []
  15. for (var i = 0; i < res.rawHeaders.length; i += 2) {
  16. var lowerKey = res.rawHeaders[i].toLowerCase()
  17. if (lowerKey.indexOf('test-') === 0)
  18. rawHeaders.push(lowerKey, res.rawHeaders[i + 1])
  19. }
  20. var header1Pos = rawHeaders.indexOf('test-response-header')
  21. t.ok(header1Pos >= 0, 'raw response header 1 present')
  22. t.equal(rawHeaders[header1Pos + 1], 'bar', 'raw response header value 1')
  23. var header2Pos = rawHeaders.indexOf('test-response-header-2')
  24. t.ok(header2Pos >= 0, 'raw response header 2 present')
  25. t.equal(rawHeaders[header2Pos + 1], 'BAR2', 'raw response header value 2')
  26. t.equal(rawHeaders.length, 4, 'correct number of raw headers')
  27. t.equal(res.headers['test-response-header'], 'bar', 'response header 1')
  28. t.equal(res.headers['test-response-header-2'], 'BAR2', 'response header 2')
  29. var buffers = []
  30. res.on('end', function () {
  31. var body = JSON.parse(Buffer.concat(buffers).toString())
  32. t.equal(body['test-request-header'], 'foo', 'request header 1')
  33. t.equal(body['test-request-header-2'], 'FOO2', 'request header 2')
  34. t.equal(Object.keys(body).length, 2, 'correct number of request headers')
  35. t.end()
  36. })
  37. res.on('data', function (data) {
  38. buffers.push(data)
  39. })
  40. })
  41. })
  42. test('arrays of headers', function (t) {
  43. http.get({
  44. path: '/testHeaders?Response-Header=bar&Response-Header=BAR2',
  45. headers: {
  46. 'Test-Request-Header': ['foo', 'FOO2']
  47. }
  48. }, function (res) {
  49. var rawHeaders = []
  50. for (var i = 0; i < res.rawHeaders.length; i += 2) {
  51. var lowerKey = res.rawHeaders[i].toLowerCase()
  52. if (lowerKey.indexOf('test-') === 0)
  53. rawHeaders.push(lowerKey, res.rawHeaders[i + 1])
  54. }
  55. t.equal(rawHeaders[0], 'test-response-header', 'raw response header present')
  56. t.equal(rawHeaders[1], 'bar, BAR2', 'raw response header value')
  57. t.equal(rawHeaders.length, 2, 'correct number of raw headers')
  58. t.equal(res.headers['test-response-header'], 'bar, BAR2', 'response header')
  59. var buffers = []
  60. res.on('end', function () {
  61. var body = JSON.parse(Buffer.concat(buffers).toString())
  62. t.equal(body['test-request-header'], 'foo,FOO2', 'request headers')
  63. t.equal(Object.keys(body).length, 1, 'correct number of request headers')
  64. t.end()
  65. })
  66. res.on('data', function (data) {
  67. buffers.push(data)
  68. })
  69. })
  70. })
  71. test('content-type response header', function (t) {
  72. http.get('/testHeaders', function (res) {
  73. t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
  74. t.end()
  75. })
  76. })
  77. var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
  78. var browserName = browser.name
  79. var browserVersion = browser.major
  80. var browserMinorVersion = browser.minor || 0
  81. // The content-type header is broken when 'prefer-streaming' or 'allow-wrong-content-type'
  82. // is passed in browsers that rely on xhr.overrideMimeType(), namely older chrome, safari 6-10.0, and the stock Android browser
  83. // Note that Safari 10.0 on iOS 10.3 doesn't need to override the mime type, so the content-type is preserved.
  84. var wrongMimeType = ((browserName === 'Chrome' && browserVersion <= 42) ||
  85. ((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion >= 6 && (browserVersion < 10 || (browserVersion == 10 && browserMinorVersion == 0)))
  86. || (browserName === 'Android Browser'))
  87. test('content-type response header with forced streaming', function (t) {
  88. http.get({
  89. path: '/testHeaders',
  90. mode: 'prefer-streaming'
  91. }, function (res) {
  92. if (wrongMimeType) {
  93. // allow both the 'wrong' and correct mime type, since sometimes it's impossible to tell which to expect
  94. // from the browser version alone (e.g. Safari 10.0 on iOS 10.2 vs iOS 10.3)
  95. var contentType = res.headers['content-type']
  96. var correct = (contentType === 'text/plain; charset=x-user-defined') || (contentType === 'application/json')
  97. t.ok(correct, 'content-type either preserved or overridden')
  98. } else
  99. t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
  100. t.end()
  101. })
  102. })