/
server_test.go
77 lines (68 loc) · 1.71 KB
/
server_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
package revealgo
import (
"bytes"
"net/http"
"net/http/httptest"
"regexp"
"testing"
)
func TestContentHandler(t *testing.T) {
param := ServerParam{
Path: "testdata/example.md",
Theme: "beige.css",
Transition: "fade",
Separator: "^===",
VerticalSeparator: "^----",
}
ts := httptest.NewServer(contentHandler(param, http.FileServer(http.Dir("."))))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Fatal("unexpected", err)
}
if res.StatusCode != 200 {
t.Error("server status error")
}
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(res.Body)
s := buf.String()
matches := []struct {
regexp string
}{
{regexp: "revealjs/dist/theme/beige.css"},
{regexp: `data-markdown="testdata/example.md"`},
{regexp: `|| 'zoom',`},
{regexp: `data-separator="\^==="`},
{regexp: `data-separator-vertical="\^----"`},
}
for _, m := range matches {
r := regexp.MustCompile(m.regexp)
if r.MatchString(s) == false {
t.Errorf("content do not match %v\n", m.regexp)
}
}
r2, err := http.Get(ts.URL + "/testdata/markdown.svg")
if err != nil {
t.Fatal("unexpected", err)
}
if r2.StatusCode != 200 {
t.Errorf("server status error\n")
}
if r2.Header.Get("Content-Type") != "image/svg+xml" {
t.Errorf("content type error\n")
}
}
func TestAssetHandler(t *testing.T) {
ts := httptest.NewServer(assetsHandler("/assets/", http.FileServer(http.FS(revealjs))))
defer ts.Close()
res, err := http.Get(ts.URL + "/revealjs/js/reveal.js")
if err != nil {
t.Errorf("unexpected\n")
}
if res.StatusCode != 200 {
t.Errorf("server status error\n")
}
if res.Header.Get("Content-Type") != "application/javascript" {
t.Errorf("content type error\n")
}
}