-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
135 lines (122 loc) · 3.47 KB
/
server_test.go
File metadata and controls
135 lines (122 loc) · 3.47 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
package main
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
// initServer inits a server with default content in the repo, such as
// the material at assets/static (including styles) and pages (including
// images from assets/images and templates from assets/templates).
func initServer(t *testing.T) *server {
t.Helper()
cfg := &config{
AssetsDir: "assets",
PageTemplate: "templates/page.html",
IndexTemplate: "templates/index.html",
Pages: []page{
page{
URL: "/home",
Title: "Home",
ImagePath: "images/home.jpg",
// Note: "",
Zones: []pageZone{pageZone{367, 44, 539, 263, "/detail", ""}},
},
page{
URL: "/detail",
Title: "Detail",
ImagePath: "images/detail.jpg",
Note: "",
Zones: []pageZone{pageZone{436, 31, 538, 73, "/home", ""}},
},
},
}
if err := cfg.validateConfig(); err != nil {
t.Fatal(err)
}
s, err := newServer(
"127.0.0.1",
"8001",
cfg,
)
if err != nil {
t.Fatal(err)
}
return s
}
// TestServer tests a running server instance of the site using the
// configuration loaded from initServer. The configuration depends on
// the on-disk default content in the repo.
func TestServer(t *testing.T) {
s := initServer(t)
handler, err := s.buildHandler()
if err != nil {
t.Fatal("buildHander error:", err)
}
ts := httptest.NewServer(handler)
defer ts.Close()
testCases := []struct {
name string
path string
statusCode int
bodyContains string
}{
{"Health Check", "/health", http.StatusOK, `{"status":"up"}`},
{"Home Page", "/home", http.StatusOK, "<title>Home"},
{"Detail Page", "/detail", http.StatusOK, "<title>Detail"},
{"Favicon", "/favicon", http.StatusOK, "<svg xmlns="},
{"Favicon ico", "/favicon.ico", http.StatusOK, "<svg xmlns="},
{"Image File", "/images/home.jpg", http.StatusOK, "Photoshop 3.0"},
{"Index", "/index", http.StatusOK, "<h1>Index</h1>"},
{"Root", "/", http.StatusOK, "<h1>Index</h1>"},
{"Not Found", "/nonexistent", http.StatusNotFound, "404 page not found"},
{"Templates file 404", "/templates", http.StatusNotFound, "404 page not found"},
{"Templates dir 404", "/templates/", http.StatusNotFound, "The templates directory is purposely not mounted."},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
client := ts.Client()
url, err := url.JoinPath(ts.URL, tt.path)
if err != nil {
t.Fatalf("url joining error: %s, %s : %v", ts.URL, tt.path, err)
}
resp, err := client.Get(url)
if err != nil {
t.Fatalf("get error: %v", err)
}
defer func() {
_ = resp.Body.Close()
}()
if got, want := resp.StatusCode, tt.statusCode; got != want {
t.Fatalf("got %d want %d", got, want)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("could not read body: %v", err)
}
if !bytes.Contains(body, []byte(tt.bodyContains)) {
t.Errorf("body does not contain %q", tt.bodyContains)
}
})
}
}
// TestServerServe checks if the server is actually triggered (albeit
// once shutdown has already been registered).
func TestServerServe(t *testing.T) {
s := initServer(t)
ctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond*50)
defer cancel()
err := s.webServer.Shutdown(ctx)
if err != nil {
t.Fatal("cannot register shutdown:", err)
}
err = Serve(s)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
t.Fatalf("unexpected error %T %v", err, err)
}
}