-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_websocket_test.go
More file actions
37 lines (31 loc) · 1.13 KB
/
context_websocket_test.go
File metadata and controls
37 lines (31 loc) · 1.13 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
package gin_test
import (
"net/http"
"net/http/httptest"
"testing"
engine "github.com/darkit/gin"
"github.com/darkit/gin/pkg/websocket"
"github.com/gin-gonic/gin"
)
func TestUpgradeWebSocket_DefaultOrigin(t *testing.T) {
ginCtx, _ := gin.CreateTestContext(httptest.NewRecorder())
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/ws", nil)
ctx := &engine.Context{Context: ginCtx}
ctx.SetEngine(engine.New())
if _, err := ctx.UpgradeWebSocket("", nil); err == nil {
t.Fatalf("expected upgrade to fail in test context without websocket handshake")
}
}
func TestUpgradeWebSocket_CheckOriginAllowed(t *testing.T) {
ginCtx, _ := gin.CreateTestContext(httptest.NewRecorder())
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/ws", nil)
ginCtx.Request.Header.Set("Origin", "https://example.com")
ctx := &engine.Context{Context: ginCtx}
ctx.SetEngine(engine.New())
_, err := ctx.UpgradeWebSocket("", websocket.WithWSCheckOrigin(func(r *http.Request) bool {
return r.Header.Get("Origin") == "https://example.com"
}))
if err == nil {
t.Fatalf("expected upgrade to fail in test context without websocket handshake")
}
}