-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
203 lines (176 loc) · 4.99 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-FileCopyrightText: 2024 Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package ciigo_test
import (
"net/http"
"os"
"path/filepath"
"regexp"
"testing"
"git.sr.ht/~shulhan/ciigo"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
"git.sr.ht/~shulhan/pakakeh.go/lib/test/httptest"
)
func TestCiigoOnGet(t *testing.T) {
var tdata *test.Data
var err error
tdata, err = test.LoadData(`testdata/onGet_test.txt`)
if err != nil {
t.Fatal(err)
}
var dirRoot = t.TempDir()
// Create one adoc, one md, and one html file.
for _, name := range []string{`one.adoc`, `two.md`, `three.html`} {
var file = filepath.Join(dirRoot, name)
err = os.WriteFile(file, tdata.Input[name], 0600)
if err != nil {
t.Fatal(err)
}
}
var cigo = ciigo.Ciigo{}
var serveOpts = ciigo.ServeOptions{
Address: `127.0.0.1:11083`,
ConvertOptions: ciigo.ConvertOptions{
Root: dirRoot,
HTMLTemplate: `testdata/onGet_template.gohtml`,
},
}
err = cigo.InitHTTPServer(serveOpts)
if err != nil {
t.Fatal(err)
}
var redactLastUpdated = regexp.MustCompile(`Last updated.*`)
var listCase = []struct {
desc string
req httptest.SimulateRequest
expBody []byte
}{{
desc: `GET /one.html`,
req: httptest.SimulateRequest{
Method: http.MethodGet,
Path: `/one.html`,
},
expBody: tdata.Output[`one.html`],
}, {
desc: `GET /two.html`,
req: httptest.SimulateRequest{
Method: http.MethodGet,
Path: `/two.html`,
},
expBody: tdata.Output[`two.html`],
}, {
desc: `GET /three.html`,
req: httptest.SimulateRequest{
Method: http.MethodGet,
Path: `/three.html`,
},
expBody: tdata.Output[`three.html`],
}}
var result *httptest.SimulateResult
for _, tcase := range listCase {
result, err = httptest.Simulate(cigo.HTTPServer.ServeHTTP,
&tcase.req)
if err != nil {
t.Fatal(err)
}
var gotBody = redactLastUpdated.ReplaceAll(
result.ResponseBody, []byte("[REDACTED]"))
test.Assert(t, tcase.desc, string(tcase.expBody),
string(gotBody))
}
// On next test, we create markup file for three.html.
// The output from HTML should changes.
t.Run(`On markup created for HTML`, func(t *testing.T) {
var name = `three.adoc`
var file = filepath.Join(dirRoot, name)
err = os.WriteFile(file, tdata.Input[name], 0600)
if err != nil {
t.Fatal(err)
}
var req = httptest.SimulateRequest{
Method: http.MethodGet,
Path: `/three.html`,
}
var result *httptest.SimulateResult
result, err = httptest.Simulate(cigo.HTTPServer.ServeHTTP, &req)
if err != nil {
t.Fatal(err)
}
var expBody = tdata.Output[`new_three.html`]
var gotBody = redactLastUpdated.ReplaceAll(
result.ResponseBody, []byte("[REDACTED]"))
test.Assert(t, `new_three.html`, string(expBody), string(gotBody))
})
t.Run(`On markup updated`, func(t *testing.T) {
var file = filepath.Join(dirRoot, `one.adoc`)
err = os.WriteFile(file, tdata.Input[`update_one.adoc`], 0600)
if err != nil {
t.Fatal(err)
}
var req = httptest.SimulateRequest{
Method: http.MethodGet,
Path: `/one.html`,
}
var result *httptest.SimulateResult
result, err = httptest.Simulate(cigo.HTTPServer.ServeHTTP, &req)
if err != nil {
t.Fatal(err)
}
var expBody = tdata.Output[`update_one.html`]
var gotBody = redactLastUpdated.ReplaceAll(
result.ResponseBody, []byte("[REDACTED]"))
test.Assert(t, `body`, string(expBody), string(gotBody))
})
t.Run(`On new directory with markup`, func(t *testing.T) {
var dirJournal = filepath.Join(dirRoot, `journal`)
err = os.MkdirAll(dirJournal, 0755)
if err != nil {
t.Fatal(err)
}
var journalIndex = filepath.Join(dirJournal, `index.adoc`)
err = os.WriteFile(journalIndex,
tdata.Input[`/journal/index.adoc`], 0600)
if err != nil {
t.Fatal(err)
}
var req = httptest.SimulateRequest{
Method: http.MethodGet,
Path: `/journal/`,
}
var result *httptest.SimulateResult
result, err = httptest.Simulate(cigo.HTTPServer.ServeHTTP, &req)
if err != nil {
t.Fatal(err)
}
var expBody = tdata.Output[`/journal/index.html`]
var gotBody = redactLastUpdated.ReplaceAll(
result.ResponseBody, []byte("[REDACTED]"))
test.Assert(t, `body`, string(expBody), string(gotBody))
})
t.Run(`On new directory request without slash`, func(t *testing.T) {
var dirJournal2 = filepath.Join(dirRoot, `journal2`)
err = os.MkdirAll(dirJournal2, 0755)
if err != nil {
t.Fatal(err)
}
var journalIndexAdoc = filepath.Join(dirJournal2, `index.adoc`)
err = os.WriteFile(journalIndexAdoc,
tdata.Input[`/journal2/index.adoc`], 0600)
if err != nil {
t.Fatal(err)
}
var req = httptest.SimulateRequest{
Method: http.MethodGet,
Path: `/journal2`,
}
var result *httptest.SimulateResult
result, err = httptest.Simulate(cigo.HTTPServer.ServeHTTP, &req)
if err != nil {
t.Fatal(err)
}
var expBody = tdata.Output[`/journal2/index.html`]
var gotBody = redactLastUpdated.ReplaceAll(
result.ResponseBody, []byte("[REDACTED]"))
test.Assert(t, `body`, string(expBody), string(gotBody))
})
}