Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
62 lines (57 loc) · 1.62 KB
/
Copy pathmain_test.go
File metadata and controls
62 lines (57 loc) · 1.62 KB
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
package main
import "testing"
func TestNormalizeURL(t *testing.T) {
cases := []struct {
in string
want string
ok bool
}{
{"example.com", "https://example.com", true},
{" example.com/api ", "https://example.com/api", true},
{`"http://x.tld/y"`, "http://x.tld/y", true},
{"https://a.tld/p?q=1", "https://a.tld/p?q=1", true},
{"# comment", "", false},
{"", "", false},
{"http://", "", false},
{"ftp://host/x", "", false},
{"javascript:alert(1)", "", false},
}
for _, c := range cases {
got, ok := normalizeURL(c.in)
if ok != c.ok || (ok && got != c.want) {
t.Errorf("normalizeURL(%q) = (%q,%v), want (%q,%v)", c.in, got, ok, c.want, c.ok)
}
}
}
func TestParseHeaders(t *testing.T) {
h := parseHeaders(`X-A: 1\nAuthorization: Bearer z`)
if h["X-A"] != "1" || h["Authorization"] != "Bearer z" {
t.Fatalf("parseHeaders got %v", h)
}
// Real newlines also work; blank and malformed lines are skipped.
h = parseHeaders("A: b\n\nnokeyhere\n: noname\nC: d")
if h["A"] != "b" || h["C"] != "d" {
t.Fatalf("parseHeaders got %v", h)
}
if len(h) != 2 {
t.Fatalf("expected 2 valid headers, got %v", h)
}
}
func TestParseInclude(t *testing.T) {
inc := parseInclude("a=b&c=d&e=")
if inc["a"] != "b" || inc["c"] != "d" {
t.Fatalf("parseInclude got %v", inc)
}
}
func TestParseCodes(t *testing.T) {
codes, err := parseCodes("429, 503 ,418")
if err != nil {
t.Fatalf("parseCodes err: %v", err)
}
if len(codes) != 3 || codes[0] != 429 || codes[2] != 418 {
t.Fatalf("parseCodes got %v", codes)
}
if _, err := parseCodes("429,notanumber"); err == nil {
t.Fatalf("expected error on bad code")
}
}