-
Notifications
You must be signed in to change notification settings - Fork 9
/
url_test.go
189 lines (179 loc) · 5.18 KB
/
url_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package php_test
import (
"strings"
"github.com/hyperjiang/php"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("URL Functions", func() {
It("Base64Encode", func() {
tests := []struct {
input string
want string
}{
{"This is an encoded string", "VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=="},
{"abc123!?$*&()'-=@~", "YWJjMTIzIT8kKiYoKSctPUB+"},
}
for _, t := range tests {
Expect(php.Base64Encode(t.input)).To(Equal(t.want))
}
})
It("Base64Decode", func() {
tests := []struct {
input string
want string
wantErr bool
}{
{"VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==", "This is an encoded string", false},
{"YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~", false},
{"#iu3498r", "", true},
{"asiudfh9w=8uihf", "", true},
}
for _, t := range tests {
s, err := php.Base64Decode(t.input)
if t.wantErr {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal(t.want))
}
}
})
It("GetHeaders", func() {
h, err := php.GetHeaders("https://httpbin.org/")
Expect(err).NotTo(HaveOccurred())
Expect(len(h)).To(BeNumerically(">", 0))
_, err = php.GetHeaders("http://un.reachable.url/")
Expect(err).To(HaveOccurred())
})
It("GetMetaTags", func() {
metaTags, err := php.GetMetaTags("https://httpbin.org/")
Expect(err).NotTo(HaveOccurred())
Expect(len(metaTags)).To(BeNumerically(">", 0))
_, err = php.GetMetaTags("http://un.reachable.url/")
Expect(err).To(HaveOccurred())
})
It("ParseDocument", func() {
doc := `
<html prefix="og: http://ogp.me/ns#">
<head>
<title>The Rock (1996)</title>
<meta charset="UTF-8">
<meta name="description" content="MMMK" />
<meta property="" content="your mom" />
<!-- meta property="title" content="The Rock" /-->
<meta property="og:title" content="The Rock" />
<meta property="og:TYPE" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
<meta property="og:image" content="http://ia.media-imdb.com/images/cheese.jpg" />
<meta property="og:cows " content="mooo" />
</head>
</html>
`
expected := map[string]string{
"description": "MMMK",
"title": "The Rock (1996)",
"og:title": "The Rock",
"og:type": "video.movie",
"og:url": "http://www.imdb.com/title/tt0117500/",
"og:image": "http://ia.media-imdb.com/images/cheese.jpg",
"og:cows": "mooo",
}
data := php.ParseDocument(strings.NewReader(doc))
Expect(data).To(Equal(expected))
doc2 := "invalid doc"
data2 := php.ParseDocument(strings.NewReader(doc2))
Expect(len(data2)).To(BeZero())
})
It("HTTPBuildQuery", func() {
data := make(map[string]any)
Expect(php.HTTPBuildQuery(data)).To(BeEmpty())
data["foo"] = "bar"
data["baz"] = "boom"
data["cow"] = "milk"
data["php"] = "hypertext processor"
query := php.HTTPBuildQuery(data)
Expect(query).To(ContainSubstring("foo=bar"))
Expect(query).To(ContainSubstring("baz=boom"))
Expect(query).To(ContainSubstring("cow=milk"))
Expect(query).To(ContainSubstring("php=hypertext+processor"))
data2 := map[string]any{
"user": map[string]any{
"name": "Bob Smith",
"age": 47,
"sex": "M",
"dob": "5/12/1956",
},
"pastimes": []string{"golf", "opera", "poker", "rap"},
}
query = php.HTTPBuildQuery(data2)
Expect(query).To(ContainSubstring("pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&pastimes%5B2%5D=poker&pastimes%5B3%5D=rap"))
Expect(query).To(ContainSubstring("user%5Bage%5D=47"))
Expect(query).To(ContainSubstring("user%5Bdob%5D=5%2F12%2F1956"))
Expect(query).To(ContainSubstring("user%5Bname%5D=Bob+Smith"))
Expect(query).To(ContainSubstring("user%5Bsex%5D=M"))
})
It("ParseURL", func() {
url, err := php.ParseURL("https://httpbin.org:80/")
Expect(err).NotTo(HaveOccurred())
Expect(url.Scheme).To(Equal("https"))
Expect(url.Host).To(Equal("httpbin.org:80"))
Expect(url.Path).To(Equal("/"))
Expect(url.Port()).To(Equal("80"))
_, err = php.ParseURL("::::")
Expect(err).To(HaveOccurred())
})
It("RawURLDecode", func() {
tests := []struct {
input string
want string
}{
{"my=apples&are=green+and+red", "my=apples&are=green+and+red"},
{"one%20%26%20two", "one & two"},
{"myvar=%BA", "myvar=\xba"},
{"myvar=%B", ""},
}
for _, t := range tests {
Expect(php.RawURLDecode(t.input)).To(Equal(t.want))
}
})
It("RawURLEncode", func() {
tests := []struct {
input string
want string
}{
{"some=weird/value", "some=weird%2Fvalue"},
{" ", "%20"},
}
for _, t := range tests {
Expect(php.RawURLEncode(t.input)).To(Equal(t.want))
}
})
It("URLDecode", func() {
tests := []struct {
input string
want string
}{
{"my=apples&are=green+and+red", "my=apples&are=green and red"},
{"one%20%26%20two", "one & two"},
{"myvar=%BA", "myvar=\xba"},
{"myvar=%B", ""},
}
for _, t := range tests {
Expect(php.URLDecode(t.input)).To(Equal(t.want))
}
})
It("URLEncode", func() {
tests := []struct {
input string
want string
}{
{"some=weird/value", "some%3Dweird%2Fvalue"},
{" ", "+"},
}
for _, t := range tests {
Expect(php.URLEncode(t.input)).To(Equal(t.want))
}
})
})