-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
77 lines (63 loc) · 1.58 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
package main
import (
"net/http/httptest"
"testing"
)
func TestGetPort(t *testing.T) {
t.Run("OK", func(t *testing.T) {
getenv := func(string) string { return "1234" }
if got, want := getPort(getenv), "1234"; got != want {
t.Fatalf("getPort(getenv) = %q, want %q", got, want)
}
})
t.Run("Default", func(t *testing.T) {
getenv := func(string) string { return "" }
if got, want := getPort(getenv), defaultPort; got != want {
t.Fatalf("getPort(getenv) = %q, want %q", got, want)
}
})
}
func TestParseApp(t *testing.T) {
app, err := parseApp()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := len(app.recipes), 6; got < want {
t.Fatalf("len(app.recipes) = %d, want >=%d", got, want)
}
t.Run("Det fantastiska brödet", func(t *testing.T) {
var dfb Recipe
for _, r := range app.recipes {
if r.Path == "Det-fantastiska-brödet.md" {
dfb = r
}
}
titel, ok := dfb.Meta["Titel"].(string)
if !ok {
t.Fatalf("expected Titel string")
}
if got, want := titel, "Det fantastiska brödet"; got != want {
t.Fatalf("titel = %q, want %q", got, want)
}
})
t.Run("HTTP", func(t *testing.T) {
for _, tt := range []struct {
path string
code int
}{
{"/", 200},
{"/favicon.ico", 200},
{"/Det-fantastiska-brödet.md", 200},
{"/not-found", 404},
} {
t.Run(tt.path, func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", tt.path, nil)
app.ServeHTTP(w, r)
if got, want := w.Code, tt.code; got != want {
t.Fatalf("w.Code = %d, want %d", got, want)
}
})
}
})
}