Skip to content

feat: add websocket support (https://github.com/kedacore/http-add-on/pull/835) #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.6
require (
github.com/go-logr/logr v1.4.2
github.com/google/go-cmp v0.7.0
github.com/gorilla/websocket v1.5.1
github.com/hashicorp/go-immutable-radix/v2 v2.1.0
github.com/kedacore/keda/v2 v2.16.1
github.com/kelseyhightower/envconfig v1.4.0
Expand All @@ -19,6 +20,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.35.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.35.0
go.opentelemetry.io/otel/sdk v1.35.0
go.opentelemetry.io/otel/trace v1.35.0
go.uber.org/mock v0.5.0
golang.org/x/sync v0.12.0
google.golang.org/grpc v1.71.0
Expand Down Expand Up @@ -56,7 +58,6 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0 // indirect
Expand Down Expand Up @@ -115,7 +116,6 @@ require (
go.opentelemetry.io/otel/exporters/prometheus v0.57.0
go.opentelemetry.io/otel/metric v1.35.0
go.opentelemetry.io/otel/sdk/metric v1.35.0
go.opentelemetry.io/otel/trace v1.35.0
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
golang.org/x/mod v0.23.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lSh
github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/expr-lang/expr v1.17.0 h1:+vpszOyzKLQXC9VF+wA8cVA0tlA984/Wabc/1hF9Whg=
github.com/expr-lang/expr v1.17.0/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
Expand Down
11 changes: 11 additions & 0 deletions interceptor/middleware/responsewriter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package middleware

import (
"bufio"
"errors"
"net"
"net/http"
)

Expand Down Expand Up @@ -46,3 +49,11 @@ func (rw *responseWriter) WriteHeader(statusCode int) {

rw.statusCode = statusCode
}

func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := rw.downstreamResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}

return nil, nil, errors.New("http.Hijacker not implemented")
}
46 changes: 46 additions & 0 deletions interceptor/middleware/responsewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"

"github.com/gorilla/websocket"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -119,4 +120,49 @@ var _ = Describe("responseWriter", func() {
Expect(w.Code).To(Equal(sc))
})
})

Context("Websocket", func() {
It("returns the expected values when http.Hijacker is implemented", func() {

// Create a server that will accept websocket connections
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Upgrade the connection to a websocket connection
upgrader := websocket.Upgrader{}
conn, err := upgrader.Upgrade(w, r, nil)
Expect(err).To(BeNil())
Expect(conn).NotTo(BeNil())

// Write a message to the client
err = conn.WriteMessage(websocket.TextMessage, []byte("hello"))
Expect(err).To(BeNil())

// Close the connection
err = conn.Close()
Expect(err).To(BeNil())
}))
defer server.Close()

// Use a ResponseRecorder to satisfy http.ResponseWriter
recorder := httptest.NewRecorder()
_ = &responseWriter{
downstreamResponseWriter: recorder,
}

// Create a client that will connect to the server
dialer := websocket.Dialer{}
_, _, _ = dialer.Dial(server.URL, nil)
})

It("returns an error when http.Hijacker is not implemented", func() {
w := httptest.NewRecorder()
lrw := &responseWriter{
downstreamResponseWriter: w,
}

c, r, err := lrw.Hijack()
Expect(err).To(MatchError("http.Hijacker not implemented"))
Expect(c).To(BeNil())
Expect(r).To(BeNil())
})
})
})