fix(fetch): Support 101 status code (#6059)

This commit is contained in:
Marcos Casagrande 2020-06-03 15:43:11 +02:00 committed by GitHub
parent aaa2ed5a64
commit a1915a0d4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View file

@ -12,7 +12,7 @@ import { DomFileImpl } from "./dom_file.ts";
import { getHeaderValueParams } from "./util.ts"; import { getHeaderValueParams } from "./util.ts";
import { ReadableStreamImpl } from "./streams/readable_stream.ts"; import { ReadableStreamImpl } from "./streams/readable_stream.ts";
const NULL_BODY_STATUS = [/* 101, */ 204, 205, 304]; const NULL_BODY_STATUS = [101, 204, 205, 304];
const REDIRECT_STATUS = [301, 302, 303, 307, 308]; const REDIRECT_STATUS = [301, 302, 303, 307, 308];
const responseData = new WeakMap(); const responseData = new WeakMap();
@ -115,7 +115,7 @@ export class Response extends Body.Body implements domTypes.Response {
this.url = url; this.url = url;
this.statusText = statusText; this.statusText = statusText;
this.status = status; this.status = extraInit.status || status;
this.headers = headers; this.headers = headers;
this.redirected = extraInit.redirected; this.redirected = extraInit.redirected;
this.type = type; this.type = type;
@ -329,7 +329,7 @@ export async function fetch(
} }
responseInit = { responseInit = {
status: fetchResponse.status, status: 200,
statusText: fetchResponse.statusText, statusText: fetchResponse.statusText,
headers: fetchResponse.headers, headers: fetchResponse.headers,
}; };
@ -337,6 +337,7 @@ export async function fetch(
responseData.set(responseInit, { responseData.set(responseInit, {
redirected, redirected,
rid: fetchResponse.bodyRid, rid: fetchResponse.bodyRid,
status: fetchResponse.status,
url, url,
}); });

View file

@ -767,7 +767,7 @@ unitTest(
unitTest( unitTest(
{ perms: { net: true } }, { perms: { net: true } },
async function fetchNullBodyStatus(): Promise<void> { async function fetchNullBodyStatus(): Promise<void> {
const nullBodyStatus = [204, 205, 304]; const nullBodyStatus = [101, 204, 205, 304];
for (const status of nullBodyStatus) { for (const status of nullBodyStatus) {
const headers = new Headers([["x-status", String(status)]]); const headers = new Headers([["x-status", String(status)]]);
@ -801,3 +801,23 @@ unitTest(
} }
} }
); );
unitTest(
{ perms: { net: true } },
function fetchResponseConstructorInvalidStatus(): void {
const invalidStatus = [101, 600, 199];
for (const status of invalidStatus) {
try {
new Response("deno", { status });
fail("Invalid status");
} catch (e) {
assert(e instanceof RangeError);
assertEquals(
e.message,
`The status provided (${status}) is outside the range [200, 599]`
);
}
}
}
);