abort.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var Buffer = require('buffer').Buffer
  2. var fs = require('fs')
  3. var test = require('tape')
  4. var http = require('../..')
  5. test('abort before response', function (t) {
  6. var req = http.get('/basic.txt', function (res) {
  7. t.fail('unexpected response')
  8. })
  9. req.abort()
  10. t.end()
  11. })
  12. test('abort on response', function (t) {
  13. var req = http.get('/basic.txt', function (res) {
  14. req.abort()
  15. t.end()
  16. res.on('end', function () {
  17. t.fail('unexpected end')
  18. })
  19. res.on('data', function (data) {
  20. t.fail('unexpected data')
  21. })
  22. })
  23. })
  24. test('abort on data', function (t) {
  25. var req = http.get('/browserify.png?copies=5', function (res) {
  26. var firstData = true
  27. var failOnData = false
  28. res.on('end', function () {
  29. t.fail('unexpected end')
  30. })
  31. res.on('data', function (data) {
  32. if (failOnData)
  33. t.fail('unexpected data')
  34. else if (firstData) {
  35. firstData = false
  36. req.abort()
  37. t.end()
  38. process.nextTick(function () {
  39. // Wait for any data that may have been queued
  40. // in the stream before considering data events
  41. // as errors
  42. failOnData = true
  43. })
  44. }
  45. })
  46. })
  47. })