Skip to content

Commit 0c1d2b9

Browse files
Jake ChampionJakeChampion
authored andcommitted
validate status is in range
fixes #1213
1 parent 8984692 commit 0c1d2b9

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

fetch.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ export function Response(bodyInit, options) {
462462

463463
this.type = 'default'
464464
this.status = options.status === undefined ? 200 : options.status
465+
if (this.status < 200 || this.status > 599) {
466+
throw new RangeError("Failed to construct 'Response': The status provided (0) is outside the range [200, 599].")
467+
}
465468
this.ok = this.status >= 200 && this.status < 300
466469
this.statusText = options.statusText === undefined ? '' : '' + options.statusText
467470
this.headers = new Headers(options.headers)
@@ -481,7 +484,8 @@ Response.prototype.clone = function() {
481484
}
482485

483486
Response.error = function() {
484-
var response = new Response(null, {status: 0, statusText: ''})
487+
var response = new Response(null, {status: 200, statusText: ''})
488+
response.status = 0
485489
response.type = 'error'
486490
return response
487491
}

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,21 @@ exercise.forEach(function(exerciseMode) {
623623
Response('{"foo":"bar"}', {headers: {'content-type': 'application/json'}})
624624
})
625625
})
626+
test('status outside inclusive range 200-599 ', function() {
627+
assert.throws(function() {
628+
new Response('', {status: 199})
629+
})
630+
for (var i = 0; i < 200; i++) {
631+
assert.throws(function() {
632+
new Response('', {status: i})
633+
})
634+
}
635+
for (i = 999; i > 599; i--) {
636+
assert.throws(function() {
637+
new Response('', {status: i})
638+
})
639+
}
640+
})
626641
test('creates Headers object from raw headers', function() {
627642
var r = new Response('{"foo":"bar"}', {headers: {'content-type': 'application/json'}})
628643
assert.equal(r.headers instanceof Headers, true)

0 commit comments

Comments
 (0)