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
Jake Champion committed Jul 17, 2023
1 parent 8984692 commit 24a8f1c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
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 Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
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 24a8f1c

Please sign in to comment.