forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_test.go
66 lines (59 loc) · 1.43 KB
/
setup_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
package main
import (
"fmt"
"net/http"
"os"
"testing"
"time"
)
const rootURL = "http://localhost:8080"
const existentURL = "http://localhost:8080/foo"
const nonExistentURL = "http://localhost:8080/bar"
const erroneousURL = "http://localhost:8080/erroneous"
const fragmentURL = "http://localhost:8080/fragment"
const tagsURL = "http://localhost:8080/tags"
type handler struct{}
func (handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "", "/":
w.Write([]byte(htmlWithBody(`<a href="/foo" />`)))
case "/foo":
w.Write([]byte(htmlWithBody(`<a href="/" />`)))
case "/erroneous":
w.Write([]byte(htmlWithBody(`
<a href=":" />
<a href="mailto:[email protected]" />
<a href="/bar" />
<a href="#foo" />
`)))
case "/fragment":
w.Write([]byte(htmlWithBody(`<a id="foo" href="#foo" />`)))
case "/tags":
// TODO: Test <frame> tag.
w.Write([]byte(htmlWithBody(`
<a href="/" />
<iframe src="/"></iframe>
<img src="/foo.png" />
<link href="/" />
<script src="/foo.js"></script>
<source src="/foo.png" />
<track src="/foo.vtt" />
`)))
case "/foo.js":
w.Write(nil)
case "/foo.png":
w.Write(nil)
case "/foo.vtt":
w.Write(nil)
default:
w.WriteHeader(404)
}
}
func TestMain(m *testing.M) {
go http.ListenAndServe(":8080", handler{})
time.Sleep(time.Millisecond)
os.Exit(m.Run())
}
func htmlWithBody(b string) string {
return fmt.Sprintf(`<html><body>%v</body></html>`, b)
}