This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
uuid_test.go
81 lines (72 loc) 路 2.25 KB
/
uuid_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
package middleware_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/kamilsk/form-api/pkg/domain"
"github.com/kamilsk/form-api/pkg/server/middleware"
"github.com/stretchr/testify/assert"
)
const UUID domain.ID = "41ca5e09-3ce2-4094-b108-3ecc257c6fa4"
func TestSchema(t *testing.T) {
tests := []struct {
name string
uuid domain.ID
next func(uuid domain.ID) (*domain.ID, http.Handler)
code int
}{
{"invalid uuid", "abc-def-ghi", func(uuid domain.ID) (*domain.ID, http.Handler) {
return &uuid, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
})
}, http.StatusBadRequest},
{"valid uuid", UUID, func(_ domain.ID) (*domain.ID, http.Handler) {
uuid := new(domain.ID)
return uuid, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
*uuid = req.Context().Value(middleware.SchemaKey{}).(domain.ID)
})
}, http.StatusOK},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
rw, req := httptest.NewRecorder(), &http.Request{}
uuid, next := tc.next(tc.uuid)
middleware.Schema(tc.uuid.String(), rw, req, next)
assert.Equal(t, tc.code, rw.Code)
assert.Equal(t, tc.uuid, *uuid)
})
}
}
func TestTemplate(t *testing.T) {
tests := []struct {
name string
uuid domain.ID
next func(uuid domain.ID) (*domain.ID, http.Handler)
code int
}{
{"invalid uuid", "abc-def-ghi", func(uuid domain.ID) (*domain.ID, http.Handler) {
return &uuid, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
})
}, http.StatusBadRequest},
{"valid uuid", UUID, func(_ domain.ID) (*domain.ID, http.Handler) {
uuid := new(domain.ID)
return uuid, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
*uuid = req.Context().Value(middleware.TemplateKey{}).(domain.ID)
})
}, http.StatusOK},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
rw, req := httptest.NewRecorder(), &http.Request{}
uuid, next := tc.next(tc.uuid)
middleware.Template(tc.uuid.String(), rw, req, next)
assert.Equal(t, tc.code, rw.Code)
assert.Equal(t, tc.uuid, *uuid)
})
}
}