Merge pull request #4239 from overleaf/ae-html-error-message

Avoid using HTML responses as error messages

GitOrigin-RevId: 4cb8df259f51ff351c6f22fa82c016068ff87880
This commit is contained in:
Shane Kilkelly
2021-06-25 02:06:08 +00:00
committed by Copybot
parent 46c00c13ba
commit 7df57174ca
2 changed files with 95 additions and 10 deletions
@@ -62,6 +62,80 @@ describe('fetchJSON', function () {
})
})
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({})
}
})
it('handles POST requests', function () {
const body = { example: true }