fetch-json.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { expect } from 'chai'
  2. import fetchMock from 'fetch-mock'
  3. import { Response } from 'node-fetch'
  4. import {
  5. deleteJSON,
  6. FetchError,
  7. getJSON,
  8. postJSON,
  9. putJSON,
  10. } from '../../../frontend/js/infrastructure/fetch-json'
  11. describe('fetchJSON', function () {
  12. before(function () {
  13. fetchMock.restore()
  14. })
  15. afterEach(function () {
  16. fetchMock.restore()
  17. })
  18. const headers = {
  19. Accept: 'application/json',
  20. 'Content-Type': 'application/json',
  21. }
  22. it('handles GET requests', function () {
  23. fetchMock.once(
  24. { method: 'GET', url: '/test', headers },
  25. { status: 200, body: { result: 'success' } }
  26. )
  27. return expect(getJSON('/test')).to.eventually.deep.equal({
  28. result: 'success',
  29. })
  30. })
  31. it('handles 4xx responses', function () {
  32. fetchMock.get('/test', {
  33. status: 400,
  34. body: { message: 'The request was invalid' },
  35. })
  36. return expect(getJSON('/test'))
  37. .to.eventually.be.rejectedWith('Bad Request')
  38. .and.be.an.instanceOf(FetchError)
  39. .to.nested.include({
  40. message: 'Bad Request',
  41. 'data.message': 'The request was invalid',
  42. 'response.status': 400,
  43. 'info.statusCode': 400,
  44. })
  45. })
  46. it('handles 5xx responses', async function () {
  47. fetchMock.get('/test', { status: 500 })
  48. return expect(getJSON('/test'))
  49. .to.eventually.be.rejectedWith('Internal Server Error')
  50. .and.be.an.instanceOf(FetchError)
  51. .to.nested.include({
  52. 'response.status': 500,
  53. 'info.statusCode': 500,
  54. })
  55. })
  56. it('handles JSON error responses', async function () {
  57. fetchMock.get('/test', {
  58. status: 500,
  59. headers: {
  60. 'Content-Type': 'application/json',
  61. },
  62. body: { message: 'lorem ipsum' },
  63. })
  64. return expect(getJSON('/test'))
  65. .to.eventually.be.rejectedWith('Internal Server Error')
  66. .and.be.an.instanceOf(FetchError)
  67. .to.nested.include({
  68. 'data.message': 'lorem ipsum',
  69. })
  70. })
  71. it('handles text error responses', async function () {
  72. fetchMock.get('/test', {
  73. status: 500,
  74. headers: {
  75. 'Content-Type': 'text/plain',
  76. },
  77. body: 'lorem ipsum',
  78. })
  79. return expect(getJSON('/test'))
  80. .to.eventually.be.rejectedWith('Internal Server Error')
  81. .and.be.an.instanceOf(FetchError)
  82. .to.nested.include({
  83. 'data.message': 'lorem ipsum',
  84. })
  85. })
  86. it('handles text error responses sent as HTML', async function () {
  87. fetchMock.get('/test', {
  88. status: 500,
  89. headers: {
  90. 'Content-Type': 'text/html',
  91. },
  92. body: 'lorem ipsum',
  93. })
  94. return expect(getJSON('/test'))
  95. .to.eventually.be.rejectedWith('Internal Server Error')
  96. .and.be.an.instanceOf(FetchError)
  97. .to.nested.include({
  98. 'data.message': 'lorem ipsum',
  99. })
  100. })
  101. it('handles (ignores) HTML error responses sent as HTML', async function () {
  102. fetchMock.get('/test', {
  103. status: 500,
  104. headers: {
  105. 'Content-Type': 'text/html',
  106. },
  107. body:
  108. '<!doctype html><html lang="en"><body><p>lorem ipsum</p></body></html>',
  109. })
  110. const promise = getJSON('/test')
  111. expect(promise)
  112. .to.eventually.be.rejectedWith('Internal Server Error')
  113. .and.be.an.instanceOf(FetchError)
  114. try {
  115. await promise
  116. } catch (error) {
  117. expect(error.data).to.eql({})
  118. }
  119. })
  120. it('handles 5xx responses without a status message', async function () {
  121. // It's hard to make a Response object with statusText=null,
  122. // so we need to do some monkey-work to make it happen
  123. const response = new Response('weird scary error', {
  124. ok: false,
  125. status: 599,
  126. })
  127. Object.defineProperty(response, 'statusText', {
  128. get: () => null,
  129. set: () => {},
  130. })
  131. fetchMock.get('/test', response)
  132. return expect(getJSON('/test'))
  133. .to.eventually.be.rejectedWith('Unexpected Error: 599')
  134. .and.be.an.instanceOf(FetchError)
  135. .to.nested.include({
  136. 'response.status': 599,
  137. 'info.statusCode': 599,
  138. message: 'Unexpected Error: 599',
  139. })
  140. })
  141. it('handles POST requests', function () {
  142. const body = { example: true }
  143. fetchMock.once(
  144. { method: 'POST', url: '/test', headers, body },
  145. { status: 200, body: { result: 'success' } }
  146. )
  147. return expect(postJSON('/test', { body })).to.eventually.deep.equal({
  148. result: 'success',
  149. })
  150. })
  151. it('handles PUT requests', function () {
  152. const body = { example: true }
  153. fetchMock.once(
  154. { method: 'PUT', url: '/test', headers, body },
  155. { status: 200, body: { result: 'success' } }
  156. )
  157. return expect(putJSON('/test', { body })).to.eventually.deep.equal({
  158. result: 'success',
  159. })
  160. })
  161. it('handles DELETE requests', function () {
  162. fetchMock.once({ method: 'DELETE', url: '/test', headers }, { status: 204 })
  163. return expect(deleteJSON('/test')).to.eventually.deep.equal({})
  164. })
  165. })