-
Notifications
You must be signed in to change notification settings - Fork 0
/
compileUrlTemplate.test.js
47 lines (43 loc) · 1.58 KB
/
compileUrlTemplate.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { expect, it } from "bun:test";
import { compileUrlTemplate } from "./compileUrlTemplate";
const defaultOpts = { serialNumber: "01:23:45:67:89:ab:cd" };
it("returns the url in absence of placeholders", async () => {
const template = await compileUrlTemplate("https://grtn.org", defaultOpts);
expect(template.generateUrl()).toBe("https://grtn.org");
expect(template.check("https://grtn.org")).toBe(true);
expect(template.check("https://example.org")).toBe(false);
});
it("replaces serial numbers in {sn}", async () => {
const template = await compileUrlTemplate(
"https://grtn.org/inventory/nfc/{sn}",
defaultOpts
);
expect(template.generateUrl()).toBe(
"https://grtn.org/inventory/nfc/0123456789abcd"
);
expect(template.check("https://grtn.org/inventory/nfc/0123456789abcd")).toBe(
true
);
expect(template.check("https://grtn.org/inventory/nfc/0123456789ABCD")).toBe(
false
);
});
it("supports {hmac:key} syntax", async () => {
const template = await compileUrlTemplate(
"https://grtn.org/inventory/nfc/{sn}/{hmac:secret}",
defaultOpts
);
expect(template.generateUrl()).toBe(
"https://grtn.org/inventory/nfc/0123456789abcd/c489dd5d5d389f7c71b8f65192298189fd058b5f9f5290d9817661747673626f"
);
expect(
template.check(
"https://grtn.org/inventory/nfc/0123456789abcd/c489dd5d5d389f7c71b8f65192298189fd058b5f9f5290d9817661747673626f"
)
).toBe(true);
expect(
template.check(
"https://grtn.org/inventory/nfc/0123456789abcd/c489dd5d5d389f7c71b8f65192298189fd058b5f9f5290d9817661747673626e"
)
).toBe(false);
});