test-errors.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict'
  2. /* globals it */
  3. const bent = require('../')
  4. const tsame = require('tsame')
  5. const assert = require('assert')
  6. const zlib = require('zlib')
  7. const ttype = (e, str) => same(e.constructor.name, str)
  8. const qs = require('querystring')
  9. const test = it
  10. const same = (x, y) => assert.ok(tsame(x, y))
  11. test('Invalid encoding', done => {
  12. try {
  13. bent('blah')
  14. } catch (e) {
  15. ttype(e, 'Error')
  16. same(e.message, 'Unknown encoding, blah')
  17. done()
  18. }
  19. })
  20. test('double method', done => {
  21. try {
  22. bent('GET', 'PUT')
  23. } catch (e) {
  24. ttype(e, 'Error')
  25. same(e.message, 'Can\'t set method to PUT, already set to GET.')
  26. done()
  27. }
  28. })
  29. test('double headers', done => {
  30. try {
  31. bent({}, {})
  32. } catch (e) {
  33. ttype(e, 'Error')
  34. same(e.message, 'Cannot set headers twice.')
  35. done()
  36. }
  37. })
  38. test('unknown protocol', async () => {
  39. try {
  40. const request = bent()
  41. await request('ftp://host.com')
  42. throw new Error('Should have already failed')
  43. } catch (e) {
  44. ttype(e, 'Error')
  45. same(e.message, 'Unknown protocol, ftp:')
  46. }
  47. })
  48. test('Invalid type', done => {
  49. try {
  50. bent(true)
  51. } catch (e) {
  52. ttype(e, 'Error')
  53. same(e.message, 'Unknown type: boolean')
  54. done()
  55. }
  56. })
  57. test('Invalid body', async () => {
  58. const r = bent('PUT')
  59. try {
  60. await r('http://localhost:3000', true)
  61. throw new Error('Should have failed')
  62. } catch (e) {
  63. ttype(e, 'Error')
  64. same(e.message, 'Unknown body type.')
  65. }
  66. })
  67. test('Invalid json', async () => {
  68. const r = bent('GET', 'json')
  69. try {
  70. await r('https://echo-server.mikeal.now.sh/src/echo.js?body=[asdf]')
  71. throw new Error('Should have failed')
  72. } catch (e) {
  73. assert.ok(e.message.startsWith('Unexpected token a in JSON'))
  74. }
  75. })
  76. const getError = async () => {
  77. const r = bent(201)
  78. try {
  79. await r('https://echo-server.mikeal.now.sh/src/echo.js?body="asdf"')
  80. throw new Error('Should have failed')
  81. } catch (e) {
  82. ttype(e, 'StatusError')
  83. return e
  84. }
  85. }
  86. test('error decodings', async () => {
  87. let e = await getError()
  88. same(await e.text(), '"asdf"')
  89. e = await getError()
  90. same(await e.json(), 'asdf')
  91. })
  92. if (!process.browser) {
  93. test('Z_BUF_ERROR error', async () => {
  94. const request = bent('json')
  95. try {
  96. await request('https://echo-server.mikeal.now.sh/src/echo.js?headers=content-encoding%3Agzip%2Ccontent-type%3Aapplication%2Fjson')
  97. } catch (e) {
  98. ttype(e, 'Error')
  99. return e
  100. }
  101. })
  102. test('gzip json compresssion SyntaxError', async () => {
  103. const request = bent('json')
  104. const base64 = zlib.gzipSync('ok').toString('base64')
  105. const headers = 'content-encoding:gzip,content-type:application/json'
  106. try {
  107. await request(`https://echo-server.mikeal.now.sh/src/echo.js?${qs.stringify({ base64, headers })}`)
  108. } catch (e) {
  109. ttype(e, 'SyntaxError')
  110. return e
  111. }
  112. })
  113. }