Skip to content

Commit

Permalink
Merge pull request #39 from alphagov/redirection-handler
Browse files Browse the repository at this point in the history
Add redirect handler
  • Loading branch information
alext committed Oct 28, 2013
2 parents e6bf197 + ca9b37f commit bfe3616
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
13 changes: 13 additions & 0 deletions handlers/redirect_handler.go
@@ -0,0 +1,13 @@
package handlers

import (
"net/http"
)

func NewRedirectHandler(path string, temporary bool) http.Handler {
statusMoved := http.StatusMovedPermanently
if temporary {
statusMoved = http.StatusFound
}
return http.RedirectHandler(path, statusMoved)
}
12 changes: 12 additions & 0 deletions router.go
Expand Up @@ -31,6 +31,8 @@ type Route struct {
RouteType string `bson:"route_type"`
Handler string `bson:"handler"`
BackendId string `bson:"backend_id"`
RedirectTo string `bson:"redirect_to"`
RedirectType string `bson:"redirect_type"`
}

// NewRouter returns a new empty router instance. You will still need to call
Expand Down Expand Up @@ -144,6 +146,16 @@ func loadRoutes(c *mgo.Collection, mux *triemux.Mux, backends map[string]http.Ha
mux.Handle(route.IncomingPath, prefix, handler)
log.Printf("router: registered %s (prefix: %v) for %s",
route.IncomingPath, prefix, route.BackendId)
case "redirect":
if prefix {
log.Printf("router: found redirect route %+v which is a prefix route, skipping!", route)
continue
}
redirectTemporarily := (route.RedirectType == "temporary")
handler := handlers.NewRedirectHandler(route.RedirectTo, redirectTemporarily)
mux.Handle(route.IncomingPath, false, handler)
log.Printf("router: registered %s -> %s",
route.IncomingPath, route.RedirectTo)
default:
log.Printf("router: found route %+v with unknown handler type "+
"%s, skipping!", route, route.Handler)
Expand Down
39 changes: 39 additions & 0 deletions spec/redirect_spec.rb
@@ -0,0 +1,39 @@
require 'spec_helper'

describe "Redirection" do

describe "simple exact redirect" do
before :each do
add_redirect_route("/foo", "/bar")
add_redirect_route("/foo-temp", "/bar", :redirect_type => 'temporary')
reload_routes
end

it "should redirect permanently by default" do
response = router_request("/foo")
expect(response.code).to eq(301)
end

it "should contain the redirect location" do
response = router_request("/foo")
expect(response.headers['Location']).to eq("/bar")
end

it "should redirect temporarily when asked to" do
response = router_request("/foo-temp")
expect(response.code).to eq(302)
end
end

describe "prefix redirects" do
before :each do
add_redirect_route("/foo", "/bar", :prefix => true)
reload_routes
end

it "should skip prefix routes" do
response = router_request("/foo")
expect(response.code).to eq(404)
end
end
end
4 changes: 4 additions & 0 deletions spec/support/routes.rb
Expand Up @@ -9,6 +9,10 @@ def add_backend_route(path, backend_id, options = {})
add_route path, options.merge(:handler => "backend", :backend_id => backend_id)
end

def add_redirect_route(path, redirect_to, options = {})
add_route path, options.merge(:handler => "redirect", :redirect_to => redirect_to)
end

def add_route(path, attrs = {})
route_type = attrs.delete(:prefix) ? 'prefix' : 'exact'
RoutesHelpers.db["routes"].insert(attrs.merge({
Expand Down

0 comments on commit bfe3616

Please sign in to comment.