Skip to content

Commit

Permalink
refactor(routing): routing based on the endpoint not domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Guerra authored and Gabriel Guerra committed Mar 22, 2024
1 parent d674a68 commit 2d40af0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/app/subsystems/aio/queuing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
86 changes: 86 additions & 0 deletions internal/app/subsystems/aio/queuing/routing_test.go
Original file line number Diff line number Diff line change
@@ -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)
})

}
}

0 comments on commit 2d40af0

Please sign in to comment.