forked from go-shiori/go-readability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_test.go
196 lines (171 loc) · 5.54 KB
/
read_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
190
191
192
193
194
195
196
package readability
import (
nurl "net/url"
"strings"
"testing"
"time"
"github.com/PuerkitoBio/goquery"
)
func BenchmarkReadability(b *testing.B) {
urls := []string{
"https://www.nytimes.com/2018/01/21/technology/inside-amazon-go-a-store-of-the-future.html",
"http://www.dwmkerr.com/the-death-of-microservice-madness-in-2018/",
"https://www.eurekalert.org/pub_releases/2018-01/uoe-stt011118.php",
"http://www.slate.com/articles/arts/books/2018/01/the_reviewer_s_fallacy_when_critics_aren_t_critical_enough.html",
"https://www.theatlantic.com/business/archive/2018/01/german-board-games-catan/550826/?single_page=true",
"http://www.weeklystandard.com/the-anti-bamboozler/article/2011032",
"http://www.inquiriesjournal.com/articles/1657/the-impact-of-listening-to-music-on-cognitive-performance",
}
for _, url := range urls {
parsedURL, _ := nurl.Parse(url)
FromURL(parsedURL, 5*time.Second)
}
}
func Test_removeScripts(t *testing.T) {
// Load test file
testDoc, err := createDocFromFile("test/removeScripts.html")
if err != nil {
t.Errorf("Failed to open test file: %v", err)
}
// Remove scripts and get HTML
removeScripts(testDoc)
html, err := testDoc.Html()
if err != nil {
t.Errorf("Failed to read HTML: %v", err)
}
// Compare results
html = rxSpaces.ReplaceAllString(html, "")
want := "<!DOCTYPE html>" +
"<html><head><title>Test Remove Scripts</title></head>" +
"<body></body></html>"
if html != want {
t.Errorf("Want: %s\nGot: %s", want, html)
}
}
func Test_replaceBrs(t *testing.T) {
// Load test file
testDoc, err := createDocFromFile("test/removeBrs.html")
if err != nil {
t.Errorf("Failed to open test file: %v", err)
}
// Replace BRs and get HTML
replaceBrs(testDoc)
html, err := testDoc.Html()
if err != nil {
t.Errorf("Failed to read HTML: %v", err)
}
// Compare results
html = rxSpaces.ReplaceAllString(html, "")
want := "<!DOCTYPE html>" +
"<html><head><title>Test Remove BRs</title></head>" +
"<body><div>foo<br/>bar<p>a b c</p></div></body></html>"
if html != want {
t.Errorf("Want: %s\nGot: %s", want, html)
}
}
func Test_prepDocument(t *testing.T) {
// Load test file
testDoc, err := createDocFromFile("test/prepDocument.html")
if err != nil {
t.Errorf("Failed to open test file: %v", err)
}
// Prep document and get HTML
prepDocument(testDoc)
html, err := testDoc.Html()
if err != nil {
t.Errorf("Failed to read HTML: %v", err)
}
// Compare results
html = rxSpaces.ReplaceAllString(html, "")
want := "<!DOCTYPE html>" +
"<html><head><title>Test Prep Documents</title></head>" +
"<body><span>Bip bop</span>" +
"<div>foo<br/>bar<p>a b c</p></div>" +
"</body></html>"
if html != want {
t.Errorf("Want: %s\nGot: %s", want, html)
}
}
func Test_getArticleTitle(t *testing.T) {
tests := make(map[string]string)
tests["test/getArticleTitle1.html"] = "Test Get Article Title 1"
tests["test/getArticleTitle2.html"] = "Get Awesome Article Title 2"
tests["test/getArticleTitle3.html"] = "Test: Get Article Title 3"
for path, want := range tests {
// Load test file
testDoc, err := createDocFromFile(path)
if err != nil {
t.Errorf("Failed to open test file: %v", err)
}
// Get title and compare it
title := getArticleTitle(testDoc)
if title != want {
t.Errorf("Want: %s\nGot: %s", want, title)
}
}
}
func Test_getArticleMetadata(t *testing.T) {
tests := make(map[string]Metadata)
tests["test/getArticleMetadata1.html"] = Metadata{
Title: "Just-released Minecraft exploit makes it easy to crash game servers",
Image: "http://cdn.arstechnica.net/wp-content/uploads/2015/04/server-crash-640x426.jpg",
Excerpt: "Two-year-old bug exposes thousands of servers to crippling attack.",
}
tests["test/getArticleMetadata2.html"] = Metadata{
Title: "Daring Fireball: Colophon",
}
for path, want := range tests {
// Load test file
testDoc, err := createDocFromFile(path)
if err != nil {
t.Errorf("Failed to open test file: %v", err)
}
// Get metadata and compare it
metadata := getArticleMetadata(testDoc)
if metadata.Title != want.Title || metadata.Image != want.Image || metadata.Excerpt != want.Excerpt {
t.Errorf("Want: '%s',%s,'%s'\nGot: '%s',%s,'%s'",
want.Title, want.Image, want.Excerpt,
metadata.Title, metadata.Image, metadata.Excerpt)
}
}
}
func Test_hasSinglePInsideElement(t *testing.T) {
scenario1 := `<div>Hello</div>`
scenario2 := `<div><p>Hello</p></div>`
scenario3 := `<div><p>Hello</p><p>this is test</p></div>`
tests := map[string]bool{
scenario1: false,
scenario2: true,
scenario3: false,
}
for test, want := range tests {
// Generate test document
reader := strings.NewReader(test)
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
t.Errorf("Failed to generate test document: %v", err)
}
// Check element
result := hasSinglePInsideElement(doc.Find("div").First())
if result != want {
t.Errorf("%s\nWant: %t got: %t", test, want, result)
}
}
}
func Test_toAbsoluteURI(t *testing.T) {
base, _ := nurl.ParseRequestURI("http://localhost:8080")
tests := map[string]string{
"/test/123": "http://localhost:8080/test/123",
"test/123": "http://localhost:8080/test/123",
"https://www.google.com": "https://www.google.com",
"ftp://ftp.server.com": "ftp://ftp.server.com",
"www.google.com": "http://localhost:8080/www.google.com",
"http//www.google.com": "http://localhost:8080/http//www.google.com",
}
for test, want := range tests {
result := toAbsoluteURI(test, base)
if result != want {
t.Errorf("%s\nWant: %s got: %s", test, want, result)
}
}
}