Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(routing): routing based on the endpoint not domain #249

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})

}
}
Loading