Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
136 lines (129 loc) · 4.91 KB
/
Copy pathserver_test.go
File metadata and controls
136 lines (129 loc) · 4.91 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
package main
import (
"bytes"
"net"
"os"
"strings"
"testing"
)
func TestSourceFileName(t *testing.T) {
s := &server{doc: true}
for urlPath, want := range map[string]string{
"/_examples/media.slide": "examples/media.slide",
"/talks/go/intro.slide": "talks/go/intro.slide",
"/_examplesx/a.slide": "_examplesx/a.slide",
"/_examples/../mine.slide": "mine.slide",
} {
if got := sourceFileName(s.file(urlPath)); got != want {
t.Errorf("sourceFileName of %s = %q, want %q", urlPath, got, want)
}
}
}
func TestOriginURL(t *testing.T) {
hostname, _ := os.Hostname()
local := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 4321}
anyAddr := &net.TCPAddr{IP: net.IPv4zero, Port: 4321}
for _, tt := range []struct {
addr *net.TCPAddr
httpAddr, originHost string
want string
}{
{local, "127.0.0.1:4321", "", "http://127.0.0.1:4321"},
{local, "localhost:4321", "", "http://localhost:4321"},
{local, "127.0.0.1:0", "", "http://127.0.0.1:4321"},
{anyAddr, "0.0.0.0:4321", "", "http://" + net.JoinHostPort(hostname, "4321")},
{local, "127.0.0.1:4321", "example.com", "http://example.com:4321"},
{local, "127.0.0.1:4321", "http://example.com", "http://example.com:4321"},
{local, "127.0.0.1:4321", "https://example.com", "https://example.com:4321"},
} {
got, err := originURL(tt.addr, tt.httpAddr, tt.originHost)
if err != nil || got.String() != tt.want {
t.Errorf("originURL(%v, %q, %q) = %v, %v; want %s", tt.addr, tt.httpAddr, tt.originHost, got, err, tt.want)
}
}
}
func TestRenderDocSource(t *testing.T) {
pr, err := newRenderer(embedFS, func(path string) string { return path }, pageSettings{})
if err != nil {
t.Fatal(err)
}
for _, doc := range []bool{false, true} {
s := &server{renderer: pr, doc: doc}
var buf bytes.Buffer
if err := s.renderDoc(&buf, examplesRoot, "intro.slide", "/_examples/intro.slide"); err != nil {
t.Fatal(err)
}
page := buf.String()
if got := strings.Contains(page, `<aside id="source"`); got != doc {
t.Errorf("with -doc=%v, page has source panel: %v", doc, got)
}
if !doc {
continue
}
for _, want := range []string{
`<div class="src-name">examples/intro.slide</div>`,
// Lines are numbered as in the presentation file.
`<span class="src-n">131</span><span class="src-heading">## Links and HTML` + "\n" + `</span>`,
`<span class="src-n">135</span><span class="src-command">.html html/badge.html` + "\n" + `</span>`,
// The included HTML is shown as text, numbered from 1.
`<pre class="src-lines"><span class="src-n">1</span><span><p style=`,
} {
if !strings.Contains(page, want) {
t.Errorf("page does not contain %q", want)
}
}
}
}
func TestRenderDocStyles(t *testing.T) {
pr, err := newRenderer(embedFS, func(path string) string { return path }, pageSettings{})
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
writeFiles(t, dir, map[string]string{
"talk/x.slide": "# Talk\n\n## Title\n\nText\n",
"talk/style.css": "/* style */",
"talk/slide.css": "/* slide */",
"talk/x.slide.css": "/* x.slide */",
"talk/doc.article": "# Article\n\n## Section\n\nText\n",
"talk/article.css": "/* article */",
"talk/doc.article.css": "/* doc.article */",
})
s := &server{content: dirRoot(dir), renderer: pr}
// Test slide styles
var slideBuf bytes.Buffer
if err := s.renderDoc(&slideBuf, s.content, "talk/x.slide", "/talk/x.slide"); err != nil {
t.Fatal(err)
}
slidePage := slideBuf.String()
iStyle := strings.Index(slidePage, `<link rel='stylesheet' href='style.css'>`)
iSlide := strings.Index(slidePage, `<link rel='stylesheet' href='slide.css'>`)
iXSlide := strings.Index(slidePage, `<link rel='stylesheet' href='x.slide.css'>`)
if iStyle == -1 || iSlide == -1 || iXSlide == -1 {
t.Fatalf("slide page missing styles: iStyle=%d, iSlide=%d, iXSlide=%d", iStyle, iSlide, iXSlide)
}
if !(iStyle < iSlide && iSlide < iXSlide) {
t.Errorf("slide styles in wrong order: %d < %d < %d", iStyle, iSlide, iXSlide)
}
if strings.Contains(slidePage, "article.css") {
t.Errorf("slide page should not load article.css")
}
// Test article styles
var articleBuf bytes.Buffer
if err := s.renderDoc(&articleBuf, s.content, "talk/doc.article", "/talk/doc.article"); err != nil {
t.Fatal(err)
}
articlePage := articleBuf.String()
iArtStyle := strings.Index(articlePage, `<link rel='stylesheet' href='style.css'>`)
iArt := strings.Index(articlePage, `<link rel='stylesheet' href='article.css'>`)
iDocArt := strings.Index(articlePage, `<link rel='stylesheet' href='doc.article.css'>`)
if iArtStyle == -1 || iArt == -1 || iDocArt == -1 {
t.Fatalf("article page missing styles: iArtStyle=%d, iArt=%d, iDocArt=%d", iArtStyle, iArt, iDocArt)
}
if !(iArtStyle < iArt && iArt < iDocArt) {
t.Errorf("article styles in wrong order: %d < %d < %d", iArtStyle, iArt, iDocArt)
}
if strings.Contains(articlePage, "slide.css") {
t.Errorf("article page should not load slide.css")
}
}