-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_url_html_test.go
181 lines (176 loc) · 3.57 KB
/
get_url_html_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
package main
import (
"fmt"
"reflect"
"strings"
"testing"
)
func Test(t *testing.T) {
type testCase struct {
name string
inputURL string
inputBody string
expected []string
errorContains string
}
tests := []testCase{
{
name: "absolute URL",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href="https://blog.boot.dev">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: []string{"https://blog.boot.dev"},
},
{
name: "relative URL",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href="/path/one">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: []string{"https://blog.boot.dev/path/one"},
},
{
name: "absolute and relative URLs",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href="/path/one">
<span>Boot.dev</span>
</a>
<a href="https://other.com/path/one">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: []string{"https://blog.boot.dev/path/one", "https://other.com/path/one"},
},
{
name: "no href",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a>
<span>Boot.dev></span>
</a>
</body>
</html>
`,
expected: nil,
},
{
name: "bad HTML",
inputURL: "https://blog.boot.dev",
inputBody: `
<html body>
<a href="path/one">
<span>Boot.dev></span>
</a>
</html body>
`,
expected: []string{"https://blog.boot.dev/path/one"},
},
{
name: "invalid href URL",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href=":\\invalidURL">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: nil,
},
{
name: "handle invalid base URL",
inputURL: `:\\invalidBaseURL`,
inputBody: `
<html>
<body>
<a href="/path">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: nil,
errorContains: "couldn't parse base URL",
},
}
passCount := 0
failCount := 0
for i, test := range tests {
actual, err := getURLsFromHTML(test.inputBody, test.inputURL)
if err != nil && !strings.Contains(err.Error(), test.errorContains){
failCount++
t.Errorf(`---------------------------------
Test %v - '%s'
Input: (%v, %v)
Expected: %v
Actual: %v
Fail: expected error contains '%v', but got '%v'
`, i, test.name, test.inputBody, test.inputURL, test.expected, actual, test.errorContains, err)
} else if err != nil && test.errorContains == "" {
failCount++
t.Errorf(`---------------------------------
Test %v - '%s'
Input: (%v, %v)
Expected: %v
Actual: %v
Fail: unexpected error: '%v'
`, i, test.name, test.inputBody, test.inputURL, test.expected, actual, err)
} else if err == nil && test.errorContains != "" {
failCount++
t.Errorf(`---------------------------------
Test %v - '%s'
Input: (%v, %v)
Expected: %v
Actual: %v
Fail: expected error contains '%v', but got none
`, i, test.name, test.inputBody, test.inputURL, test.expected, actual, test.errorContains)
} else if !reflect.DeepEqual(actual, test.expected) {
failCount++
t.Errorf(`---------------------------------
Test %v - '%s'
Input: (%v, %v)
Expected: %v
Actual: %v
Fail
`, i, test.name, test.inputBody, test.inputURL, test.expected, actual)
} else {
passCount++
fmt.Printf(`---------------------------------
Test %v - '%s'
Input: (%v, %v)
Expected: %v
Actual: %v
Pass
`, i, test.name, test.inputBody, test.inputURL, test.expected, actual)
}
}
}
/*
Fail Error Case Map
Result vs Actual
1. expects a specific error got a differnet error
2. expect an error got no error
3. does not expect error got an error
*/