Skip to content

Commit

Permalink
Handle old-style /goverment/uploads routes
Browse files Browse the repository at this point in the history
This is currently handled by Whitehall but we're trying to move away
from Whitehall being a frontend application. The next best place for
this bit of code seems to be Government Frontend.

https://github.com/alphagov/whitehall/blob/master/test/functional/asset_manager_redirect_controller_test.rb

The code itself is used to handle assets which are still being requested
on their own URL before we migrated all assets over to Asset Manager.
  • Loading branch information
thomasleese committed Mar 6, 2020
1 parent 8c30acd commit 2c56aca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/asset_manager_redirect_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AssetManagerRedirectController < ApplicationController
def show
asset_url = Plek.new.public_asset_host
if request.host.start_with?("draft-")
asset_url = Plek.new.external_url_for("draft-assets")
end

redirect_to host: URI.parse(asset_url).host, status: :moved_permanently
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

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

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

get "*path/:variant" => "content_items#show",
constraints: {
variant: /print/,
Expand Down
22 changes: 22 additions & 0 deletions test/controllers/asset_manager_redirect_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "test_helper"

class AssetManagerRedirectControllerTest < ActionController::TestCase
setup do
Plek.any_instance.stubs(:public_asset_host).returns("http://asset-host.com")
Plek.any_instance.stubs(:external_url_for).returns("http://draft-asset-host.com")
end

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" }

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" }

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

0 comments on commit 2c56aca

Please sign in to comment.