Skip to content

Commit

Permalink
Support redirecting to asset preview URLs
Browse files Browse the repository at this point in the history
Currently the route for assets assumes that it ends with a file
extension (for example `.pdf`). In the case of the preview URLs (which
end with `/preview`) it thinks the format of the page is `.pdf/preview`.
This gets URL encoded into `.pdf%2Fpreview` by Rails and so the redirect
doesn't work.

Rather than handling the file extension special, this PR changes it to
accept any path containing any number of file extensions or slashes in
no particular format.
  • Loading branch information
thomasleese committed Mar 9, 2020
1 parent e4c9a2e commit 2f9ad4a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

get "healthcheck", to: proc { [200, {}, [""]] }

get "/government/uploads/*path" => "asset_manager_redirect#show", format: true
get "/government/uploads/*path" => "asset_manager_redirect#show", format: false

get "*path/:variant" => "content_items#show",
constraints: {
Expand Down
11 changes: 9 additions & 2 deletions test/controllers/asset_manager_redirect_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ class AssetManagerRedirectControllerTest < ActionController::TestCase

test "redirects asset requests made via public host to the public asset host" do
request.host = "some-host.com"
get :show, params: { path: "asset", format: "txt" }
get :show, params: { path: "asset.txt" }

assert_redirected_to "http://asset-host.com/government/uploads/asset.txt"
end

test "redirects asset requests made via draft host to the draft asset host" do
request.host = "draft-some-host.com"
get :show, params: { path: "asset", format: "txt" }
get :show, params: { path: "asset.txt" }

assert_redirected_to "http://draft-asset-host.com/government/uploads/asset.txt"
end

test "redirects asset preview requests made via public host to the public asset host" do
request.host = "some-host.com"
get :show, params: { path: "asset.csv/preview" }

assert_redirected_to "http://asset-host.com/government/uploads/asset.csv/preview"
end
end

0 comments on commit 2f9ad4a

Please sign in to comment.