-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
98 lines (75 loc) · 2.55 KB
/
errors_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
package ws_test
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
wstools "github.com/XDoubleU/essentia/pkg/communication/ws"
"github.com/XDoubleU/essentia/pkg/config"
errortools "github.com/XDoubleU/essentia/pkg/errors"
"github.com/XDoubleU/essentia/pkg/logging"
sentrytools "github.com/XDoubleU/essentia/pkg/sentry"
"github.com/XDoubleU/essentia/pkg/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testErrorStatusCode(t *testing.T, handler http.HandlerFunc) int {
t.Helper()
req, _ := http.NewRequest(http.MethodGet, "", nil)
res := httptest.NewRecorder()
sentryMiddleware, err := sentrytools.Middleware(
config.TestEnv,
sentrytools.MockedSentryClientOptions(),
)
require.Nil(t, err)
sentryMiddleware(handler).ServeHTTP(res, req)
return res.Result().StatusCode
}
func setupWS(t *testing.T, allowedOrigin string) http.Handler {
t.Helper()
logger := logging.NewNopLogger()
wsHandler := wstools.CreateWebSocketHandler[TestSubscribeMsg](
logger,
1,
10,
)
_, err := wsHandler.AddTopic("topic", []string{allowedOrigin}, nil)
require.Nil(t, err)
sentryMiddleware, err := sentrytools.Middleware(
config.TestEnv,
sentrytools.MockedSentryClientOptions(),
)
require.Nil(t, err)
return sentryMiddleware(wsHandler.Handler())
}
func TestUpgradeErrorResponse(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
wstools.UpgradeErrorResponse(w, r, errors.New("test"))
}
statusCode := testErrorStatusCode(t, handler)
assert.Equal(t, http.StatusInternalServerError, statusCode)
}
func TestErrorResponse(t *testing.T) {
handler := setupWS(t, "http://localhost")
tWeb := test.CreateWebSocketTester(handler)
tWeb.SetInitialMessage(TestSubscribeMsg{TopicName: "unknown"})
var errorDto errortools.ErrorDto
err := tWeb.Do(t, &errorDto, nil)
require.Nil(t, err)
assert.Equal(t, http.StatusBadRequest, errorDto.Status)
assert.Equal(t, http.StatusText(http.StatusBadRequest), errorDto.Error)
assert.Equal(t, "topic 'unknown' doesn't exist", errorDto.Message)
}
func TestFailedValidationResponse(t *testing.T) {
handler := setupWS(t, "http://localhost")
tWeb := test.CreateWebSocketTester(handler)
tWeb.SetInitialMessage(TestSubscribeMsg{TopicName: ""})
var errorDto errortools.ErrorDto
err := tWeb.Do(t, &errorDto, nil)
require.Nil(t, err)
assert.Equal(t, http.StatusUnprocessableEntity, errorDto.Status)
assert.Equal(t, http.StatusText(http.StatusUnprocessableEntity), errorDto.Error)
assert.Equal(t, map[string]any{
"topicName": "must be provided",
}, errorDto.Message)
}