disable the redirection to the first domain functionnality for requests to the default host (related to locomotivecms/engine#785)

did committed Oct 21, 2015
commit cdb2d946f6ae69f8a570b14690cdaf3461491297
Showing 2 changed files with 20 additions and 5 deletions
locomotive/steam/middlewares/site.rb b/lib/locomotive/steam/middlewares/site.rb +9 -3
@@ @@ -41,9 +41,7 @@ module Locomotive::Steam
end
def redirect_to_first_domain_if_enabled(site)
- # the site parameter can be an instance of Locomotive::Steam::Services::Defer and
- # so comparing just site may not be reliable.
- if site.try(:redirect_to_first_domain) && site.domains.first != request.host
+ if redirect_to_first_domain?(site)
klass = request.scheme == 'https' ? URI::HTTPS : URI::HTTP
redirect_to klass.build(
host: site.domains.first,
@@ @@ -53,6 +51,14 @@ module Locomotive::Steam
end
end
+ def redirect_to_first_domain?(site)
+ # the site parameter can be an instance of Locomotive::Steam::Services::Defer and
+ # so comparing just site may not be reliable.
+ !env['steam.is_default_host'] &&
+ site.try(:redirect_to_first_domain) &&
+ site.domains.first != request.host
+ end
+
def log_site(site)
if site.nil?
msg = "Unable to find a site, url asked: #{request.url} ".colorize(color: :light_white, background: :red)
spec/unit/middlewares/site_spec.rb +11 -2
@@ @@ -12,10 +12,12 @@ describe Locomotive::Steam::Middlewares::Site do
let(:url) { 'http://models.example.com' }
let(:app) { ->(env) { [200, env, 'app'] } }
let(:middleware) { Locomotive::Steam::Middlewares::Site.new(app) }
+ let(:is_default_host) { nil }
subject do
env = env_for(url, 'steam.services' => services)
- env['steam.request'] = Rack::Request.new(env)
+ env['steam.request'] = Rack::Request.new(env)
+ env['steam.is_default_host'] = is_default_host
code, env = middleware.call(env)
[code, env['Location']]
end
@@ @@ -58,7 +60,14 @@ describe Locomotive::Steam::Middlewares::Site do
context 'first domain requested' do
- let(:url) { 'http://www.acme.com' }
+ let(:url) { 'http://www.acme.com' }
+ it { is_expected.to eq [200, nil] }
+
+ end
+
+ context 'requesting the default host' do
+
+ let(:is_default_host) { true }
it { is_expected.to eq [200, nil] }
end