mirror of
https://github.com/denoland/deno.git
synced 2025-02-08 15:21:26 -05:00
fix(cli/fetch): response constructor default properties (#6650)
This commit is contained in:
parent
5b09e721d3
commit
3b4260dc54
2 changed files with 43 additions and 35 deletions
|
@ -34,14 +34,14 @@ export class Response extends Body.Body implements domTypes.Response {
|
||||||
const extraInit = responseData.get(init) || {};
|
const extraInit = responseData.get(init) || {};
|
||||||
let { type = "default", url = "" } = extraInit;
|
let { type = "default", url = "" } = extraInit;
|
||||||
|
|
||||||
let status = (Number(init.status) || 0) ?? 200;
|
let status = init.status === undefined ? 200 : Number(init.status || 0);
|
||||||
let statusText = init.statusText ?? "";
|
let statusText = init.statusText ?? "";
|
||||||
let headers =
|
let headers =
|
||||||
init.headers instanceof Headers
|
init.headers instanceof Headers
|
||||||
? init.headers
|
? init.headers
|
||||||
: new Headers(init.headers);
|
: new Headers(init.headers);
|
||||||
|
|
||||||
if (init.status && (status < 200 || status > 599)) {
|
if (init.status !== undefined && (status < 200 || status > 599)) {
|
||||||
throw new RangeError(
|
throw new RangeError(
|
||||||
`The status provided (${init.status}) is outside the range [200, 599]`
|
`The status provided (${init.status}) is outside the range [200, 599]`
|
||||||
);
|
);
|
||||||
|
@ -117,7 +117,7 @@ export class Response extends Body.Body implements domTypes.Response {
|
||||||
this.statusText = statusText;
|
this.statusText = statusText;
|
||||||
this.status = extraInit.status || status;
|
this.status = extraInit.status || status;
|
||||||
this.headers = headers;
|
this.headers = headers;
|
||||||
this.redirected = extraInit.redirected;
|
this.redirected = extraInit.redirected || false;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -851,9 +851,7 @@ unitTest(
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
unitTest(
|
unitTest(function fetchResponseConstructorNullBody(): void {
|
||||||
{ perms: { net: true } },
|
|
||||||
function fetchResponseConstructorNullBody(): void {
|
|
||||||
const nullBodyStatus = [204, 205, 304];
|
const nullBodyStatus = [204, 205, 304];
|
||||||
|
|
||||||
for (const status of nullBodyStatus) {
|
for (const status of nullBodyStatus) {
|
||||||
|
@ -868,18 +866,17 @@ unitTest(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
unitTest(
|
unitTest(function fetchResponseConstructorInvalidStatus(): void {
|
||||||
{ perms: { net: true } },
|
const invalidStatus = [101, 600, 199, null, "", NaN];
|
||||||
function fetchResponseConstructorInvalidStatus(): void {
|
|
||||||
const invalidStatus = [101, 600, 199];
|
|
||||||
|
|
||||||
for (const status of invalidStatus) {
|
for (const status of invalidStatus) {
|
||||||
try {
|
try {
|
||||||
|
// deno-lint-ignore ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
new Response("deno", { status });
|
new Response("deno", { status });
|
||||||
fail("Invalid status");
|
fail(`Invalid status: ${status}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
assert(e instanceof RangeError);
|
assert(e instanceof RangeError);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
|
@ -888,5 +885,16 @@ unitTest(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
unitTest(function fetchResponseEmptyConstructor(): void {
|
||||||
|
const response = new Response();
|
||||||
|
assertEquals(response.status, 200);
|
||||||
|
assertEquals(response.body, null);
|
||||||
|
assertEquals(response.type, "default");
|
||||||
|
assertEquals(response.url, "");
|
||||||
|
assertEquals(response.redirected, false);
|
||||||
|
assertEquals(response.ok, true);
|
||||||
|
assertEquals(response.bodyUsed, false);
|
||||||
|
assertEquals([...response.headers], []);
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue