1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(op_crates/fetch): Stringify and parse Request URLs (#7838)

Fixes #7837
This commit is contained in:
Nayeem Rahman 2020-10-09 06:12:44 +01:00 committed by GitHub
parent c06fbc449d
commit 98727b331d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View file

@ -4,7 +4,7 @@ import { assert, assertEquals, unitTest } from "./test_util.ts";
// just a hack to get a body object // just a hack to get a body object
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
function buildBody(body: any, headers?: Headers): Body { function buildBody(body: any, headers?: Headers): Body {
const stub = new Request("", { const stub = new Request("http://foo/", {
body: body, body: body,
headers, headers,
}); });

View file

@ -1,8 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, unitTest } from "./test_util.ts"; import { assert, assertEquals, assertThrows, unitTest } from "./test_util.ts";
unitTest(function fromInit(): void { unitTest(function fromInit(): void {
const req = new Request("https://example.com", { const req = new Request("http://foo/", {
body: "ahoyhoy", body: "ahoyhoy",
method: "POST", method: "POST",
headers: { headers: {
@ -12,12 +12,12 @@ unitTest(function fromInit(): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
assertEquals("ahoyhoy", (req as any)._bodySource); assertEquals("ahoyhoy", (req as any)._bodySource);
assertEquals(req.url, "https://example.com"); assertEquals(req.url, "http://foo/");
assertEquals(req.headers.get("test-header"), "value"); assertEquals(req.headers.get("test-header"), "value");
}); });
unitTest(function fromRequest(): void { unitTest(function fromRequest(): void {
const r = new Request("https://example.com"); const r = new Request("http://foo/");
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
(r as any)._bodySource = "ahoyhoy"; (r as any)._bodySource = "ahoyhoy";
r.headers.set("test-header", "value"); r.headers.set("test-header", "value");
@ -30,10 +30,26 @@ unitTest(function fromRequest(): void {
assertEquals(req.headers.get("test-header"), r.headers.get("test-header")); assertEquals(req.headers.get("test-header"), r.headers.get("test-header"));
}); });
unitTest(function requestNonString(): void {
const nonString = {
toString() {
return "http://foo/";
},
};
// deno-lint-ignore ban-ts-comment
// @ts-expect-error
assertEquals(new Request(nonString).url, "http://foo/");
});
unitTest(function requestRelativeUrl(): void {
// TODO(nayeemrmn): Base from `--location` when implemented and set.
assertThrows(() => new Request("relative-url"), TypeError, "Invalid URL.");
});
unitTest(async function cloneRequestBodyStream(): Promise<void> { unitTest(async function cloneRequestBodyStream(): Promise<void> {
// hack to get a stream // hack to get a stream
const stream = new Request("", { body: "a test body" }).body; const stream = new Request("http://foo/", { body: "a test body" }).body;
const r1 = new Request("https://example.com", { const r1 = new Request("http://foo/", {
body: stream, body: stream,
}); });

View file

@ -985,8 +985,9 @@
this.headers = new Headers(input.headers); this.headers = new Headers(input.headers);
this.credentials = input.credentials; this.credentials = input.credentials;
this._stream = input._stream; this._stream = input._stream;
} else if (typeof input === "string") { } else {
this.url = input; // TODO(nayeemrmn): Base from `--location` when implemented and set.
this.url = new URL(String(input)).href;
} }
if (init && "method" in init) { if (init && "method" in init) {