fixes https://github.com/JakeChampion/fetch/issues/1213
This commit is contained in:
Jake Champion 2023-07-17 23:38:00 +01:00 коммит произвёл Jake Champion
Родитель 8984692515
Коммит 0c1d2b97b7
2 изменённых файлов: 20 добавлений и 1 удалений

Просмотреть файл

@ -462,6 +462,9 @@ export function Response(bodyInit, options) {
this.type = 'default'
this.status = options.status === undefined ? 200 : options.status
if (this.status < 200 || this.status > 599) {
throw new RangeError("Failed to construct 'Response': The status provided (0) is outside the range [200, 599].")
}
this.ok = this.status >= 200 && this.status < 300
this.statusText = options.statusText === undefined ? '' : '' + options.statusText
this.headers = new Headers(options.headers)
@ -481,7 +484,8 @@ Response.prototype.clone = function() {
}
Response.error = function() {
var response = new Response(null, {status: 0, statusText: ''})
var response = new Response(null, {status: 200, statusText: ''})
response.status = 0
response.type = 'error'
return response
}

Просмотреть файл

@ -623,6 +623,21 @@ exercise.forEach(function(exerciseMode) {
Response('{"foo":"bar"}', {headers: {'content-type': 'application/json'}})
})
})
test('status outside inclusive range 200-599 ', function() {
assert.throws(function() {
new Response('', {status: 199})
})
for (var i = 0; i < 200; i++) {
assert.throws(function() {
new Response('', {status: i})
})
}
for (i = 999; i > 599; i--) {
assert.throws(function() {
new Response('', {status: i})
})
}
})
test('creates Headers object from raw headers', function() {
var r = new Response('{"foo":"bar"}', {headers: {'content-type': 'application/json'}})
assert.equal(r.headers instanceof Headers, true)