-
Notifications
You must be signed in to change notification settings - Fork 14
/
ocp_test.go
151 lines (141 loc) · 3.93 KB
/
ocp_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
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"sort"
"testing"
)
func init() {
flag.Parse()
sem = make(chan bool, throttle)
}
var hostPortHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("close") == "true" {
w.Header().Set("Connection", "close")
}
w.Write([]byte(r.RemoteAddr))
})
func dummyServer(ch chan<- string) *httptest.Server {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ocpdummy %s", r.URL.Path)
ch <- r.URL.Path
})
return httptest.NewServer(h)
}
func TestGetUrlsFromSitemap(t *testing.T) {
f, err := ioutil.TempFile("", "ocp-testsitemap.xml")
if err != nil {
t.Fatal("Couldn't write test sitemap:", f.Name())
}
f.WriteString(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://localhost:8081/a</loc>
<priority>0.4</priority>
</url>
<url>
<loc>http://localhost:8081/b</loc>
<priority>0.6</priority>
</url>
<url>
<loc>http://localhost:8081/c</loc>
<priority>1.0</priority>
</url>
</urlset>`)
f.Close()
urlset, err := getUrlsFromSitemap(f.Name(), true)
if err != nil ||
urlset.Url[0].Loc != "http://localhost:8081/a" ||
urlset.Url[0].Priority != 0.4 ||
urlset.Url[1].Loc != "http://localhost:8081/b" ||
urlset.Url[1].Priority != 0.6 ||
urlset.Url[2].Loc != "http://localhost:8081/c" ||
urlset.Url[2].Priority != 1.0 {
t.Fatal("Incorrectly parsed urlset:", urlset)
}
}
func TestGetUrlsFromSitemapindex(t *testing.T) {
f1, err := ioutil.TempFile("", "ocp-testchild1.xml")
if err != nil {
t.Fatal("Couldn't write test child 1")
}
f1.WriteString(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://localhost:8081/a</loc>
<priority>0.4</priority>
</url>
</urlset>`)
f1.Close()
f2, err := ioutil.TempFile("", "ocp-testchild2.xml")
if err != nil {
t.Fatal("Couldn't write test child 2")
}
f2.WriteString(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://localhost:8081/b</loc>
<priority>0.6</priority>
</url>
</urlset>`)
f2.Close()
f3, err := ioutil.TempFile("", "ocp-testchild3.xml")
if err != nil {
t.Fatal("Couldn't write test child 3")
}
f3.WriteString(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://localhost:8081/c</loc>
<priority>1.0</priority>
</url>
</urlset>`)
f3.Close()
fi, err := ioutil.TempFile("", "ocp-testsitemapindex.xml")
if err != nil {
t.Fatal("Couldn't write test sitemapindex")
}
fmt.Fprintf(fi, `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>%s</loc>
</sitemap>
<sitemap>
<loc>%s</loc>
</sitemap>
<sitemap>
<loc>%s</loc>
</sitemap>
</sitemapindex>`, f1.Name(), f2.Name(), f3.Name())
fi.Close()
urlset, err := getUrlsFromSitemap(fi.Name(), true)
if err != nil ||
urlset.Url[0].Loc != "http://localhost:8081/a" ||
urlset.Url[0].Priority != 0.4 ||
urlset.Url[1].Loc != "http://localhost:8081/b" ||
urlset.Url[1].Priority != 0.6 ||
urlset.Url[2].Loc != "http://localhost:8081/c" ||
urlset.Url[2].Priority != 1.0 {
t.Fatal("Incorrectly parsed urlset:", urlset)
}
}
func TestPrimeUrlset(t *testing.T) {
ch := make(chan string)
s := dummyServer(ch)
defer s.Close()
a := Url{s.URL + "/a", 0.4}
b := Url{s.URL + "/b", 0.6}
c := Url{s.URL + "/c", 1.0}
urlset := &Urlset{Url: []Url{a, b, c}}
sort.Sort(urlset)
go primeUrlset(urlset)
for i := 0; i < 3; i++ {
msg := <-ch
if msg != "/a" && msg != "/b" && msg != "/c" {
t.Error("Web server on :8081 did not acknowledge a, b, c requests")
}
}
}