-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgluahttpscrape_test.go
146 lines (125 loc) · 4.34 KB
/
gluahttpscrape_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
package gluahttpscrape
import (
"strings"
"testing"
"github.com/yuin/gopher-lua"
)
var httpBody = `<!DOCTYPE html><html><body><h1 id=\"testid\" class=\"testclass\" href=\"testhref\">My First Heading</h1><h1 id=\"testid\" class=\"testclass\" href=\"testhref2\">My First Heading</h1><p>My first paragraph.</p></body></html>`
func TestFindAttrByClass(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_attr_by_class("`+httpBody+`", "href", "testclass")
assert_equal("testhref", response[1])
assert_equal("testhref2", response[2])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindAttrById(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_attr_by_id("`+httpBody+`", "href", "testid")
assert_equal("testhref", response[1])
assert_equal("testhref2", response[2])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindAttrByTag(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_attr_by_tag("`+httpBody+`", "href", "h1")
assert_equal("testhref", response[1])
assert_equal("testhref2", response[2])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindAttrsByClass(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_attrs_by_class("`+httpBody+`", 2, "id", "href", "testclass")
assert_equal("testhref", response[1]["href"])
assert_equal("testid", response[1]["id"])
assert_equal("testid", response[2]["id"])
assert_equal("testhref2", response[2]["href"])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindAttrsById(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_attrs_by_id("`+httpBody+`", 2, "id", "href", "testid")
assert_equal("testhref", response[1]["href"])
assert_equal("testid", response[1]["id"])
assert_equal("testid", response[2]["id"])
assert_equal("testhref2", response[2]["href"])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindAttrsByTag(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_attrs_by_tag("`+httpBody+`", 2, "id", "href", "h1")
assert_equal("testhref", response[1]["href"])
assert_equal("testid", response[1]["id"])
assert_equal("testid", response[2]["id"])
assert_equal("testhref2", response[2]["href"])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindTextByClass(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_text_by_class("`+httpBody+`", "testclass")
assert_equal("My First Heading", response[1])
assert_equal("My First Heading", response[2])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindTextById(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_text_by_id("`+httpBody+`", "testid")
assert_equal("My First Heading", response[1])
assert_equal("My First Heading", response[2])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func TestFindTextByTag(t *testing.T) {
if err := evalLua(t, `
local scrape = require("scrape")
response, error = scrape.find_text_by_tag("`+httpBody+`", "h1")
assert_equal("My First Heading", response[1])
assert_equal("My First Heading", response[2])
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err)
}
}
func evalLua(t *testing.T, script string) error {
L := lua.NewState()
defer L.Close()
L.PreloadModule("scrape", NewHttpScrapeModule().Loader)
L.SetGlobal("assert_equal", L.NewFunction(func(L *lua.LState) int {
expected := L.Get(1)
actual := L.Get(2)
if expected.Type() != actual.Type() || expected.String() != actual.String() {
t.Errorf("Expected %s %q, got %s %q", expected.Type(), expected, actual.Type(), actual)
}
return 0
}))
L.SetGlobal("assert_contains", L.NewFunction(func(L *lua.LState) int {
contains := L.Get(1)
actual := L.Get(2)
if !strings.Contains(actual.String(), contains.String()) {
t.Errorf("Expected %s %q contains %s %q", actual.Type(), actual, contains.Type(), contains)
}
return 0
}))
return L.DoString(script)
}