-
Notifications
You must be signed in to change notification settings - Fork 3
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 #261 from alphagov/add-govuk-proxy
Add GovukProxy::StaticProxy for development
- Loading branch information
Showing
7 changed files
with
76 additions
and
1 deletion.
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
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
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,19 @@ | ||
require "rack-proxy" | ||
|
||
module GovukProxy | ||
class StaticProxy < Rack::Proxy | ||
def perform_request(env) | ||
request = Rack::Request.new(env) | ||
|
||
# use rack proxy to forward any requests for /assets/static/* | ||
# this regex needs to match the path set for `Rails.application.config.assets.prefix` in Static | ||
# https://github.com/alphagov/static/blob/main/config/initializers/assets.rb | ||
if request.path =~ %r{^/assets/static/} | ||
env["HTTP_HOST"] = @backend.host | ||
super(env) | ||
else | ||
@app.call(env) | ||
end | ||
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module GovukAppConfig | ||
VERSION = "4.8.0".freeze | ||
VERSION = "4.9.0".freeze | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "spec_helper" | ||
require "govuk_app_config/govuk_proxy/static_proxy" | ||
require "rack/mock" | ||
|
||
RSpec.describe GovukProxy::StaticProxy do | ||
def test_request(path, proxied) | ||
dest_host = (proxied ? static_domain : app_domain) | ||
|
||
stub_request(:get, "https://#{dest_host}#{path}") | ||
.with(headers: { "Host" => dest_host }) | ||
.to_return(status: 200, body: "", headers: {}) | ||
|
||
env = Rack::MockRequest.env_for("http://#{app_domain}#{path}") | ||
status, _headers, _response = proxy.call(env) | ||
expect(status.to_i).to eq(200) | ||
end | ||
|
||
# dummy app to validate success | ||
let(:app) { ->(_env) { [200, {}, "success"] } } | ||
let(:app_domain) { "app.domain" } | ||
let(:static_domain) { "static.domain" } | ||
|
||
let(:proxy) { GovukProxy::StaticProxy.new(app, backend: "https://static.domain", streaming: false) } | ||
|
||
it "redirects the request if path begins with /asset/static" do | ||
test_request("/assets/static/a.css", true) | ||
end | ||
|
||
it "ignores requests not with path prefix /asset/static" do | ||
test_request("/assets/app/a.css", false) | ||
end | ||
|
||
it "ignores requests where /asset/static isn't a prefix" do | ||
test_request("/another/prefix/assets/static/a.css", false) | ||
end | ||
|
||
it "ignores requests with no path" do | ||
test_request("/", false) | ||
end | ||
end |