File: url_test.go

package info (click to toggle)
golang-goji 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,179 bytes parent folder | download
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
package pat

import (
	"net/url"
	"testing"
)

var HexTexts = []struct {
	input byte
	ishex bool
	unhex byte
}{
	{'0', true, 0},
	{'4', true, 4},
	{'a', true, 10},
	{'F', true, 15},
	{'h', false, 0},
	{'^', false, 0},
}

func TestHex(t *testing.T) {
	t.Parallel()

	for _, test := range HexTexts {
		if actual := ishex(test.input); actual != test.ishex {
			t.Errorf("ishex(%v) == %v, expected %v", test.input, actual, test.ishex)
		}
		if actual := unhex(test.input); actual != test.unhex {
			t.Errorf("unhex(%v) == %v, expected %v", test.input, actual, test.unhex)
		}
	}
}

var UnescapeTests = []struct {
	input  string
	err    error
	output string
}{
	{"hello", nil, "hello"},
	{"file%20one%26two", nil, "file one&two"},
	{"one/two%2fthree", nil, "one/two/three"},
	{"this%20is%0not%valid", url.EscapeError("%0n"), ""},
}

func TestUnescape(t *testing.T) {
	t.Parallel()

	for _, test := range UnescapeTests {
		if actual, err := unescape(test.input); err != test.err {
			t.Errorf("unescape(%q) had err %v, expected %q", test.input, err, test.err)
		} else if actual != test.output {
			t.Errorf("unescape(%q) = %q, expected %q)", test.input, actual, test.output)
		}
	}
}