2021-02-05 09:01:37 -05:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
import {
|
|
|
|
deleteJSON,
|
2021-02-10 04:36:54 -05:00
|
|
|
FetchError,
|
2021-02-05 09:01:37 -05:00
|
|
|
getJSON,
|
|
|
|
postJSON,
|
2021-04-27 03:52:58 -04:00
|
|
|
putJSON,
|
2021-02-05 09:01:37 -05:00
|
|
|
} from '../../../frontend/js/infrastructure/fetch-json'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('fetchJSON', function () {
|
|
|
|
before(function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
fetchMock.restore()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
fetchMock.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
const headers = {
|
|
|
|
Accept: 'application/json',
|
2021-04-27 03:52:58 -04:00
|
|
|
'Content-Type': 'application/json',
|
2021-02-05 09:01:37 -05:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handles GET requests', function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
fetchMock.once(
|
|
|
|
{ method: 'GET', url: '/test', headers },
|
|
|
|
{ status: 200, body: { result: 'success' } }
|
|
|
|
)
|
|
|
|
|
|
|
|
return expect(getJSON('/test')).to.eventually.deep.equal({
|
2021-04-27 03:52:58 -04:00
|
|
|
result: 'success',
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handles 4xx responses', function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
fetchMock.get('/test', {
|
|
|
|
status: 400,
|
2021-04-27 03:52:58 -04:00
|
|
|
body: { message: 'The request was invalid' },
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return expect(getJSON('/test'))
|
|
|
|
.to.eventually.be.rejectedWith('Bad Request')
|
2021-02-10 04:36:54 -05:00
|
|
|
.and.be.an.instanceOf(FetchError)
|
2021-02-05 09:01:37 -05:00
|
|
|
.to.nested.include({
|
2021-02-10 04:36:54 -05:00
|
|
|
message: 'Bad Request',
|
|
|
|
'data.message': 'The request was invalid',
|
|
|
|
'response.status': 400,
|
2021-04-27 03:52:58 -04:00
|
|
|
'info.statusCode': 400,
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handles 5xx responses', async function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
fetchMock.get('/test', { status: 500 })
|
|
|
|
|
|
|
|
return expect(getJSON('/test'))
|
|
|
|
.to.eventually.be.rejectedWith('Internal Server Error')
|
2021-02-10 04:36:54 -05:00
|
|
|
.and.be.an.instanceOf(FetchError)
|
2021-02-05 09:01:37 -05:00
|
|
|
.to.nested.include({
|
2021-02-10 04:36:54 -05:00
|
|
|
'response.status': 500,
|
2021-04-27 03:52:58 -04:00
|
|
|
'info.statusCode': 500,
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-24 04:28:23 -04:00
|
|
|
it('handles JSON error responses', async function () {
|
|
|
|
fetchMock.get('/test', {
|
|
|
|
status: 500,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: { message: 'lorem ipsum' },
|
|
|
|
})
|
|
|
|
|
|
|
|
return expect(getJSON('/test'))
|
|
|
|
.to.eventually.be.rejectedWith('Internal Server Error')
|
|
|
|
.and.be.an.instanceOf(FetchError)
|
|
|
|
.to.nested.include({
|
|
|
|
'data.message': 'lorem ipsum',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles text error responses', async function () {
|
|
|
|
fetchMock.get('/test', {
|
|
|
|
status: 500,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
},
|
|
|
|
body: 'lorem ipsum',
|
|
|
|
})
|
|
|
|
|
|
|
|
return expect(getJSON('/test'))
|
|
|
|
.to.eventually.be.rejectedWith('Internal Server Error')
|
|
|
|
.and.be.an.instanceOf(FetchError)
|
|
|
|
.to.nested.include({
|
|
|
|
'data.message': 'lorem ipsum',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles text error responses sent as HTML', async function () {
|
|
|
|
fetchMock.get('/test', {
|
|
|
|
status: 500,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/html',
|
|
|
|
},
|
|
|
|
body: 'lorem ipsum',
|
|
|
|
})
|
|
|
|
|
|
|
|
return expect(getJSON('/test'))
|
|
|
|
.to.eventually.be.rejectedWith('Internal Server Error')
|
|
|
|
.and.be.an.instanceOf(FetchError)
|
|
|
|
.to.nested.include({
|
|
|
|
'data.message': 'lorem ipsum',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles (ignores) HTML error responses sent as HTML', async function () {
|
|
|
|
fetchMock.get('/test', {
|
|
|
|
status: 500,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/html',
|
|
|
|
},
|
|
|
|
body:
|
|
|
|
'<!doctype html><html lang="en"><body><p>lorem ipsum</p></body></html>',
|
|
|
|
})
|
|
|
|
|
|
|
|
const promise = getJSON('/test')
|
|
|
|
|
|
|
|
expect(promise)
|
|
|
|
.to.eventually.be.rejectedWith('Internal Server Error')
|
|
|
|
.and.be.an.instanceOf(FetchError)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await promise
|
|
|
|
} catch (error) {
|
|
|
|
expect(error.data).to.eql({})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handles POST requests', function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
const body = { example: true }
|
|
|
|
|
|
|
|
fetchMock.once(
|
|
|
|
{ method: 'POST', url: '/test', headers, body },
|
|
|
|
{ status: 200, body: { result: 'success' } }
|
|
|
|
)
|
|
|
|
|
|
|
|
return expect(postJSON('/test', { body })).to.eventually.deep.equal({
|
2021-04-27 03:52:58 -04:00
|
|
|
result: 'success',
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handles PUT requests', function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
const body = { example: true }
|
|
|
|
|
|
|
|
fetchMock.once(
|
|
|
|
{ method: 'PUT', url: '/test', headers, body },
|
|
|
|
{ status: 200, body: { result: 'success' } }
|
|
|
|
)
|
|
|
|
|
|
|
|
return expect(putJSON('/test', { body })).to.eventually.deep.equal({
|
2021-04-27 03:52:58 -04:00
|
|
|
result: 'success',
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handles DELETE requests', function () {
|
2021-02-05 09:01:37 -05:00
|
|
|
fetchMock.once({ method: 'DELETE', url: '/test', headers }, { status: 204 })
|
|
|
|
|
|
|
|
return expect(deleteJSON('/test')).to.eventually.deep.equal({})
|
|
|
|
})
|
|
|
|
})
|