-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_test.go
72 lines (66 loc) · 1.92 KB
/
api_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
package chimera_test
import (
"net/http"
"github.com/matt1484/chimera"
"github.com/stretchr/testify/assert"
)
func addRequestTestHandler[ReqPtr chimera.RequestReaderPtr[Req], Req any](t assert.TestingT, api *chimera.API, method, path string, expectedReq ReqPtr) *ReqPtr {
value := new(ReqPtr)
switch method {
case http.MethodGet:
chimera.Get(api, path, func(req ReqPtr) (*chimera.EmptyResponse, error) {
*value = req
return nil, nil
})
case http.MethodPut:
chimera.Put(api, path, func(req ReqPtr) (*chimera.EmptyResponse, error) {
*value = req
return nil, nil
})
case http.MethodPost:
chimera.Post(api, path, func(req ReqPtr) (*chimera.EmptyResponse, error) {
*value = req
return nil, nil
})
case http.MethodDelete:
chimera.Delete(api, path, func(req ReqPtr) (*chimera.EmptyResponse, error) {
*value = req
return nil, nil
})
case http.MethodPatch:
chimera.Patch(api, path, func(req ReqPtr) (*chimera.EmptyResponse, error) {
*value = req
return nil, nil
})
}
return value
}
func addResponseTestHandler[RespPtr chimera.ResponseWriterPtr[Resp], Resp any](t assert.TestingT, api *chimera.API, method, path string, resp RespPtr) {
switch method {
case http.MethodGet:
chimera.Get(api, path, func(*chimera.EmptyRequest) (RespPtr, error) {
return resp, nil
})
case http.MethodPut:
chimera.Put(api, path, func(*chimera.EmptyRequest) (RespPtr, error) {
return resp, nil
})
case http.MethodPost:
chimera.Post(api, path, func(*chimera.EmptyRequest) (RespPtr, error) {
return resp, nil
})
case http.MethodDelete:
chimera.Delete(api, path, func(*chimera.EmptyRequest) (RespPtr, error) {
return resp, nil
})
case http.MethodPatch:
chimera.Patch(api, path, func(*chimera.EmptyRequest) (RespPtr, error) {
return resp, nil
})
}
}
type TestValidCustomParam string
type TestStructParams struct {
StringProp string `prop:"stringprop"`
IntProp int `prop:"intprop"`
}