From ae9be36df9ce460f009bdbb06e5197440f59bca7 Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Fri, 26 Apr 2024 19:28:41 +0800 Subject: [PATCH] Removes context from channel --- channel/channel.go | 16 +----------- channel/channel_test.go | 48 ----------------------------------- examples/http_backend/main.go | 2 +- interfaces.go | 1 - 4 files changed, 2 insertions(+), 65 deletions(-) diff --git a/channel/channel.go b/channel/channel.go index c4914fb..cdc8a96 100644 --- a/channel/channel.go +++ b/channel/channel.go @@ -1,7 +1,6 @@ package channel import ( - "context" "net/http" "github.com/ksysoev/wasabi" @@ -13,7 +12,6 @@ type Channel struct { path string disptacher wasabi.Dispatcher connRegistry wasabi.ConnectionRegistry - ctx context.Context middlewares []Middlewere config channelConfig } @@ -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. @@ -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) @@ -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. diff --git a/channel/channel_test.go b/channel/channel_test.go index 06c7f5c..52c6d8a 100644 --- a/channel/channel_test.go +++ b/channel/channel_test.go @@ -1,9 +1,7 @@ package channel import ( - "context" "net/http" - "net/http/httptest" "testing" "github.com/ksysoev/wasabi/mocks" @@ -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() @@ -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) @@ -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" diff --git a/examples/http_backend/main.go b/examples/http_backend/main.go index dbb3ab0..5198300 100644 --- a/examples/http_backend/main.go +++ b/examples/http_backend/main.go @@ -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 { diff --git a/interfaces.go b/interfaces.go index d213efd..96153e8 100644 --- a/interfaces.go +++ b/interfaces.go @@ -50,7 +50,6 @@ type RequestHandler interface { // Channel is interface for channels type Channel interface { Path() string - SetContext(ctx context.Context) Handler() http.Handler }