Skip to content

Commit

Permalink
validate status is in range
Browse files Browse the repository at this point in the history
fixes #1213
  • Loading branch information
JakeChampion committed Jul 17, 2023
1 parent 8984692 commit 0c1d2b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fetch.js
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Expand Up @@ -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)
Expand Down

0 comments on commit 0c1d2b9

Please sign in to comment.