|
1 | 1 | import { assert, assertEquals } from "../dev_deps.ts";
|
2 | 2 |
|
3 | 3 | import { generateToken } from "../deps.deno.ts";
|
| 4 | +import { hyper, HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts"; |
| 5 | +import { HyperRequest } from "../types.ts"; |
4 | 6 |
|
5 |
| -Deno.test("generateToken", async () => { |
6 |
| - try { |
7 |
| - const res = await generateToken("SUB", "SECRET"); |
8 |
| - assert(true); |
9 |
| - assertEquals(typeof res, "string"); |
10 |
| - // deno-lint-ignore no-explicit-any |
11 |
| - } catch (error: any) { |
12 |
| - assert(false, error.message); |
13 |
| - } |
| 7 | +Deno.test("hyper-request", async (t) => { |
| 8 | + await t.step("generateToken", async (t) => { |
| 9 | + await t.step("should generate a token successfully", async () => { |
| 10 | + try { |
| 11 | + const res = await generateToken("SUB", "SECRET"); |
| 12 | + assert(true); |
| 13 | + assertEquals(typeof res, "string"); |
| 14 | + // deno-lint-ignore no-explicit-any |
| 15 | + } catch (error: any) { |
| 16 | + assert(false, error.message); |
| 17 | + } |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + await t.step("hyper", async (t) => { |
| 22 | + const req: HyperRequest = { |
| 23 | + service: "data", |
| 24 | + method: "GET", |
| 25 | + }; |
| 26 | + |
| 27 | + await t.step("url", async (t) => { |
| 28 | + await t.step("it should append params", async () => { |
| 29 | + const resource = await hyper( |
| 30 | + new URL("cloud://mock.hyper.io/foobar"), |
| 31 | + "default", |
| 32 | + )({ |
| 33 | + ...req, |
| 34 | + params: { |
| 35 | + foo: "bar", |
| 36 | + }, |
| 37 | + }); |
| 38 | + assertEquals( |
| 39 | + resource.url, |
| 40 | + "https://mock.hyper.io/foobar/data/default?foo=bar", |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + await t.step("isCloud", async (t) => { |
| 45 | + const cloudCs = "cloud://mock.hyper.io/foobar"; |
| 46 | + |
| 47 | + await t.step("should build the resource url correctly", async () => { |
| 48 | + const resource = await hyper(new URL(cloudCs), "default")(req); |
| 49 | + assertEquals( |
| 50 | + resource.url, |
| 51 | + "https://mock.hyper.io/foobar/data/default", |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + await t.step("should build the action url correctly", async () => { |
| 56 | + const action = await hyper( |
| 57 | + new URL(cloudCs), |
| 58 | + "default", |
| 59 | + )({ ...req, action: "_bulk" }); |
| 60 | + assertEquals( |
| 61 | + action.url, |
| 62 | + "https://mock.hyper.io/foobar/data/default/_bulk", |
| 63 | + ); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + await t.step("not isCloud", async (t) => { |
| 68 | + const notCloud = "http://localhost:6363/foobar"; |
| 69 | + |
| 70 | + await t.step("should build the resource url correctly", async () => { |
| 71 | + const resource = await hyper(new URL(notCloud), "default")(req); |
| 72 | + assertEquals(resource.url, "http://localhost:6363/data/foobar"); |
| 73 | + }); |
| 74 | + |
| 75 | + await t.step("should build the action url correctly", async () => { |
| 76 | + const action = await hyper( |
| 77 | + new URL(notCloud), |
| 78 | + "default", |
| 79 | + )({ ...req, action: "_bulk" }); |
| 80 | + assertEquals(action.url, "http://localhost:6363/data/foobar/_bulk"); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + await t.step("options", async (t) => { |
| 86 | + await t.step("headers", async (t) => { |
| 87 | + await t.step("should set the Authorization header", async () => { |
| 88 | + const resource = await hyper( |
| 89 | + new URL("http://foo:bar@localhost:6363/foobar"), |
| 90 | + "default", |
| 91 | + )(req); |
| 92 | + assert(resource.options?.headers.has("Authorization")); |
| 93 | + }); |
| 94 | + |
| 95 | + await t.step("should set the Content-Type header", async () => { |
| 96 | + const resource = await hyper( |
| 97 | + new URL("http://foo:bar@localhost:6363/foobar"), |
| 98 | + "default", |
| 99 | + )(req); |
| 100 | + assert(resource.options?.headers.has("Content-Type")); |
| 101 | + }); |
| 102 | + |
| 103 | + await t.step("should set any provided headers", async () => { |
| 104 | + const resource = await hyper( |
| 105 | + new URL("http://foo:bar@localhost:6363/foobar"), |
| 106 | + "default", |
| 107 | + )({ |
| 108 | + ...req, |
| 109 | + headers: new Headers({ [HYPER_LEGACY_GET_HEADER]: "true" }), |
| 110 | + }); |
| 111 | + assert(resource.options?.headers.has(HYPER_LEGACY_GET_HEADER)); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + await t.step("should add the body", async () => { |
| 116 | + const resource = await hyper( |
| 117 | + new URL("http://localhost:6363/foobar"), |
| 118 | + "default", |
| 119 | + )({ ...req, body: { foo: "bar" } }); |
| 120 | + assertEquals(resource.options?.body, JSON.stringify({ foo: "bar" })); |
| 121 | + }); |
| 122 | + |
| 123 | + await t.step("should add the method", async () => { |
| 124 | + const resource = await hyper( |
| 125 | + new URL("http://localhost:6363/foobar"), |
| 126 | + "default", |
| 127 | + )(req); |
| 128 | + assertEquals(resource.options?.method, "GET"); |
| 129 | + |
| 130 | + const resource2 = await hyper( |
| 131 | + new URL("http://localhost:6363/foobar"), |
| 132 | + "default", |
| 133 | + // deno-lint-ignore ban-ts-comment |
| 134 | + // @ts-ignore |
| 135 | + )({ ...req, method: undefined }); |
| 136 | + assertEquals(resource2.options?.method, "GET"); |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
14 | 140 | });
|
0 commit comments