request-errors-test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const nock = require('nock')
  2. const GitHub = require('../../')
  3. require('../mocha-node-setup')
  4. describe('request errors', () => {
  5. it('timeout', () => {
  6. nock('https://request-errors-test.com')
  7. .get('/orgs/myorg')
  8. .socketDelay(2000)
  9. .reply(200, {})
  10. const github = new GitHub({
  11. baseUrl: 'https://request-errors-test.com',
  12. timeout: 1000
  13. })
  14. return github.orgs.get({org: 'myorg'})
  15. .catch(error => {
  16. expect(error.name).to.equal('HttpError')
  17. expect(error.code).to.equal(504)
  18. expect(error).to.have.property('stack')
  19. })
  20. })
  21. it('500', () => {
  22. nock('https://request-errors-test.com')
  23. .get('/orgs/myorg')
  24. .replyWithError('ooops')
  25. const github = new GitHub({
  26. baseUrl: 'https://request-errors-test.com'
  27. })
  28. return github.orgs.get({org: 'myorg'})
  29. .catch(error => {
  30. expect(error.name).to.equal('HttpError')
  31. expect(error.code).to.equal(500)
  32. expect(error).to.have.property('stack')
  33. })
  34. })
  35. it('404', () => {
  36. nock('https://request-errors-test.com')
  37. .get('/orgs/myorg')
  38. .reply(404, 'not found')
  39. const github = new GitHub({
  40. baseUrl: 'https://request-errors-test.com',
  41. timeout: 1000
  42. })
  43. return github.orgs.get({org: 'myorg'})
  44. .catch(error => {
  45. expect(error.name).to.equal('HttpError')
  46. expect(error.code).to.equal(404)
  47. expect(error).to.have.property('stack')
  48. })
  49. })
  50. it('401', () => {
  51. nock('https://request-errors-test.com')
  52. .get('/orgs/myorg')
  53. .reply(401)
  54. const github = new GitHub({
  55. baseUrl: 'https://request-errors-test.com',
  56. timeout: 1000
  57. })
  58. return github.orgs.get({org: 'myorg'})
  59. .catch(error => {
  60. expect(error.name).to.equal('HttpError')
  61. expect(error.code).to.equal(401)
  62. expect(error).to.have.property('stack')
  63. })
  64. })
  65. it('error headers', () => {
  66. nock('https://request-errors-test.com')
  67. .get('/orgs/myorg')
  68. .reply(401, {}, {
  69. 'x-foo': 'bar'
  70. })
  71. const github = new GitHub({
  72. baseUrl: 'https://request-errors-test.com',
  73. timeout: 1000
  74. })
  75. return github.orgs.get({org: 'myorg'})
  76. .catch(error => {
  77. expect(error.name).to.equal('HttpError')
  78. expect(error.code).to.equal(401)
  79. expect(error.headers).to.deep.equal({
  80. 'content-type': 'application/json',
  81. 'x-foo': 'bar'
  82. })
  83. })
  84. })
  85. })