-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux_test.go
42 lines (37 loc) · 881 Bytes
/
mux_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
package main
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/manaty226/go_todo_app/config"
)
func TestNewMux(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/health", nil)
ctx := context.Background()
cfg, err := config.New()
if err != nil {
t.Fatal("failed to initialize config: %w", err)
}
sut, cleanup, err := NewMux(ctx, cfg)
if err != nil {
cleanup()
t.Fatal("tailed to initialize mux: %w", err)
}
sut.ServeHTTP(w, r)
resp := w.Result()
t.Cleanup(func() { _ = resp.Body.Close() })
if resp.StatusCode != http.StatusOK {
t.Error("want status code 200, but", resp.StatusCode)
}
got, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
want := `{"status": "ok"}`
if string(got) != want {
t.Errorf("want %q, but got %q", want, got)
}
}