From 27a72a12b772cfa74b519714aca6e3b6fdaa5eeb Mon Sep 17 00:00:00 2001 From: ayame113 <40050810+ayame113@users.noreply.github.com> Date: Wed, 20 Jul 2022 19:30:41 +0900 Subject: [PATCH] chore: align some Web API type definitions to lib.dom.d.ts (#15219) --- cli/tests/unit/fetch_test.ts | 8 ++++++++ cli/tests/unit/request_test.ts | 7 +++++++ cli/tests/unit/url_test.ts | 12 ++++++++++++ cli/tests/unit/websocket_test.ts | 14 +++++++++++++- ext/fetch/lib.deno_fetch.d.ts | 4 ++-- ext/url/lib.deno_url.d.ts | 2 +- ext/websocket/lib.deno_websocket.d.ts | 2 +- 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 7adbb77216..72660c547b 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -818,6 +818,14 @@ Deno.test(function responseRedirect() { assertEquals(redir.type, "default"); }); +Deno.test(function responseRedirectTakeURLObjectAsParameter() { + const redir = Response.redirect(new URL("https://example.com/")); + assertEquals( + redir.headers.get("Location"), + "https://example.com/", + ); +}); + Deno.test(async function responseWithoutBody() { const response = new Response(); assertEquals(await response.arrayBuffer(), new ArrayBuffer(0)); diff --git a/cli/tests/unit/request_test.ts b/cli/tests/unit/request_test.ts index 41a1d7e2f1..68f7a86c1d 100644 --- a/cli/tests/unit/request_test.ts +++ b/cli/tests/unit/request_test.ts @@ -68,3 +68,10 @@ Deno.test(function customInspectFunction() { ); assertStringIncludes(Deno.inspect(Request.prototype), "Request"); }); + +Deno.test(function requestConstructorTakeURLObjectAsParameter() { + assertEquals( + new Request(new URL("http://foo/")).url, + "http://foo/", + ); +}); diff --git a/cli/tests/unit/url_test.ts b/cli/tests/unit/url_test.ts index 3d4e1aa2ca..0ba848adda 100644 --- a/cli/tests/unit/url_test.ts +++ b/cli/tests/unit/url_test.ts @@ -494,3 +494,15 @@ Deno.test(function urlSearchParamsIdentityPreserved() { const sp2 = u.searchParams; assertStrictEquals(sp1, sp2); }); + +Deno.test(function urlTakeURLObjectAsParameter() { + const url = new URL( + new URL( + "https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat", + ), + ); + assertEquals( + url.href, + "https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat", + ); +}); diff --git a/cli/tests/unit/websocket_test.ts b/cli/tests/unit/websocket_test.ts index f033c5ac4a..6bd35efca1 100644 --- a/cli/tests/unit/websocket_test.ts +++ b/cli/tests/unit/websocket_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -import { assertThrows } from "./test_util.ts"; +import { assertEquals, assertThrows, deferred, fail } from "./test_util.ts"; Deno.test({ permissions: "none" }, function websocketPermissionless() { assertThrows( @@ -7,3 +7,15 @@ Deno.test({ permissions: "none" }, function websocketPermissionless() { Deno.errors.PermissionDenied, ); }); + +Deno.test(async function websocketConstructorTakeURLObjectAsParameter() { + const promise = deferred(); + const ws = new WebSocket(new URL("ws://localhost:4242/")); + assertEquals(ws.url, "ws://localhost:4242/"); + ws.onerror = () => fail(); + ws.onopen = () => ws.close(); + ws.onclose = () => { + promise.resolve(); + }; + await promise; +}); diff --git a/ext/fetch/lib.deno_fetch.d.ts b/ext/fetch/lib.deno_fetch.d.ts index 18c39c8661..79d4354686 100644 --- a/ext/fetch/lib.deno_fetch.d.ts +++ b/ext/fetch/lib.deno_fetch.d.ts @@ -252,7 +252,7 @@ interface RequestInit { /** This Fetch API interface represents a resource request. */ declare class Request implements Body { - constructor(input: RequestInfo, init?: RequestInit); + constructor(input: RequestInfo | URL, init?: RequestInit); /** * Returns the cache mode associated with request, which is a string @@ -385,7 +385,7 @@ declare class Response implements Body { constructor(body?: BodyInit | null, init?: ResponseInit); static json(data: unknown, init?: ResponseInit): Response; static error(): Response; - static redirect(url: string, status?: number): Response; + static redirect(url: string | URL, status?: number): Response; readonly headers: Headers; readonly ok: boolean; diff --git a/ext/url/lib.deno_url.d.ts b/ext/url/lib.deno_url.d.ts index 4dc56b3f68..d3a51a00ef 100644 --- a/ext/url/lib.deno_url.d.ts +++ b/ext/url/lib.deno_url.d.ts @@ -153,7 +153,7 @@ declare class URLSearchParams { /** The URL interface represents an object providing static methods used for creating object URLs. */ declare class URL { - constructor(url: string, base?: string | URL); + constructor(url: string | URL, base?: string | URL); static createObjectURL(blob: Blob): string; static revokeObjectURL(url: string): void; diff --git a/ext/websocket/lib.deno_websocket.d.ts b/ext/websocket/lib.deno_websocket.d.ts index 8b79fa5cc5..70d0bd61d5 100644 --- a/ext/websocket/lib.deno_websocket.d.ts +++ b/ext/websocket/lib.deno_websocket.d.ts @@ -40,7 +40,7 @@ interface WebSocketEventMap { * If you are looking to create a WebSocket server, please take a look at `Deno.upgradeWebSocket()`. */ declare class WebSocket extends EventTarget { - constructor(url: string, protocols?: string | string[]); + constructor(url: string | URL, protocols?: string | string[]); static readonly CLOSED: number; static readonly CLOSING: number;