Newer
Older
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
127
128
129
130
131
132
133
134
135
136
package routebuildermdl
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"github.com/stretchr/testify/assert"
"github.com/gin-gonic/gin"
)
var router *gin.Engine
func DummyService(data []byte) (interface{}, error) {
return "success", nil
}
func RDummyService(data []byte) (interface{}, error) {
return "success", nil
}
func RBDummyService(data []byte) (interface{}, error) {
return "success", nil
}
func MultiparService(form *multipart.Form) (interface{}, error) {
return "success", nil
}
func init() {
router = gin.New()
o := router.Group("/o")
r := router.Group("/r")
c := r.Group("/c")
Init(o, r, c)
RegisterNormalService("DummyService", DummyService, false, false)
RegisterNormalService("RDummyService", RDummyService, true, false)
RegisterNormalService("RBDummyService", RBDummyService, true, true)
RegisterFormBasedService("MultiparService", MultiparService, true, true)
}
func Test_OpenHandler(t *testing.T) {
login := `
{
"loginId": "demo",
"password": "demo"
}
`
req, _ := http.NewRequest("POST", "/o/mql", bytes.NewBuffer([]byte(login)))
req.Header.Set("Service-Header", "DummyService")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
}
func Test_RestrictedHandler(t *testing.T) {
login := `
{
"loginId": "demo",
"password": "demo"
}
`
req, _ := http.NewRequest("POST", "/r/mql", bytes.NewBuffer([]byte(login)))
req.Header.Set("Service-Header", "RDummyService")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
}
func Test_RoleBasedHandler(t *testing.T) {
login := `
{
"loginId": "demo",
"password": "demo"
}
`
req, _ := http.NewRequest("POST", "/r/c/mql", bytes.NewBuffer([]byte(login)))
req.Header.Set("Service-Header", "RBDummyService")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
}
func Test1_RestrictedHandler(t *testing.T) {
login := `
{
"loginId": "demo",
"password": "demo"
}
`
req, _ := http.NewRequest("POST", "/r/c/mql", bytes.NewBuffer([]byte(login)))
req.Header.Set("Service-Header", "RDummyService")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 417)
}
func Test1_MultipartHandler(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", "tmp.json")
part.Write([]byte("tmp data"))
writer.Close()
req, _ := http.NewRequest("POST", "/r/c/mql", body)
req.Header.Set("Service-Header", "MultiparService")
req.Header.Add("Content-Type", writer.FormDataContentType())
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
}
func Test2_MultipartHandler(t *testing.T) {
errormdl.IsTestingNegetiveCaseOn = true
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", "tmp.json")
part.Write([]byte("tmp data"))
writer.Close()
req, _ := http.NewRequest("POST", "/r/c/mql", body)
req.Header.Set("Service-Header", "MultiparService")
req.Header.Add("Content-Type", writer.FormDataContentType())
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
errormdl.IsTestingNegetiveCaseOn = false
assert.Equal(t, resp.Code, 417)
}