-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathapi_test.go
128 lines (101 loc) · 3.3 KB
/
api_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
package test
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
"exusiai.dev/backend-next/internal/app"
"exusiai.dev/backend-next/internal/app/appcontext"
)
// testing hooks: https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks
var (
gMu sync.Mutex
gFiberApp *fiber.App
)
func startup(t *testing.T) {
t.Helper()
gMu.Lock()
defer gMu.Unlock()
if gFiberApp != nil {
return
}
var fiberApp *fiber.App
fxApp := fxtest.New(t,
append(app.Options(appcontext.Declare(appcontext.EnvServer)), fx.Populate(&fiberApp))...,
)
fxApp.RequireStart()
gFiberApp = fiberApp
}
func request(t *testing.T, req *http.Request, msTimeout ...int) *http.Response {
t.Helper()
resp, err := gFiberApp.Test(req, msTimeout...)
if err != nil {
t.Fatal(err)
}
return resp
}
func JsonRequestCustom(t *testing.T, req *http.Request, msTimeout ...int) (*http.Response, *gjson.Result) {
t.Helper()
resp := request(t, req, msTimeout...)
bodyBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "failed to read response body")
body := gjson.ParseBytes(bodyBytes)
return resp, &body
}
func JsonRequest(t *testing.T, path, body string, headers *http.Header, msTimeout ...int) (*http.Response, *gjson.Result) {
t.Helper()
req := httptest.NewRequest(http.MethodPost, path, bytes.NewBufferString(body))
if headers != nil {
req.Header = *headers
}
req.Header.Set("Content-Type", "application/json")
return JsonRequestCustom(t, req, msTimeout...)
}
func TestAPIMeta(t *testing.T) {
startup(t)
t.Parallel()
t.Run("health", func(t *testing.T) {
resp := request(
t,
httptest.NewRequest(http.MethodGet, "/api/_/health", nil),
)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
t.Run("version", func(t *testing.T) {
resp := request(
t,
httptest.NewRequest(http.MethodGet, "/api/_/bininfo", nil),
)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
t.Run("CORS Anonymous Origin", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/PenguinStats/api/v2/config", nil)
req.Header.Set("Origin", "https://penguin-stats.io")
resp := request(
t,
req,
)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "https://penguin-stats.io", resp.Header.Get("Access-Control-Allow-Origin"))
assert.Equal(t, "true", resp.Header.Get("Access-Control-Allow-Credentials"))
})
t.Run("CORS Origin", func(t *testing.T) {
req := httptest.NewRequest(http.MethodOptions, "/PenguinStats/api/v2/config", nil)
req.Header.Set("Origin", "https://penguin-stats.io")
req.Header.Set("Access-Control-Request-Headers", "Content-Type,Authorization,X-Requested-With,X-Penguin-Variant,sentry-trace")
req.Header.Set("Access-Control-Request-Method", "GET")
req.Header.Set("Access-Control-Request-Credentials", "true")
resp := request(t, req)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
assert.Equal(t, "GET,POST,DELETE,OPTIONS", resp.Header.Get("Access-Control-Allow-Methods"))
assert.Equal(t, "https://penguin-stats.io", resp.Header.Get("Access-Control-Allow-Origin"))
assert.Equal(t, "Content-Type,Authorization,X-Requested-With,X-Penguin-Variant,sentry-trace", resp.Header.Get("Access-Control-Allow-Headers"))
})
}