add tests to prove it works + refactor
Didier Lafforgue
committed Jun 13, 2018
commit a2ca4cf6c7dba28864b9cc7a38e9759790afae29
Showing 3
changed files with
142 additions
and 79 deletions
locomotive/steam/middlewares/helpers.rb b/lib/locomotive/steam/middlewares/helpers.rb
+79
-2
| @@ | @@ -3,6 +3,70 @@ module Locomotive::Steam |
| module Helpers | |
| + | #= Shortcuts = |
| + | |
| + | def services |
| + | @services ||= env.fetch('steam.services') |
| + | end |
| + | |
| + | def repositories |
| + | @repositories ||= services.repositories |
| + | end |
| + | |
| + | def request |
| + | @request ||= env.fetch('steam.request') |
| + | end |
| + | |
| + | def site |
| + | @site ||= env.fetch('steam.site') |
| + | end |
| + | |
| + | def page |
| + | @page ||= env.fetch('steam.page') |
| + | end |
| + | |
| + | def path |
| + | @path ||= env.fetch('steam.path') |
| + | end |
| + | |
| + | def locale |
| + | @locale ||= env.fetch('steam.locale') |
| + | end |
| + | |
| + | def liquid_assigns |
| + | @liquid_assigns ||= env.fetch('steam.liquid_assigns') |
| + | end |
| + | |
| + | def locales |
| + | site.locales |
| + | end |
| + | |
| + | def default_locale |
| + | site.default_locale |
| + | end |
| + | |
| + | def live_editing? |
| + | !!env['steam.live_editing'] |
| + | end |
| + | |
| + | def mounted_on |
| + | env['steam.mounted_on'] |
| + | end |
| + | |
| + | # if this is a JSON request with a JSON body, try to parse it |
| + | # if we are unable to parse it, fallback to the original params |
| + | def params |
| + | return @params if @params.present? |
| + | |
| + | if json? && (request.post? || request.put?) |
| + | @params = JSON.parse(request.body.read) rescue nil |
| + | end |
| + | |
| + | @params = (@params || request.params).with_indifferent_access |
| + | end |
| + | |
| + | #= Useful getters = |
| + | |
| def html? | |
| ['text/html', 'application/x-www-form-urlencoded', 'multipart/form-data'].include?(self.request.media_type) && | |
| !self.request.xhr? && | |
| @@ | @@ -13,6 +77,8 @@ module Locomotive::Steam |
| self.request.content_type == 'application/json' || File.extname(self.request.path) == '.json' | |
| end | |
| + | #= Helper methods |
| + | |
| def render_response(content, code = 200, type = nil) | |
| @next_response = [code, { 'Content-Type' => type || 'text/html' }, [content]] | |
| end | |
| @@ | @@ -45,8 +111,19 @@ module Locomotive::Steam |
| location.gsub(Regexp.new('^' + mounted_on), '') | |
| end | |
| - | def mounted_on |
| - | request.env['steam.mounted_on'] |
| + | def decorate_entry(entry) |
| + | return nil if entry.nil? |
| + | Locomotive::Steam::Decorators::I18nDecorator.new(entry, locale, default_locale) |
| + | end |
| + | |
| + | def default_liquid_context |
| + | ::Liquid::Context.new({ 'site' => site.to_liquid }, {}, { |
| + | request: request, |
| + | locale: locale, |
| + | site: site, |
| + | services: services, |
| + | repositories: services.repositories |
| + | }, true) |
| end | |
| def log(msg, offset = 2) | |
locomotive/steam/middlewares/thread_safe.rb b/lib/locomotive/steam/middlewares/thread_safe.rb
+2
-77
| @@ | @@ -6,14 +6,10 @@ module Locomotive::Steam::Middlewares |
| attr_accessor :env | |
| def call(env) | |
| - | threadsafed = dup |
| + | threadsafed = dup |
| threadsafed.env = env | |
| - | # time = Benchmark.realtime do |
| - | threadsafed._call # thread-safe purpose |
| - | # end |
| - | |
| - | # puts "[Benchmark][#{self.class.name}] Time elapsed #{time*1000} milliseconds" |
| + | threadsafed._call |
| threadsafed.next | |
| end | |
| @@ | @@ -23,77 +19,6 @@ module Locomotive::Steam::Middlewares |
| @next_response || (@next_response = app.call(env)) | |
| end | |
| - | #= Shortcuts = |
| - | |
| - | def services |
| - | @services ||= env.fetch('steam.services') |
| - | end |
| - | |
| - | def repositories |
| - | @repositories ||= services.repositories |
| - | end |
| - | |
| - | def request |
| - | @request ||= env.fetch('steam.request') |
| - | end |
| - | |
| - | def site |
| - | @site ||= env.fetch('steam.site') |
| - | end |
| - | |
| - | def page |
| - | @page ||= env.fetch('steam.page') |
| - | end |
| - | |
| - | def path |
| - | @path ||= env.fetch('steam.path') |
| - | end |
| - | |
| - | def locale |
| - | @locale ||= env.fetch('steam.locale') |
| - | end |
| - | |
| - | def liquid_assigns |
| - | @liquid_assigns ||= env.fetch('steam.liquid_assigns') |
| - | end |
| - | |
| - | def locales |
| - | site.locales |
| - | end |
| - | |
| - | def default_locale |
| - | site.default_locale |
| - | end |
| - | |
| - | def params |
| - | req = self.request |
| - | if req.content_type == 'application/json' && (req.post? || req.put?) |
| - | req.body.rewind |
| - | @params ||= JSON.parse(req.body.read).with_indifferent_access |
| - | else |
| - | @params ||= req.params.with_indifferent_access |
| - | end |
| - | end |
| - | |
| - | def live_editing? |
| - | !!env['steam.live_editing'] |
| - | end |
| - | |
| - | def decorate_entry(entry) |
| - | return nil if entry.nil? |
| - | Locomotive::Steam::Decorators::I18nDecorator.new(entry, locale, default_locale) |
| - | end |
| - | |
| - | def default_liquid_context |
| - | ::Liquid::Context.new({ 'site' => site.to_liquid }, {}, { |
| - | request: request, |
| - | locale: locale, |
| - | site: site, |
| - | services: services, |
| - | repositories: services.repositories |
| - | }, true) |
| - | end |
| - | |
| end | |
| end | |
spec/unit/middlewares/helpers_spec.rb
+61
-0
| @@ | @@ -58,4 +58,65 @@ describe Locomotive::Steam::Middlewares::Helpers do |
| end | |
| + | describe '#params' do |
| + | |
| + | let(:url) { 'http://models.example.com' } |
| + | let(:app) { ->(env) { [200, env, 'app'] } } |
| + | let(:options) { {} } |
| + | |
| + | before do |
| + | env = env_for(url, options) |
| + | env['steam.request'] = Rack::Request.new(env) |
| + | allow(instance).to receive(:app).and_return(app) |
| + | allow(instance).to receive(:env).and_return(env) |
| + | end |
| + | |
| + | subject { instance.params } |
| + | |
| + | context 'from a GET' do |
| + | |
| + | let(:url) { 'http://models.example.com?foo=bar' } |
| + | |
| + | it { is_expected.to eq('foo' => 'bar') } |
| + | |
| + | end |
| + | |
| + | context 'from a GET (JSON)' do |
| + | |
| + | let(:url) { 'http://models.example.com/api.json?foo=bar' } |
| + | |
| + | it { is_expected.to eq('foo' => 'bar') } |
| + | |
| + | end |
| + | |
| + | context 'from the body of JSON POST request' do |
| + | |
| + | let(:input) { '{"foo": { "bar": 42 } }' } |
| + | |
| + | let(:options) { { |
| + | method: 'POST', |
| + | input: input, |
| + | 'CONTENT_TYPE' => 'application/json' |
| + | } } |
| + | |
| + | it { is_expected.to eq('foo' => { 'bar' => 42 }) } |
| + | |
| + | it 'builds a hash with indifferent access' do |
| + | expect(subject[:foo][:bar]).to eq 42 |
| + | end |
| + | |
| + | context 'the JSON is invalid' do |
| + | |
| + | let(:input) { '{ a: 2 }' } |
| + | |
| + | it 'returns an empty hash' do |
| + | is_expected.to eq({}) |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| end | |