From 2d40af0dfbcb580c0e72e652d80ed73920d226ef Mon Sep 17 00:00:00 2001 From: Gabriel Guerra Date: Fri, 22 Mar 2024 15:49:36 -0400 Subject: [PATCH] refactor(routing): routing based on the endpoint not domain --- .../app/subsystems/aio/queuing/routing.go | 6 +- .../subsystems/aio/queuing/routing_test.go | 86 +++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 internal/app/subsystems/aio/queuing/routing_test.go diff --git a/internal/app/subsystems/aio/queuing/routing.go b/internal/app/subsystems/aio/queuing/routing.go index 9fced07d..f23896cc 100644 --- a/internal/app/subsystems/aio/queuing/routing.go +++ b/internal/app/subsystems/aio/queuing/routing.go @@ -55,15 +55,15 @@ func NewRouter() Router { } func (r *RouterImpl) Handle(pattern string, handler *RouteHandler) { - // chi routing pattern must begin with '/' - pattern = "/" + pattern + // chi routing pattern must begin with '/' and accept any domain. + pattern = "/" + "{http}://{domain}" + pattern r.patterns.Post(pattern, func(w http.ResponseWriter, r *http.Request) {}) r.handlers[pattern] = handler } func (r *RouterImpl) Match(route string) (*MatchResult, error) { - // chi routing pattern must begin with '/' + // chi routing pattern must begin with '/'. route = "/" + route rctx := chi.NewRouteContext() diff --git a/internal/app/subsystems/aio/queuing/routing_test.go b/internal/app/subsystems/aio/queuing/routing_test.go new file mode 100644 index 00000000..3f052f0d --- /dev/null +++ b/internal/app/subsystems/aio/queuing/routing_test.go @@ -0,0 +1,86 @@ +package queuing + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRouting(t *testing.T) { + + tcs := []struct { + name string + pattern string + route string + expectedResult *MatchResult + expectedErr error + }{ + { + name: "simple .com", + pattern: "/payments/*", + route: "http://demo.example.com/payments/123", + expectedResult: &MatchResult{ + Route: "/http://demo.example.com/payments/123", + RoutePattern: "/{http}://{domain}/payments/*", + Connection: "conn1", + }, + }, + { + name: "simple .io", + pattern: "/payments/*", + route: "http://demo.example.io/payments/123", + expectedResult: &MatchResult{ + Route: "/http://demo.example.io/payments/123", + RoutePattern: "/{http}://{domain}/payments/*", + Connection: "conn1", + }, + }, + { + name: "simple localhost", + pattern: "/payments/*", + route: "http://localhost/payments/123", + expectedResult: &MatchResult{ + Route: "/http://localhost/payments/123", + RoutePattern: "/{http}://{domain}/payments/*", + Connection: "conn1", + }, + }, + { + name: "simple localhost https", + pattern: "/payments/*", + route: "https://localhost/payments/123", + expectedResult: &MatchResult{ + Route: "/https://localhost/payments/123", + RoutePattern: "/{http}://{domain}/payments/*", + Connection: "conn1", + }, + }, + { + name: "simple .com", + pattern: "/payments/*", + route: "http://demo.example.com/analytics/123", + expectedErr: ErrRouteDoesNotMatchAnyPattern, + }, + } + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + router := NewRouter() + + router.Handle(tc.pattern, &RouteHandler{ + Connection: "conn1", + }) + + result, err := router.Match(tc.route) + if tc.expectedErr != nil { + assert.Equal(t, tc.expectedErr, err) + return + } + + assert.Equal(t, tc.expectedResult.Route, result.Route) + assert.Equal(t, tc.expectedResult.RoutePattern, result.RoutePattern) + assert.Equal(t, tc.expectedResult.Connection, result.Connection) + }) + + } +}