Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Add a Redirect function to the Router closes #245 #246

Merged
merged 2 commits into from
Feb 14, 2017
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
7 changes: 7 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ func (a *App) PATCH(p string, h Handler) RouteInfo {
return a.addRoute("PATCH", p, h)
}

// Redirect from one URL to another URL. Only works for "GET" requests.
func (a *App) Redirect(status int, from, to string) RouteInfo {
return a.GET(from, func(c Context) error {
return c.Redirect(status, to)
})
}

// ServeFiles maps an path to a directory on disk to serve static files.
// Useful for JavaScript, images, CSS, etc...
/*
Expand Down
13 changes: 13 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (

func testApp() *App {
a := New(Options{})
a.Redirect(301, "/foo", "/bar")
a.GET("/bar", func(c Context) error {
return c.Render(200, render.String("bar"))
})

rt := a.Group("/router/tests")

h := func(c Context) error {
Expand Down Expand Up @@ -103,6 +108,14 @@ func Test_Router_Group_Middleware(t *testing.T) {
r.Len(g.Middleware.stack, 2)
}

func Test_Router_Redirect(t *testing.T) {
r := require.New(t)
w := willie.New(testApp())
res := w.Request("/foo").Get()
r.Equal(301, res.Code)
r.Equal("/bar", res.Location())
}

func Test_Router_ServeFiles(t *testing.T) {
r := require.New(t)

Expand Down