/
requests.go
126 lines (93 loc) · 3.39 KB
/
requests.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Kodex (Community Edition - CE) - Privacy & Security Engineering Platform
// Copyright (C) 2019-2022 KIProtect GmbH (HRB 208395B) - Germany
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package testing
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/kiprotect/kodex/api"
"net/http"
"net/http/httptest"
)
func Request(controller api.Controller, user *api.ExternalUser, reader *bytes.Reader, method, path string) (*gin.Engine, *http.Request, error) {
withUser := func(c *gin.Context) {
c.Set("user", user)
}
router, err := Router(controller, withUser)
if err != nil {
return nil, nil, err
}
req, err := http.NewRequest(method, path, reader)
if err != nil {
return nil, nil, err
}
return router, req, nil
}
func Serve(request *http.Request, router *gin.Engine) (*httptest.ResponseRecorder, error) {
resp := httptest.NewRecorder()
router.ServeHTTP(resp, request)
return resp, nil
}
func PostPut(controller api.Controller, user *api.ExternalUser, method, path string, data interface{}) (*httptest.ResponseRecorder, error) {
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
}
reader := bytes.NewReader(jsonData)
router, req, err := Request(controller, user, reader, method, path)
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/json")
return Serve(req, router)
}
func GetDelete(controller api.Controller, user *api.ExternalUser, method, path string, params interface{}) (*httptest.ResponseRecorder, error) {
reader := bytes.NewReader(nil)
router, req, err := Request(controller, user, reader, method, path)
if err != nil {
return nil, err
}
if params != nil {
data, ok := params.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid query data")
}
q := req.URL.Query()
for k, v := range data {
strV, ok := v.(string)
if !ok {
return nil, fmt.Errorf("not a string")
}
q.Add(k, strV)
}
// we assign the URL back to the raw query
req.URL.RawQuery = q.Encode()
}
return Serve(req, router)
}
func Get(controller api.Controller, user *api.ExternalUser, path string, params interface{}) (*httptest.ResponseRecorder, error) {
return GetDelete(controller, user, "GET", path, params)
}
func Del(controller api.Controller, user *api.ExternalUser, path string, params interface{}) (*httptest.ResponseRecorder, error) {
return GetDelete(controller, user, "DELETE", path, params)
}
func Post(controller api.Controller, user *api.ExternalUser, path string, data interface{}) (*httptest.ResponseRecorder, error) {
return PostPut(controller, user, "POST", path, data)
}
func Put(controller api.Controller, user *api.ExternalUser, path string, data interface{}) (*httptest.ResponseRecorder, error) {
return PostPut(controller, user, "PATCH", path, data)
}