-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_test.go
105 lines (90 loc) · 2.38 KB
/
context_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
97
98
99
100
101
102
103
104
105
package webfmwk
import (
"context"
"log/slog"
"net/http"
"testing"
"github.com/burgesQ/gommon/webtest"
"github.com/stretchr/testify/assert"
)
var (
hBody = `{"message":"nul"}`
jsonEncode = "application/json; charset=UTF-8"
)
func TestParam(t *testing.T) {
wrapperGet(t, "/test/{id}", "/test/tutu", func(c Context) error {
id := c.GetVar("id")
if id != "tutu" {
t.Errorf("error fetching the url param : [%s] expected [tutu]", id)
}
return c.JSONOk(id)
}, func(t *testing.T, resp *http.Response) {
t.Helper()
webtest.Body(t, `"tutu"`, resp)
webtest.StatusCode(t, http.StatusOK, resp)
})
}
func TestLogger(t *testing.T) {
var (
c = icontext{}
logger = slog.Default()
)
c.SetStructuredLogger(logger)
assert.True(t, logger == c.GetStructuredLogger(), "context logger should be the setted one")
// assert.True(t, logger == GetLogger(), "default logger should be the setted one")
}
func TestContext(t *testing.T) {
var (
ctx context.Context
c = icontext{
ctx: ctx,
}
)
assert.True(t, ctx == c.GetContext())
}
func TestFetchContent(t *testing.T) {
const (
_ok = iota
_unprocessable
)
tests := map[string]struct {
payload []byte
t int
}{
"fetch content": {[]byte(`{"first_name": "tutu"}`), _ok},
"fetch content unprocessable": {[]byte(`{"first_name": tutu"}`), _unprocessable},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
wrapperPost(t, "/test", "/test", test.payload, func(c Context) error {
anonymous := struct {
FirstName string `json:"first_name,omitempty" validate:"required"`
}{}
if e := c.FetchContent(&anonymous); e != nil {
return e
} else if e := c.Validate(anonymous); e != nil {
return e
}
return c.JSON(http.StatusCreated, anonymous)
}, func(t *testing.T, resp *http.Response) {
t.Helper()
switch test.t {
case _ok:
webtest.Body(t, `{"first_name":"tutu"}`, resp)
webtest.StatusCode(t, http.StatusCreated, resp)
case _unprocessable:
webtest.StatusCode(t, http.StatusUnprocessableEntity, resp)
}
})
})
}
}
func TestJSONBlobPretty(t *testing.T) {
wrapperGet(t, "/test", "/test?pretty", func(c Context) error {
return c.JSONBlob(http.StatusOK, []byte(hBody))
}, func(t *testing.T, resp *http.Response) {
t.Helper()
webtest.BodyDiffere(t, hBody, resp)
webtest.StatusCode(t, http.StatusOK, resp)
})
}