Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from alphagov/redirection-handler
Add redirect handler
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters