-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisallow_test.go
More file actions
156 lines (146 loc) · 3.33 KB
/
Copy pathdisallow_test.go
File metadata and controls
156 lines (146 loc) · 3.33 KB
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
package main
import (
"flag"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestDisallow(t *testing.T) {
tests := []struct {
name string
global_want bool
chap21_want bool
robots_text string
}{
{
name: "allow_all",
global_want: true,
chap21_want: true,
robots_text: `
User-agent: *
Disallow:
`,
},
{
name: "disallow_all",
global_want: false,
chap21_want: false,
robots_text: `
User-agent: *
Disallow: /
`,
},
{
name: "allow_chap21_only",
global_want: false,
chap21_want: true,
robots_text: `
User-agent: *
Disallow: /
Allow: */The%20Project%20Gutenberg%20eBook%20of%20The%20Prince,%20by%20Nicolo%20Machiavelli/index.html
Allow: *chap21.html
`,
},
{
name: "disallow_chap21_only",
global_want: true,
chap21_want: false,
robots_text: `
User-agent: *
Disallow: *chap21.html
`,
},
{
name: "allow_over_disallow_chap21",
global_want: true,
chap21_want: true,
robots_text: `
User-agent: *
Disallow: *chap21.html
Allow: *chap21.html
`,
},
}
// Start file server
fs := http.FileServer(http.Dir("./SearchEngine"))
ts := httptest.NewServer(http.StripPrefix("/", fs))
defer ts.Close()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create stop words list
stop_words, err := CreateStopWordSet("stopwords-en.json")
if err != nil {
t.Fatal("Could not create stop words list")
return
}
// Parse flags to get data_handler type
flag.Parse()
args := flag.Args()
var data_handler DataHandler
if len(args) >= 2 && args[0] == "ondisk" {
var err error
data_handler, err = NewSQLDataBase(args[1])
if err != nil {
data_handler = NewInvIdx()
}
} else {
data_handler = NewInvIdx()
}
t.Cleanup(func() {
data_handler.DeleteDataBase()
})
// Create robots.txt
robots_path := filepath.Join("SearchEngine", "robots.txt")
if err := os.WriteFile(robots_path, []byte(tc.robots_text), 0644); err != nil {
t.Fatalf("could not write root robots.txt: %v", err)
}
t.Cleanup(func() {
_ = os.Remove(robots_path)
})
// Start crawl and get hrefs
crawler := NewCrawler(data_handler, stop_words, 0.001)
startURL := ts.URL + "/top10/index.html"
_, err = crawler.Crawl(
startURL,
struct {
clean_href int
download int
extract int
}{16, 16, 16},
)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
hrefs, err := data_handler.GetAllDocs()
if err != nil {
t.Fatalf("Data handler GetAllWordCount failed: %v", err)
}
// Try to find something thats not chap21 or a path to it (ie chap22)
global_got := false
for href := range hrefs {
if strings.HasSuffix(href, "chap22.html") {
global_got = true
break
}
}
// Try to find chap21
chap21_got := false
for href := range hrefs {
if strings.HasSuffix(href, "chap21.html") {
chap21_got = true
break
}
}
// Check against desired outputs
if global_got != tc.global_want {
t.Fatalf("test %s: global crawled want=%v, got=%v", tc.name, tc.global_want, global_got)
}
if chap21_got != tc.chap21_want {
t.Fatalf("test %s: chap21 crawled want=%v, got=%v", tc.name, tc.chap21_want, chap21_got)
}
})
}
}