Skip to content

Commit

Permalink
Removes context from channel
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 26, 2024
1 parent 79e7f29 commit ae9be36
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 65 deletions.
16 changes: 1 addition & 15 deletions channel/channel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package channel

import (
"context"
"net/http"

"github.com/ksysoev/wasabi"
Expand All @@ -13,7 +12,6 @@ type Channel struct {
path string
disptacher wasabi.Dispatcher
connRegistry wasabi.ConnectionRegistry
ctx context.Context
middlewares []Middlewere
config channelConfig
}
Expand Down Expand Up @@ -60,7 +58,7 @@ func (c *Channel) Path() string {

// Handler returns http.Handler for channel
func (c *Channel) Handler() http.Handler {
return c.setContext(c.wrapMiddleware(c.wsConnectionHandler()))
return c.wrapMiddleware(c.wsConnectionHandler())
}

// wsConnectionHandler handles the WebSocket connection and sets up the necessary components for communication.
Expand All @@ -81,11 +79,6 @@ func (c *Channel) wsConnectionHandler() http.Handler {
})
}

// SetContext sets context for channel
func (c *Channel) SetContext(ctx context.Context) {
c.ctx = ctx
}

// Use adds middlewere to channel
func (c *Channel) Use(middlewere Middlewere) {
c.middlewares = append(c.middlewares, middlewere)
Expand All @@ -100,13 +93,6 @@ func (c *Channel) wrapMiddleware(handler http.Handler) http.Handler {
return handler
}

// setContext sets context for handler
func (c *Channel) setContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(c.ctx))
})
}

// WithOriginPatterns sets the origin patterns for the channel.
// The origin patterns are used to validate the Origin header of the WebSocket handshake request.
// If the Origin header does not match any of the patterns, the connection is rejected.
Expand Down
48 changes: 0 additions & 48 deletions channel/channel_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package channel

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/ksysoev/wasabi/mocks"
Expand Down Expand Up @@ -42,7 +40,6 @@ func TestChannel_Handler(t *testing.T) {
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher, NewConnectionRegistry())
channel.SetContext(context.Background())

// Call the Handler method
handler := channel.Handler()
Expand All @@ -51,19 +48,7 @@ func TestChannel_Handler(t *testing.T) {
t.Errorf("Unexpected nil handler")
}
}
func TestChannel_SetContext(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher, NewConnectionRegistry())

ctx := context.Background()
channel.SetContext(ctx)

if channel.ctx != ctx {
t.Errorf("Unexpected context: got %v, expected %v", channel.ctx, ctx)
}
}
func TestChannel_Use(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)
Expand Down Expand Up @@ -118,39 +103,6 @@ func TestChannel_wrapMiddleware(t *testing.T) {
t.Errorf("Unexpected nil wrappedHandler")
}
}
func TestChannel_SetContextMiddleware(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher, NewConnectionRegistry())

// Create a mock handler
var ctx context.Context

mockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx = r.Context()
})

// Create a mock request
mockRequest := httptest.NewRequest(http.MethodGet, "/", http.NoBody)

// Create a mock response recorder
mockResponseRecorder := httptest.NewRecorder()

// Set the context for the channel
channel.SetContext(context.WithValue(context.Background(), struct{ key string }{"test"}, "test"))

// Wrap the mock handler with the setContext middleware
wrappedHandler := channel.setContext(mockHandler)

// Call the wrappedHandler with the mock request and response recorder
wrappedHandler.ServeHTTP(mockResponseRecorder, mockRequest)

// Assert that the context of the request is set to the channel's context
if ctx != channel.ctx {
t.Errorf("Unexpected context: got %v, expected %v", ctx, channel.ctx)
}
}

func TestChannel_WithOriginPatterns(t *testing.T) {
path := "/test/path"
Expand Down
2 changes: 1 addition & 1 deletion examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {

channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Addr)
server := server.NewServer(Addr, server.WithBaseContext(context.Background()))
server.AddChannel(channel)

if err := server.Run(); err != nil {
Expand Down
1 change: 0 additions & 1 deletion interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type RequestHandler interface {
// Channel is interface for channels
type Channel interface {
Path() string
SetContext(ctx context.Context)
Handler() http.Handler
}

Expand Down

0 comments on commit ae9be36

Please sign in to comment.