-
Notifications
You must be signed in to change notification settings - Fork 41
/
html_test.go
64 lines (56 loc) · 1.54 KB
/
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
package antch
import (
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/antchfx/htmlquery"
)
func TestMediaTypeParse(t *testing.T) {
s := "text/html; charset=utf-8"
m := ParseMediaType(s)
if v := m.ContentType(); v != s {
t.Errorf("ContentType() = %s; want %s", v, s)
}
s = "text/html"
m = ParseMediaType(s)
if m.Charset != "" {
t.Errorf("Charset = %s; want empty", m.Charset)
}
if g, e := m.Type, "text/html"; g != e {
t.Errorf("Type = %s; want %s", g, e)
}
}
var testHTML = `<html><head><meta charset="utf-8"></head><body>abc,这是中文内容</body> </html>`
func TestParseHTML(t *testing.T) {
res := &http.Response{
Header: map[string][]string{
"Content-Type": []string{"text/html; charset=utf-8"},
},
Body: ioutil.NopCloser(strings.NewReader(testHTML)),
}
doc, err := ParseHTML(res)
if err != nil {
t.Fatalf("ParseHTML failed: %v", err)
}
body := htmlquery.FindOne(doc, "//body")
if g, e := strings.TrimSpace(htmlquery.InnerText(body)), "abc,这是中文内容"; g != e {
t.Errorf("body expected is %s; but got %s", e, g)
}
}
func TestParseHTMLWithoutEncoding(t *testing.T) {
res := &http.Response{
Header: map[string][]string{
"Content-Type": []string{"text/html"},
},
Body: ioutil.NopCloser(strings.NewReader(testHTML)),
}
doc, err := ParseHTML(res)
if err != nil {
t.Fatalf("ParseHTML failed: %v", err)
}
body := htmlquery.FindOne(doc, "//body")
if g, e := strings.TrimSpace(htmlquery.InnerText(body)), "abc,这是中文内容"; g != e {
t.Errorf("body expected is %s; but got %s", e, g)
}
}