2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-10-09 01:12:44 -04:00
|
|
|
import { assert, assertEquals, assertThrows, unitTest } from "./test_util.ts";
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function fromInit(): void {
|
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
|
|
|
});
|
|
|
|
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
assertEquals("ahoyhoy", (req as any)._bodySource);
|
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
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function fromRequest(): void {
|
2020-10-09 01:12:44 -04:00
|
|
|
const r = new Request("http://foo/");
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(r as any)._bodySource = "ahoyhoy";
|
2019-10-28 12:41:36 -04:00
|
|
|
r.headers.set("test-header", "value");
|
|
|
|
|
|
|
|
const req = new Request(r);
|
|
|
|
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
assertEquals((req as any)._bodySource, (r as any)._bodySource);
|
2019-10-28 12:41:36 -04:00
|
|
|
assertEquals(req.url, r.url);
|
|
|
|
assertEquals(req.headers.get("test-header"), r.headers.get("test-header"));
|
|
|
|
});
|
|
|
|
|
2020-10-09 01:12:44 -04:00
|
|
|
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.");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function cloneRequestBodyStream(): Promise<void> {
|
2019-10-28 12:41:36 -04:00
|
|
|
// hack to get a stream
|
2020-10-09 01:12:44 -04:00
|
|
|
const stream = new Request("http://foo/", { body: "a test body" }).body;
|
|
|
|
const r1 = new Request("http://foo/", {
|
2020-03-28 13:03:49 -04:00
|
|
|
body: stream,
|
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);
|
|
|
|
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
assert((r1 as any)._bodySource !== (r2 as any)._bodySource);
|
2019-10-28 12:41:36 -04:00
|
|
|
});
|