-
Notifications
You must be signed in to change notification settings - Fork 15
/
rfc2396_test.go
69 lines (61 loc) · 1.43 KB
/
rfc2396_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
package dataurl
import (
"bytes"
"fmt"
"testing"
)
var tests = []struct {
escaped string
unescaped []byte
}{
{"A%20brief%20note%0A", []byte("A brief note\n")},
{"%7B%5B%5Dbyte(%22A%2520brief%2520note%22)%2C%20%5B%5Dbyte(%22A%20brief%20note%22)%7D", []byte(`{[]byte("A%20brief%20note"), []byte("A brief note")}`)},
}
func TestEscape(t *testing.T) {
for _, test := range tests {
escaped := Escape(test.unescaped)
if string(escaped) != test.escaped {
t.Errorf("Expected %s, got %s", test.escaped, string(escaped))
}
}
}
func TestUnescape(t *testing.T) {
for _, test := range tests {
unescaped, err := Unescape(test.escaped)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(unescaped, test.unescaped) {
t.Errorf("Expected %s, got %s", test.unescaped, unescaped)
}
}
}
func ExampleEscapeString() {
fmt.Println(EscapeString("A brief note"))
// Output: A%20brief%20note
}
func ExampleEscape() {
fmt.Println(Escape([]byte("A brief note")))
// Output: A%20brief%20note
}
func ExampleUnescape() {
data, err := Unescape("A%20brief%20note")
if err != nil {
// can fail e.g if incorrect escaped sequence
fmt.Println(err)
return
}
fmt.Println(string(data))
// Output: A brief note
}
func ExampleUnescapeToString() {
s, err := UnescapeToString("A%20brief%20note")
if err != nil {
// can fail e.g if incorrect escaped sequence
fmt.Println(err)
return
}
fmt.Println(s)
// Output: A brief note
}