From 6726dfc72c532c6dd01f1cfb3a82cb67bb3258c9 Mon Sep 17 00:00:00 2001 From: Gabriel Guerra Date: Fri, 22 Mar 2024 18:27:58 -0400 Subject: [PATCH] refactor(routing): routing based on the endpoint not domain x2 --- .../app/subsystems/aio/queuing/routing.go | 14 ++++-- .../subsystems/aio/queuing/routing_test.go | 45 ++++++------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/internal/app/subsystems/aio/queuing/routing.go b/internal/app/subsystems/aio/queuing/routing.go index f23896cc..c7e37c61 100644 --- a/internal/app/subsystems/aio/queuing/routing.go +++ b/internal/app/subsystems/aio/queuing/routing.go @@ -6,6 +6,7 @@ import ( "fmt" "html/template" "net/http" + "strings" "github.com/go-chi/chi/v5" "github.com/resonatehq/resonate/internal/util" @@ -55,16 +56,14 @@ func NewRouter() Router { } func (r *RouterImpl) Handle(pattern string, handler *RouteHandler) { - // chi routing pattern must begin with '/' and accept any domain. - pattern = "/" + "{http}://{domain}" + pattern - + // chi routing pattern must begin with '/'. This is the behavior we want. 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 '/'. - route = "/" + route + route = normalizeRoute(route) rctx := chi.NewRouteContext() @@ -110,3 +109,10 @@ func (r *RouterImpl) renderQueue(queueTemplate string, params chi.RouteParams) ( return buf.String(), nil } + +func normalizeRoute(route string) string { + if strings.HasPrefix(route, "/") { + return route + } + return "/" + route +} diff --git a/internal/app/subsystems/aio/queuing/routing_test.go b/internal/app/subsystems/aio/queuing/routing_test.go index 3f052f0d..f395b3d8 100644 --- a/internal/app/subsystems/aio/queuing/routing_test.go +++ b/internal/app/subsystems/aio/queuing/routing_test.go @@ -1,6 +1,7 @@ package queuing import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -16,49 +17,29 @@ func TestRouting(t *testing.T) { expectedErr error }{ { - name: "simple .com", + name: "payment simple", pattern: "/payments/*", - route: "http://demo.example.com/payments/123", + route: "payments/123", // promise id expectedResult: &MatchResult{ - Route: "/http://demo.example.com/payments/123", - RoutePattern: "/{http}://{domain}/payments/*", + Route: "/payments/123", + RoutePattern: "/payments/*", Connection: "conn1", }, }, { - name: "simple .io", + name: "payment simple with slash", pattern: "/payments/*", - route: "http://demo.example.io/payments/123", + route: "/payments/123", // promise id expectedResult: &MatchResult{ - Route: "/http://demo.example.io/payments/123", - RoutePattern: "/{http}://{domain}/payments/*", + Route: "/payments/123", + RoutePattern: "/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", + name: "absolute url does not work", pattern: "/payments/*", - route: "http://demo.example.com/analytics/123", + route: "http://demo.example.com/payments/123", expectedErr: ErrRouteDoesNotMatchAnyPattern, }, } @@ -72,11 +53,13 @@ func TestRouting(t *testing.T) { }) result, err := router.Match(tc.route) - if tc.expectedErr != nil { + if err != nil { assert.Equal(t, tc.expectedErr, err) return } + fmt.Printf("result: %+v\n", result) + assert.Equal(t, tc.expectedResult.Route, result.Route) assert.Equal(t, tc.expectedResult.RoutePattern, result.RoutePattern) assert.Equal(t, tc.expectedResult.Connection, result.Connection)