-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter_test.go
More file actions
116 lines (87 loc) · 2.92 KB
/
router_test.go
File metadata and controls
116 lines (87 loc) · 2.92 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
package api_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/bjaus/api"
)
func TestRouter_ServeHTTP_basic(t *testing.T) {
t.Parallel()
type Resp struct {
Message string `json:"message"`
}
r := api.New()
api.Get(r, "/health", func(_ context.Context, _ *api.Void) (*api.Resp[Resp], error) {
return &api.Resp[Resp]{Body: Resp{Message: "ok"}}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/health", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Contains(t, string(body), `"message":"ok"`)
}
func TestRouter_Use_middleware(t *testing.T) {
t.Parallel()
type Resp struct {
Value string `json:"value"`
}
r := api.New()
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("X-Custom", "applied")
next.ServeHTTP(w, req)
})
})
api.Get(r, "/test", func(_ context.Context, _ *api.Void) (*api.Resp[Resp], error) {
return &api.Resp[Resp]{Body: Resp{Value: "hello"}}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/test", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, "applied", resp.Header.Get("X-Custom"))
}
func TestRouter_options(t *testing.T) {
t.Parallel()
r := api.New(
api.WithTitle("Test API"),
api.WithVersion("1.0.0"),
)
spec := r.Spec()
assert.Equal(t, "Test API", spec.Info.Title)
assert.Equal(t, "1.0.0", spec.Info.Version)
}
func TestRouter_error_response(t *testing.T) {
t.Parallel()
r := api.New(api.WithError(api.WithErrorBody(api.ErrorBodyProblemDetails)))
api.Get(r, "/fail", func(_ context.Context, _ *api.Void) (*api.Void, error) {
return nil, api.Error(api.CodeUnprocessableContent, api.WithMessage("bad data"))
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/fail", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
assert.Equal(t, "application/problem+json", resp.Header.Get("Content-Type"))
var env api.ProblemDetails
require.NoError(t, json.NewDecoder(resp.Body).Decode(&env))
assert.Equal(t, api.CodeUnprocessableContent, env.Code)
assert.Equal(t, "bad data", env.Detail)
}