raise an exception (or not depending on the configuration) if no site has been found when processing a request

did committed Aug 24, 2015
commit 017c1efc31b5c551c1506d83e2b30ab73b6c571a
Showing 4 changed files with 64 additions and 5 deletions
locomotive/steam/configuration.rb b/lib/locomotive/steam/configuration.rb +9 -0
@@ @@ -93,6 +93,15 @@ module Locomotive
attr_accessor :moneta
def moneta; @moneta.nil? ? { store: Moneta.new(:Memory, expires: true) } : @moneta; end
+ # Render a 404 page if no site has been found.
+ # If Steam is embedded in another app, it's better to let the app handle
+ # the no site case.
+ #
+ # default: true
+ #
+ attr_accessor :render_404_if_no_site
+ def render_404_if_no_site; @render_404_if_no_site.nil? ? true : @render_404_if_no_site; end
+
# Lambda called once a Services instance has been built.
# It is used when we want to change one of the services
#
locomotive/steam/errors.rb b/lib/locomotive/steam/errors.rb +3 -0
@@ @@ -1,5 +1,8 @@
module Locomotive::Steam
+ class NoSiteException < ::Exception
+ end
+
class RenderError < ::StandardError
LINES_RANGE = 10
locomotive/steam/middlewares/site.rb b/lib/locomotive/steam/middlewares/site.rb +9 -5
@@ @@ -11,9 +11,7 @@ module Locomotive::Steam
def _call
site = find_site
- # render a simple message if the service was not able to find a site
- # based on the request.
- render_no_site if site.nil?
+ no_site! if site.nil?
# log anyway
log_site(site)
@@ @@ -30,8 +28,14 @@ module Locomotive::Steam
end
end
- def render_no_site
- render_response('Hi, we are sorry but no site was found.', 404, 'text/html')
+ def no_site!
+ # render a simple message if the service was not able to find a site
+ # based on the request.
+ if services.configuration.render_404_if_no_site
+ render_response('Hi, we are sorry but no site was found.', 404, 'text/html')
+ else
+ raise NoSiteException.new
+ end
end
def log_site(site)
spec/unit/middlewares/site_spec.rb +43 -0
@@ @@ -0,0 +1,43 @@
+ require 'spec_helper'
+
+ require_relative '../../../lib/locomotive/steam/middlewares/threadsafe'
+ require_relative '../../../lib/locomotive/steam/middlewares/helpers'
+ require_relative '../../../lib/locomotive/steam/middlewares/site'
+
+ describe Locomotive::Steam::Middlewares::Site do
+
+ let(:render_404) { true }
+ let(:configuration) { instance_double('SimpleConfiguration', render_404_if_no_site: render_404) }
+ let(:services) { instance_double('SimpleServices', configuration: configuration) }
+ let(:url) { 'http://models.example.com' }
+ let(:app) { ->(env) { [200, env, 'app'] } }
+ let(:middleware) { Locomotive::Steam::Middlewares::Site.new(app) }
+
+ subject do
+ env = env_for(url, 'steam.services' => services)
+ env['steam.request'] = Rack::Request.new(env)
+ code, env = middleware.call(env)
+ [code, env['Location']]
+ end
+
+ describe 'no site' do
+
+ before { expect(services).to receive(:current_site).and_return(nil) }
+
+ describe 'render_404 option on' do
+ it { is_expected.to eq [404, nil] }
+ end
+
+ describe 'render_404 option off' do
+
+ let(:render_404) { false }
+
+ it 'raises an exception' do
+ expect { subject }.to raise_exception(Locomotive::Steam::NoSiteException)
+ end
+
+ end
+
+ end
+
+ end