2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assertEquals, assertStringIncludes } from "./test_util.ts";
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fromInit() {
|
2020-10-09 01:12:44 -04:00
|
|
|
const req = new Request("http://foo/", {
|
2019-05-01 23:56:42 -04:00
|
|
|
body: "ahoyhoy",
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
2020-03-28 13:03:49 -04:00
|
|
|
"test-header": "value",
|
|
|
|
},
|
2019-05-01 23:56:42 -04:00
|
|
|
});
|
|
|
|
|
2021-01-28 15:37:21 -05:00
|
|
|
assertEquals("ahoyhoy", await req.text());
|
2020-10-09 01:12:44 -04:00
|
|
|
assertEquals(req.url, "http://foo/");
|
2019-05-01 23:56:42 -04:00
|
|
|
assertEquals(req.headers.get("test-header"), "value");
|
|
|
|
});
|
2019-10-28 12:41:36 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function requestNonString() {
|
2020-10-09 01:12:44 -04:00
|
|
|
const nonString = {
|
|
|
|
toString() {
|
|
|
|
return "http://foo/";
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// deno-lint-ignore ban-ts-comment
|
|
|
|
// @ts-expect-error
|
|
|
|
assertEquals(new Request(nonString).url, "http://foo/");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function methodNonString() {
|
2020-10-26 10:02:08 -04:00
|
|
|
assertEquals(new Request("http://foo/", { method: undefined }).method, "GET");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function requestRelativeUrl() {
|
2021-01-07 13:06:08 -05:00
|
|
|
assertEquals(
|
|
|
|
new Request("relative-url").url,
|
2024-02-08 15:09:47 -05:00
|
|
|
"http://127.0.0.1:4545/relative-url",
|
2021-01-07 13:06:08 -05:00
|
|
|
);
|
2020-10-09 01:12:44 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function cloneRequestBodyStream() {
|
2019-10-28 12:41:36 -04:00
|
|
|
// hack to get a stream
|
2021-04-20 08:47:22 -04:00
|
|
|
const stream =
|
|
|
|
new Request("http://foo/", { body: "a test body", method: "POST" }).body;
|
2020-10-09 01:12:44 -04:00
|
|
|
const r1 = new Request("http://foo/", {
|
2020-03-28 13:03:49 -04:00
|
|
|
body: stream,
|
2021-04-20 08:47:22 -04:00
|
|
|
method: "POST",
|
2019-10-28 12:41:36 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const r2 = r1.clone();
|
|
|
|
|
|
|
|
const b1 = await r1.text();
|
|
|
|
const b2 = await r2.text();
|
|
|
|
|
|
|
|
assertEquals(b1, b2);
|
|
|
|
});
|
2021-04-23 14:38:45 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function customInspectFunction() {
|
2021-04-23 14:38:45 -04:00
|
|
|
const request = new Request("https://example.com");
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(request),
|
|
|
|
`Request {
|
|
|
|
bodyUsed: false,
|
|
|
|
headers: Headers {},
|
|
|
|
method: "GET",
|
|
|
|
redirect: "follow",
|
|
|
|
url: "https://example.com/"
|
|
|
|
}`,
|
|
|
|
);
|
2021-07-08 09:43:36 -04:00
|
|
|
assertStringIncludes(Deno.inspect(Request.prototype), "Request");
|
2021-04-23 14:38:45 -04:00
|
|
|
});
|
2022-07-20 06:30:41 -04:00
|
|
|
|
|
|
|
Deno.test(function requestConstructorTakeURLObjectAsParameter() {
|
|
|
|
assertEquals(
|
|
|
|
new Request(new URL("http://foo/")).url,
|
|
|
|
"http://foo/",
|
|
|
|
);
|
|
|
|
});
|