binary-streaming.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
  7. var browserName = browser.name
  8. var browserVersion = browser.major
  9. // Binary streaming doesn't work in IE10 or below
  10. var skipStreamingCheck = (browserName === 'IE' && browserVersion <= 10)
  11. // Binary data gets corrupted in IE8 or below
  12. var skipVerification = (browserName === 'IE' && browserVersion <= 8)
  13. // IE8 tends to throw up modal dialogs complaining about scripts running too long
  14. // Since streaming doesn't actually work there anyway, just use one copy
  15. var COPIES = skipVerification ? 1 : 20
  16. var MIN_PIECES = 2
  17. var referenceOnce = fs.readFileSync(__dirname + '/../server/static/browserify.png')
  18. var reference = new Buffer(referenceOnce.length * COPIES)
  19. for(var i = 0; i < COPIES; i++) {
  20. referenceOnce.copy(reference, referenceOnce.length * i)
  21. }
  22. test('binary streaming', function (t) {
  23. http.get({
  24. path: '/browserify.png?copies=' + COPIES,
  25. mode: 'allow-wrong-content-type'
  26. }, function (res) {
  27. var buffers = []
  28. res.on('end', function () {
  29. if (skipVerification)
  30. t.skip('binary data not preserved on IE <= 8')
  31. else
  32. t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
  33. if (skipStreamingCheck)
  34. t.skip('streaming not available on IE <= 8')
  35. else
  36. t.ok(buffers.length >= MIN_PIECES, 'received in multiple parts')
  37. t.end()
  38. })
  39. res.on('data', function (data) {
  40. buffers.push(data)
  41. })
  42. })
  43. })
  44. test('large binary request without streaming', function (t) {
  45. http.get({
  46. path: '/browserify.png?copies=' + COPIES,
  47. mode: 'default',
  48. }, function (res) {
  49. var buffers = []
  50. res.on('end', function () {
  51. if (skipVerification)
  52. t.skip('binary data not preserved on IE <= 8')
  53. else
  54. t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
  55. t.end()
  56. })
  57. res.on('data', function (data) {
  58. buffers.push(data)
  59. })
  60. })
  61. })