-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi_test.go
More file actions
311 lines (289 loc) · 9.96 KB
/
api_test.go
File metadata and controls
311 lines (289 loc) · 9.96 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
var count = 0
type APITestSuite struct {
suite.Suite
baseURL string
serverAddr string
config string
app *App
}
func TestAPITestSuite(t *testing.T) {
configs := []string{
"./gosqlapi.json",
"./tests/mysql.json",
"./tests/mariadb.json",
"./tests/pgx.json",
"./tests/postgres.json",
// "./tests/oracle.json",
"./tests/sqlite.json",
"./tests/sqlserver.json",
// "./tests/sqlite3.json", // need to checkout sqlite3 branch
}
fmt.Println("Starting API tests version:", version)
for index, config := range configs {
testAll := os.Getenv("test_all")
if index > 0 && testAll == "" {
continue
}
port := rand.Intn(10000) + 40000
suite.Run(t, &APITestSuite{
baseURL: fmt.Sprintf("http://127.0.0.1:%v/", port),
serverAddr: fmt.Sprintf("127.0.0.1:%v", port),
config: config,
})
}
}
func (this *APITestSuite) SetupSuite() {
confBytes, err := os.ReadFile(this.config)
this.Nil(err)
this.app, err = NewApp(confBytes)
this.Nil(err)
this.app.Web.HttpAddr = this.serverAddr
go this.app.run()
time.Sleep(time.Second * 5)
}
func (this *APITestSuite) TearDownSuite() {
if this.app != nil {
this.app.shutdown()
}
}
func (this *APITestSuite) TestAPI() {
this.testAPI(false)
this.testAPI(true)
}
func (this *APITestSuite) testAPI(usePatch bool) {
count++
fmt.Println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", count)
scriptMethod := "PATCH"
if !usePatch {
scriptMethod = "GET"
}
fmt.Println("Testing API with config:", this.config, "with usePatch:", usePatch)
// patch init
req, err := http.NewRequest(scriptMethod, this.baseURL+"test_db/init/", bytes.NewBuffer([]byte(`{"low": 0,"high": 3}`)))
this.Nil(err)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal("123", resp.Header.Get("abc"))
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
this.Nil(err)
var respBody map[string]any
err = json.Unmarshal(body, &respBody)
this.Nil(err)
this.Assert().Equal(2, len(respBody["data"].([]any)))
this.Assert().Equal(1, int(respBody["data"].([]any)[0].(map[string]any)["id"].(float64)))
this.Assert().Equal("Alpha", respBody["data"].([]any)[0].(map[string]any)["name"].(string))
this.Assert().Equal(2, int(respBody["data"].([]any)[1].(map[string]any)["id"].(float64)))
this.Assert().Equal("Beta", respBody["data"].([]any)[1].(map[string]any)["name"].(string))
// get
resp, err = http.Get(this.baseURL + "test_db/test_table/1")
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBody2 map[string]any
err = json.Unmarshal(body, &respBody2)
this.Nil(err)
this.Assert().Equal(1, int(respBody2["id"].(float64)))
this.Assert().Equal("Alpha", respBody2["name"].(string))
// post
req, err = http.NewRequest("POST", this.baseURL+"test_db/test_table/", bytes.NewBuffer([]byte(`{"id": 4,"name": "Gamma"}`)))
this.Nil(err)
req.Header.Set("Content-Type", "application/json")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBody3 map[string]any
err = json.Unmarshal(body, &respBody3)
this.Nil(err)
this.Assert().Equal(1, int(respBody3["rows_affected"].(float64)))
// insert null
req, err = http.NewRequest("POST", this.baseURL+"test_db/test_table/?id=5&name=NULL", nil)
this.Nil(err)
req.Header.Set("Content-Type", "application/json")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBodyInsertNull map[string]any
err = json.Unmarshal(body, &respBodyInsertNull)
this.Nil(err)
this.Assert().Equal(1, int(respBodyInsertNull["rows_affected"].(float64)))
// test null
resp, err = http.Get(this.baseURL + "test_db/test_table/5")
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBodyTestNull map[string]any
err = json.Unmarshal(body, &respBodyTestNull)
this.Nil(err)
this.Assert().Equal(5, int(respBodyTestNull["id"].(float64)))
this.Assert().Equal(nil, respBodyTestNull["name"])
// delete null
req, err = http.NewRequest("DELETE", this.baseURL+"test_db/test_table/5", nil)
this.Nil(err)
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBodyDeleteNull map[string]any
err = json.Unmarshal(body, &respBodyDeleteNull)
this.Nil(err)
this.Assert().Equal(1, int(respBodyDeleteNull["rows_affected"].(float64)))
// put
req, err = http.NewRequest("PUT", this.baseURL+"test_db/test_table/4", bytes.NewBuffer([]byte(`{"name": "Omega"}`)))
this.Nil(err)
req.Header.Set("Content-Type", "application/json")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBody4 map[string]any
err = json.Unmarshal(body, &respBody4)
this.Nil(err)
this.Assert().Equal(1, int(respBody4["rows_affected"].(float64)))
// delete
req, err = http.NewRequest("DELETE", this.baseURL+"test_db/test_table/4", nil)
this.Nil(err)
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBody5 map[string]any
err = json.Unmarshal(body, &respBody5)
this.Nil(err)
this.Assert().Equal(1, int(respBody5["rows_affected"].(float64)))
// get page
resp, err = http.Get(this.baseURL + "test_db/test_table/?.page_size=2&.offset=1&.show_total=1")
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBody6 map[string]any
err = json.Unmarshal(body, &respBody6)
this.Nil(err)
this.Assert().Equal(3, int(respBody6["total"].(float64)))
this.Assert().Equal(1, int(respBody6["offset"].(float64)))
this.Assert().Equal(2, int(respBody6["page_size"].(float64)))
this.Assert().Equal(2, len(respBody6["data"].([]any)))
this.Assert().Equal("Beta", respBody6["data"].([]any)[0].(map[string]any)["name"].(string))
this.Assert().Equal("Gamma", respBody6["data"].([]any)[1].(map[string]any)["name"].(string))
// get without auth token and get 401
resp, err = http.Get(this.baseURL + "test_db/token_table/")
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusUnauthorized, resp.StatusCode)
// get with bad auth token and get 401
req, err = http.NewRequest("GET", this.baseURL+"test_db/token_table/", nil)
this.Nil(err)
req.Header.Set("authorization", "bad_token")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusUnauthorized, resp.StatusCode)
// get with auth token and get 200
req, err = http.NewRequest("GET", this.baseURL+"test_db/token_table/", nil)
req.Header.Set("Origin", "http://localhost:8080")
this.Nil(err)
req.Header.Set("authorization", "1234567890")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
// get with auth token but no origin and get 401
req, err = http.NewRequest("GET", this.baseURL+"test_db/token_table/", nil)
this.Nil(err)
req.Header.Set("authorization", "1234567890")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusUnauthorized, resp.StatusCode)
// get with auth token with no origin and get 401
req, err = http.NewRequest("GET", this.baseURL+"test_db/token_table/", nil)
this.Nil(err)
req.Header.Set("authorization", "no_access")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusUnauthorized, resp.StatusCode)
// get with auth token with all origin access and get 200
req, err = http.NewRequest("GET", this.baseURL+"test_db/token_table/", nil)
this.Nil(err)
req.Header.Set("authorization", "super")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
// query metadata
req, err = http.NewRequest(scriptMethod, this.baseURL+"test_db/metadata/", nil)
req.Header.Set("Origin", "https://*.example.com")
this.Nil(err)
req.Header.Set("authorization", "Bearer 0987654321")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBody7 map[string]any
err = json.Unmarshal(body, &respBody7)
this.Nil(err)
this.Assert().Equal("Bearer 0987654321", respBody7["metadata"].([]any)[0].(map[string]any)["authorization"].(string))
// query metadata with wrong referer
req, err = http.NewRequest(scriptMethod, this.baseURL+"test_db/metadata/", nil)
req.Header.Set("Origin", "https://*.example.net")
this.Nil(err)
req.Header.Set("authorization", "Bearer 0987654321")
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusUnauthorized, resp.StatusCode)
// query tables
req, err = http.NewRequest(scriptMethod, this.baseURL+"test_db/list_tables/", nil)
this.Nil(err)
resp, err = client.Do(req)
this.Nil(err)
defer resp.Body.Close()
this.Assert().Equal(http.StatusOK, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
this.Nil(err)
var respBody8 []any
err = json.Unmarshal(body, &respBody8)
this.Nil(err)
this.Assert().Equal("TEST_GOSQLAPI", strings.ToUpper(respBody8[0].(map[string]any)["name"].(string)))
this.Assert().Equal("TEST_GOSQLAPI_TOKENS", strings.ToUpper(respBody8[1].(map[string]any)["name"].(string)))
count--
fmt.Println("-------------------------------------------------------------", count)
}