summaryrefslogtreecommitdiff
path: root/std/encoding/base64url_test.ts
blob: 2af9096a41bf93df2f7622208044f80961828bc3 (plain)
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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

const { test } = Deno;
import { assertEquals } from "../testing/asserts.ts";
import { encode, decode } from "./base64url.ts";

const testsetString = [
  ["", ""],
  ["f", "Zg"],
  ["fo", "Zm8"],
  ["foo", "Zm9v"],
  ["foob", "Zm9vYg"],
  ["fooba", "Zm9vYmE"],
  ["foobar", "Zm9vYmFy"],
  [">?>d?ß", "Pj8-ZD_f"],
];

const testsetBinary = [
  [new TextEncoder().encode("\x00"), "AA"],
  [new TextEncoder().encode("\x00\x00"), "AAA"],
  [new TextEncoder().encode("\x00\x00\x00"), "AAAA"],
  [new TextEncoder().encode("\x00\x00\x00\x00"), "AAAAAA"],
];

test("[encoding/base64url] testBase64urlEncodeString", () => {
  for (const [input, output] of testsetString) {
    assertEquals(encode(input), output);
  }
});

test("[encoding/base64url] testBase64urlEncodeBinary", () => {
  for (const [input, output] of testsetBinary) {
    assertEquals(encode(input), output);
  }
});

test("[encoding/base64ur] testBase64urDecodeBinary", () => {
  for (const [input, output] of testsetBinary) {
    const outputBinary = new Uint8Array(decode(output as string));
    assertEquals(outputBinary, input as Uint8Array);
  }
});