Skip to content

Commit

Permalink
chore: align some Web API type definitions to lib.dom.d.ts (#15219)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayame113 authored Jul 20, 2022
1 parent b8e1250 commit 27a72a1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cli/tests/unit/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
7 changes: 7 additions & 0 deletions cli/tests/unit/request_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
);
});
12 changes: 12 additions & 0 deletions cli/tests/unit/url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]:8000/qux/quux?foo=bar&baz=12#qat",
),
);
assertEquals(
url.href,
"https://foo:[email protected]:8000/qux/quux?foo=bar&baz=12#qat",
);
});
14 changes: 13 additions & 1 deletion cli/tests/unit/websocket_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
// 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(
() => new WebSocket("ws://localhost"),
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;
});
4 changes: 2 additions & 2 deletions ext/fetch/lib.deno_fetch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ext/url/lib.deno_url.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion ext/websocket/lib.deno_websocket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 27a72a1

Please sign in to comment.