-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_context_factory_test.go
82 lines (74 loc) · 1.78 KB
/
gin_context_factory_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
package contextfactory
import (
"net/http"
"strconv"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
var buildGinTestContextTestCases = []ContextOptions{
{ // All field except for the body.
Method: "GET",
Path: "/test",
Body: nil,
PathParams: gin.Params{
gin.Param{
Key: "param_test_key",
Value: "param_test_value",
},
},
QueryParams: map[string]string{
"query_test_key": "query_test_value",
},
Headers: map[string]string{
"Content-Type": "application/json",
},
ContextVars: map[string]interface{}{
"test": "test",
},
},
{ // All field with the body.
Method: "GET",
Path: "/test",
Body: strings.NewReader("test body data"),
PathParams: gin.Params{
gin.Param{
Key: "param_test_key",
Value: "param_test_value",
},
},
QueryParams: map[string]string{
"query_test_key": "query_test_value",
},
Headers: map[string]string{
"header_test_key": "header_test_value",
},
ContextVars: map[string]interface{}{
"test": "test",
},
},
}
func TestBuildGinTestContext(t *testing.T) {
for i, testCase := range buildGinTestContextTestCases {
t.Run("Case "+strconv.Itoa(i), func(t *testing.T) {
c, recorder := BuildGinTestContext(testCase)
assert.NotNil(t, recorder)
req := c.Request
assert.Equal(t, testCase.Method, req.Method)
assert.Equal(t, testCase.Path, req.URL.Path)
if testCase.Body == nil {
assert.Equal(t, http.NoBody, req.Body)
}
for key, value := range testCase.Headers {
assert.Equal(t, value, req.Header.Get(key))
}
for key, value := range testCase.QueryParams {
assert.Equal(t, value, req.URL.Query()[key][0])
}
for key, value := range testCase.ContextVars {
assert.Equal(t, value, c.GetString(key))
}
})
}
}