Skip to content

Commit

Permalink
Added redirect support with query params.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestefanello committed Jun 16, 2024
1 parent 71f7de8 commit 6730b6a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
16 changes: 13 additions & 3 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"net/http"
"net/url"

"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/htmx"
Expand Down Expand Up @@ -30,10 +31,19 @@ func GetHandlers() []Handler {
return handlers
}

// redirect redirects to a given route name with optional route parameters
func redirect(ctx echo.Context, route string, routeParams ...any) error {
url := ctx.Echo().Reverse(route, routeParams...)
// redirect redirects to a given route by name with optional route parameters
func redirect(ctx echo.Context, routeName string, routeParams ...any) error {
return doRedirect(ctx, ctx.Echo().Reverse(routeName, routeParams...))
}

// redirectWithQuery redirects to a given route by name with query parameters and optional route parameters
func redirectWithQuery(ctx echo.Context, query url.Values, routeName string, routeParams ...any) error {
dest := fmt.Sprintf("%s?%s", ctx.Echo().Reverse(routeName, routeParams...), query.Encode())
return doRedirect(ctx, dest)
}

// doRedirect performs a redirect to a given URL
func doRedirect(ctx echo.Context, url string) error {
if htmx.GetRequest(ctx).Boosted {
htmx.Response{
Redirect: url,
Expand Down
28 changes: 26 additions & 2 deletions pkg/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"errors"
"net/http"
"net/url"
"testing"

"github.com/labstack/echo/v4"
Expand All @@ -27,21 +28,44 @@ func TestRedirect(t *testing.T) {
return nil
}).Name = "redirect-test"

t.Run("normal", func(t *testing.T) {
t.Run("no query", func(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/abc")
err := redirect(ctx, "redirect-test", "one", "two")
require.NoError(t, err)
assert.Equal(t, "/path/one/and/two", ctx.Response().Header().Get(echo.HeaderLocation))
assert.Equal(t, http.StatusFound, ctx.Response().Status)
})

t.Run("htmx boosted", func(t *testing.T) {
t.Run("no query htmx", func(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/abc")
ctx.Request().Header.Set(htmx.HeaderBoosted, "true")
err := redirect(ctx, "redirect-test", "one", "two")
require.NoError(t, err)
assert.Equal(t, "/path/one/and/two", ctx.Response().Header().Get(htmx.HeaderRedirect))
})

t.Run("query", func(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/abc")
q := url.Values{}
q.Add("a", "1")
q.Add("b", "2")
err := redirectWithQuery(ctx, q, "redirect-test", "one", "two")
require.NoError(t, err)
assert.Equal(t, "/path/one/and/two?a=1&b=2", ctx.Response().Header().Get(echo.HeaderLocation))
assert.Equal(t, http.StatusFound, ctx.Response().Status)
})

t.Run("query htmx", func(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/abc")
ctx.Request().Header.Set(htmx.HeaderBoosted, "true")
q := url.Values{}
q.Add("a", "1")
q.Add("b", "2")
err := redirectWithQuery(ctx, q, "redirect-test", "one", "two")
require.NoError(t, err)
assert.Equal(t, "/path/one/and/two?a=1&b=2", ctx.Response().Header().Get(htmx.HeaderRedirect))
assert.Equal(t, http.StatusFound, ctx.Response().Status)
})
}

func TestFail(t *testing.T) {
Expand Down

0 comments on commit 6730b6a

Please sign in to comment.