-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
96 lines (71 loc) · 1.62 KB
/
main_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
// Copyright © Paul Tötterman <[email protected]>. All rights reserved.
package main
import (
"flag"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func checkErr(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Fatal(err)
}
}
func TestMain(m *testing.M) {
flag.Parse()
readConfigFile("config.json", &conf)
if !testing.Short() {
var err error
pool, err = newPostgresDB()
if err != nil {
panic(err)
}
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr,
&slog.HandlerOptions{ //nolint:exhaustruct
AddSource: true,
Level: slog.LevelDebug,
})))
code := m.Run()
os.Exit(code)
}
func TestConfig(t *testing.T) {
t.Parallel()
conf := &config{} //nolint:exhaustruct
readConfig(strings.NewReader("{}"), conf)
js := conf.String()
if js != `{"Listen":"","DB":"","Debug":false,"RealIPHeader":"","RemoteUserHeader":""}` {
t.Error("Config: ", js)
}
}
func TestConfigFromFile(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
conf := &config{} //nolint:exhaustruct
readConfigFile("config.json.sample", conf)
}
func TestSetupServeMux(t *testing.T) {
t.Parallel()
db, err := newPostgresDB()
checkErr(t, err)
mux, ok := setupServeMux(db).(*http.ServeMux)
if !ok {
t.Fatal("failed type assertion")
}
_, pattern := mux.Handler(httptest.NewRequest(http.MethodGet, "/foo",
nil))
if pattern != "GET /{name}" {
t.Error("Wrong pattern:", pattern)
}
_, pattern = mux.Handler(httptest.NewRequest(http.MethodGet, "/_admin",
nil))
if pattern != "GET /_admin" {
t.Error("Wrong pattern:", pattern)
}
}