Files
atlantis/server/controllers/websocket/mux_test.go
PePe Amengual 7567b31ccd fix: resolve linter issues to enable golangci-lint update
- Fix all errcheck issues (unhandled errors for Close(), Fprintln, etc.)
- Fix gosec issues with appropriate nolint comments for test code
- Fix misspell issues and add nolint for GitLab API field names
- Fix revive issues (dot-imports in tests, missing package comments)
- Fix staticcheck and testifylint issues
- Add package comments where required by linter

This enables updating golangci-lint-action from v6 to v8 in the workflow.
2025-06-27 11:12:19 -07:00

70 lines
1.8 KiB
Go

package websocket
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/gorilla/websocket"
)
func wsHandler(t *testing.T, checkOrigin bool) http.HandlerFunc {
upgrader := websocket.Upgrader{
CheckOrigin: checkOriginFunc(checkOrigin),
}
return func(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
t.Log("upgrade:", err)
return
}
defer func() {
if closeErr := c.Close(); closeErr != nil {
t.Errorf("failed to close websocket connection: %v", closeErr)
}
}()
}
}
func TestCheckOriginFunc(t *testing.T) {
tests := []struct {
name string
checkOrigin bool
origin string
host string
wantErr bool
}{
{"same origin", true, "http://example.com/", "example.com", false},
{"same origin with port", true, "http://example.com:8080/", "example.com:8080", false},
{"fail with different origin", true, "http://example.net/", "example.com", true},
{"success with same origin without check", false, "http://example.com/", "example.com", false},
{"success with different origin without check", false, "http://example.net/", "example.com", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := httptest.NewServer(wsHandler(t, tt.checkOrigin))
u, _ := url.Parse(s.URL)
u.Path = "/"
u.Scheme = "ws"
header := http.Header{
"Origin": []string{tt.origin},
"Host": []string{tt.host},
}
c, _, err := websocket.DefaultDialer.Dial(u.String(), header)
if err == nil {
defer func() {
if closeErr := c.Close(); closeErr != nil {
t.Errorf("failed to close websocket connection: %v", closeErr)
}
}()
}
if (err != nil) != tt.wantErr {
t.Errorf("websocket dial error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}