-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathurlencode_test.go
More file actions
92 lines (83 loc) · 2.7 KB
/
urlencode_test.go
File metadata and controls
92 lines (83 loc) · 2.7 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package httpsign
import "testing"
// Correctly identifies unreserved alphanumeric characters as not needing escape
func TestUnreservedAlphanumericCharacters(t *testing.T) {
for c := byte('a'); c <= byte('z'); c++ {
if shouldEscape(c, encodePath) {
t.Errorf("Expected %c not to be escaped", c)
}
}
for c := byte('A'); c <= byte('Z'); c++ {
if shouldEscape(c, encodePath) {
t.Errorf("Expected %c not to be escaped", c)
}
}
for c := byte('0'); c <= byte('9'); c++ {
if shouldEscape(c, encodePath) {
t.Errorf("Expected %c not to be escaped", c)
}
}
}
// Handles empty input or non-ASCII characters gracefully
// Ensure that empty input is correctly identified as not needing escape.
func TestEmptyInput(t *testing.T) {
if !shouldEscape(0, encodePath) {
t.Error("Expected empty input to be escaped")
}
}
// Properly escapes characters in encodeQueryComponent and encodeQueryComponentForSignature modes
func TestEscapeInQueryComponentModes(t *testing.T) {
reservedChars := []byte{'$', '&', '+', ',', '/', ':', ';', '=', '?', '@'}
for _, mode := range []encoding{encodeQueryComponent, encodeQueryComponentForSignature} {
for _, c := range reservedChars {
if !shouldEscape(c, mode) {
t.Errorf("shouldEscape(%q, %v) = false; want true", c, mode)
}
}
}
}
// Encodes spaces as '+' when mode is encodeQueryComponent
func TestEncodeSpacesAsPlus(t *testing.T) {
input := "hello world"
expected := "hello+world"
result := escape(input, encodeQueryComponent)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
// Handles empty strings without errors
func TestHandleEmptyString(t *testing.T) {
input := ""
expected := ""
result := escape(input, encodeQueryComponent)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
// Encodes characters as '%XX' when they should be escaped
func TestEscapeEncodesCharacters(t *testing.T) {
input := "hello world!"
expected := "hello%20world%21"
result := escape(input, encodeQueryComponentForSignature)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
// Returns the original string if no characters need escaping
func TestEscapeReturnsOriginalString(t *testing.T) {
input := "helloworld"
expected := "helloworld"
result := escape(input, encodeQueryComponent)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
// Handles strings with mixed characters requiring different encodings
func TestEscapeHandlesMixedCharacters(t *testing.T) {
input := "hello world! @2023"
expected := "hello+world%21+%402023"
result := escape(input, encodeQueryComponent)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}