forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
studio_test.go
229 lines (186 loc) · 5.53 KB
/
studio_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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package studio
import (
"bytes"
"fmt"
"io"
"net/http"
"testing"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/helper"
"github.com/yaoapp/yao/runtime"
)
type kv map[string]interface{}
type arr []interface{}
func TestLoad(t *testing.T) {
runtime.Load(config.Conf)
err := Load(config.Conf)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, dfs)
res, err := gou.Yao.Engine.RootCall(map[string]interface{}{}, "table", "Ping")
assert.Nil(t, err)
assert.Equal(t, "PONG", res)
_, err = gou.Yao.Engine.Call(map[string]interface{}{}, "table", "Ping")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "The table does not loaded")
}
func TestStartStop(t *testing.T) {
var err error
go func() { err = Start(config.Conf) }()
if err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
Stop()
time.Sleep(100 * time.Millisecond)
}
func TestStartStopError(t *testing.T) {
var err error
go func() { err = Start(config.Conf) }()
if err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
go func() { err = Start(config.Conf) }()
time.Sleep(100 * time.Millisecond)
assert.NotNil(t, err)
Stop()
time.Sleep(100 * time.Millisecond)
}
func TestAPI(t *testing.T) {
Load(config.Conf)
var err error
go func() { err = Start(config.Conf) }()
if err != nil {
t.Fatal(err)
}
defer Stop()
time.Sleep(500 * time.Millisecond)
code, row := httpGet[kv]("/dsl/ReadFile?name=/models/user.json", t)
assert.Equal(t, 200, code)
assert.Equal(t, "用户", row["name"])
code, rows := httpGet[arr]("/dsl/ReadDir?name=/models", t)
assert.Equal(t, 200, code)
assert.Equal(t, 11, len(rows))
code, rows = httpGet[arr]("/dsl/ReadDir?name=/models&recursive=1", t)
assert.Equal(t, 200, code)
assert.Equal(t, 12, len(rows))
code, length := httpPost[int]("/dsl/WriteFile?name=/models/foo.mod.json", []byte(`{"name":"foo"}`), t)
assert.Equal(t, 200, code)
assert.Equal(t, 19, length)
code, _ = httpPost[kv]("/dsl/Remove?name=/models/foo.mod.json", nil, t)
assert.Equal(t, 200, code)
code, _ = httpPost[kv]("/dsl/Mkdir?name=/models/bar", nil, t)
assert.Equal(t, 200, code)
code, _ = httpPost[kv]("/dsl/Remove?name=/models/bar", nil, t)
assert.Equal(t, 200, code)
code, _ = httpPost[kv]("/dsl/MkdirAll?name=/models/bar/hi", nil, t)
assert.Equal(t, 200, code)
code, _ = httpPost[kv]("/dsl/RemoveAll?name=/models/bar", nil, t)
assert.Equal(t, 200, code)
code, res := httpPostJSON[arr](
"/service/table",
kv{
"method": "UnitTest",
"args": []interface{}{
"foo", 1, 0.618,
kv{"string": "world", "int": 1, "float": 0.618},
arr{"foo", 1, 0.618},
},
}, t)
assert.Equal(t, 200, code)
assert.Equal(t, "foo", res[0])
assert.Equal(t, float64(1), res[1])
assert.Equal(t, 0.618, res[2])
assert.Equal(t, "world", res[3].(map[string]interface{})["string"])
assert.Equal(t, float64(1), res[3].(map[string]interface{})["int"])
assert.Equal(t, 0.618, res[3].(map[string]interface{})["float"])
assert.Equal(t, "foo", res[4].([]interface{})[0])
assert.Equal(t, float64(1), res[4].([]interface{})[1])
assert.Equal(t, 0.618, res[4].([]interface{})[2])
code, excp := httpPostJSON[kv]("/service/table", kv{"method": "UnitTest", "args": []interface{}{"throw-test"}}, t)
assert.Equal(t, 418, code)
assert.Equal(t, float64(418), excp["code"])
assert.Equal(t, "I'm a teapot", excp["message"])
}
func httpGet[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, t *testing.T) (int, T) {
var data T
url = fmt.Sprintf("http://127.0.0.1:%d%s", config.Conf.Studio.Port, url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
token := getToken(t)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
client := http.Client{}
res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
if res.Body != nil {
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if body != nil && len(body) > 0 {
err = jsoniter.Unmarshal(body, &data)
if err != nil {
t.Fatal(fmt.Sprintf("%s\n%s\n", err.Error(), string(body)))
}
}
}
return res.StatusCode, data
}
func httpPost[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, payload []byte, t *testing.T) (int, T) {
var data T
var buff *bytes.Buffer = bytes.NewBuffer([]byte{})
if payload != nil {
buff = bytes.NewBuffer(payload)
}
url = fmt.Sprintf("http://127.0.0.1:%d%s", config.Conf.Studio.Port, url)
req, err := http.NewRequest("POST", url, buff)
if err != nil {
t.Fatal(err)
}
token := getToken(t)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
client := http.Client{}
res, err := client.Do(req)
if res.Body != nil {
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if body != nil && string(body) != "" {
err = jsoniter.Unmarshal(body, &data)
if err != nil {
t.Fatal(err)
}
}
}
return res.StatusCode, data
}
func httpPostJSON[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, payload interface{}, t *testing.T) (int, T) {
var data []byte
var err error
if payload != nil {
data, err = jsoniter.Marshal(payload)
if err != nil {
t.Fatal(err)
}
}
return httpPost[T](url, data, t)
}
func getToken(t *testing.T) string {
return helper.JwtMake(
1,
map[string]interface{}{"id": 1, "user_id": 1, "user": kv{"id": 1, "name": "test"}},
map[string]interface{}{"issuer": "unit-test", "timeout": 3600},
config.Conf.Studio.Secret,
).Token
}