-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
72 lines (68 loc) · 1.45 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
package main
import (
"io"
"net/http/httptest"
"testing"
)
func TestPing(t *testing.T) {
req := httptest.NewRequest("GET", "/ping", nil)
w := httptest.NewRecorder()
ping(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if string(data) != "pong" {
t.Fatalf("expected pong, got %s", string(data))
}
}
func TestGetLivePageIndex(t *testing.T) {
srcDir := "src_test"
defer cleanup("dist")
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
app, err := InitApp(srcDir)
if err != nil {
t.Fatal(err)
}
Build(srcDir)
app.getLivePage(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Fatalf("expected 200, got %d", res.StatusCode)
}
if len(string(data)) == 0 {
t.Fatalf("expected index, got %s", string(data))
}
}
func TestGetLivePage(t *testing.T) {
srcDir := "src_test"
defer cleanup("dist")
req := httptest.NewRequest("GET", "/pages/example", nil)
w := httptest.NewRecorder()
app, err := InitApp(srcDir)
if err != nil {
t.Fatal(err)
}
Build(srcDir)
app.getLivePage(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Fatalf("expected 200, got %d", res.StatusCode)
}
if len(string(data)) == 0 {
t.Fatalf("expected about, got %s", string(data))
}
}