Catch HEAD errors (#1460)

This commit is contained in:
Tomas Della Vedova
2021-05-03 16:40:09 +02:00
committed by delvedor
parent 8e638e1cfa
commit a3344aa11c
2 changed files with 6 additions and 4 deletions

View File

@ -306,8 +306,8 @@ class Transport {
return callback(err, result)
}
} else {
// cast to boolean if the request method was HEAD
result.body = isHead === true ? true : payload
// cast to boolean if the request method was HEAD and there was no error
result.body = isHead === true && result.statusCode < 400 ? true : payload
}
// we should ignore the statusCode if the user has configured the `ignore` field with

View File

@ -1613,7 +1613,7 @@ test('Should cast to boolean HEAD request', t => {
})
t.test('4xx response', t => {
t.plan(2)
t.plan(3)
const pool = new ConnectionPool({ Connection: MockConnection })
pool.addConnection('http://localhost:9200')
@ -1633,12 +1633,13 @@ test('Should cast to boolean HEAD request', t => {
path: '/400'
}, (err, { body, statusCode }) => {
t.ok(err instanceof ResponseError)
t.false(typeof err.body === 'boolean')
t.strictEqual(statusCode, 400)
})
})
t.test('5xx response', t => {
t.plan(2)
t.plan(3)
const pool = new ConnectionPool({ Connection: MockConnection })
pool.addConnection('http://localhost:9200')
@ -1657,6 +1658,7 @@ test('Should cast to boolean HEAD request', t => {
path: '/500'
}, (err, { body, statusCode }) => {
t.ok(err instanceof ResponseError)
t.false(typeof err.body === 'boolean')
t.strictEqual(statusCode, 500)
})
})