-
Notifications
You must be signed in to change notification settings - Fork 13
/
readurl_test.go
74 lines (67 loc) · 1.56 KB
/
readurl_test.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package forwarder
import (
"net/url"
"testing"
)
var base64Tests = []struct {
decoded, encoded string
}{
// RFC 3548 examples
{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
{"\x14\xfb\x9c\x03", "FPucAw=="},
// RFC 4648 examples
{"", ""},
{"f", "Zg=="},
{"fo", "Zm8="},
{"foo", "Zm9v"},
{"foob", "Zm9vYg=="},
{"fooba", "Zm9vYmE="},
{"foobar", "Zm9vYmFy"},
// Wikipedia examples
{"sure.", "c3VyZS4="},
{"sure", "c3VyZQ=="},
{"sur", "c3Vy"},
{"su", "c3U="},
{"leasure.", "bGVhc3VyZS4="},
{"easure.", "ZWFzdXJlLg=="},
{"asure.", "YXN1cmUu"},
{"sure.", "c3VyZS4="},
}
func TestReadURLData(t *testing.T) {
for i := range base64Tests {
tc := base64Tests[i]
t.Run(tc.encoded, func(t *testing.T) {
u := url.URL{
Scheme: "data",
Opaque: "//base64," + tc.encoded,
}
b, err := ReadURLString(&u, nil)
if err != nil {
t.Fatal(err)
}
if b != tc.decoded {
t.Fatalf("expected %q, got %q", tc.decoded, b)
}
})
}
}
func TestReadFileOrBase64(t *testing.T) {
for i := range base64Tests {
tc := base64Tests[i]
t.Run(tc.encoded, func(t *testing.T) {
b, err := ReadFileOrBase64("data:" + tc.encoded)
if err != nil {
t.Fatal(err)
}
if string(b) != tc.decoded {
t.Fatalf("expected %q, got %q", tc.decoded, b)
}
})
}
}