add first page and base rack test
Joel AZEMAR
committed Jun 18, 2014
commit 47c214c6cd0f0a8872c69fa0135e3cde45a79300
Showing 7
changed files with
113 additions
and 7 deletions
example/server.rb
+4
-3
| @@ | @@ -6,9 +6,10 @@ require 'bundler/setup' |
| Bundler.require | |
| require 'thin' | |
| - | require 'common' |
| + | # require 'common' |
| + | require 'locomotive/common' |
| - | require_relative '../lib/steam' |
| + | require_relative '../lib/locomotive/steam' |
| require_relative '../lib/locomotive/steam/server' | |
| require_relative '../lib/locomotive/steam/initializers' | |
| @@ | @@ -27,4 +28,4 @@ server = Thin::Server.new('localhost', '3333', app) |
| server.threaded = true | |
| Locomotive::Common::Logger.info 'Server started...' | |
| - | server.start |
| \ No newline at end of file | |
| + | server.start |
locomotive/steam.rb b/lib/locomotive/steam.rb
+26
-3
| @@ | @@ -4,9 +4,7 @@ require 'locomotive/common' |
| require_relative 'steam/exceptions' | |
| require_relative 'steam/decorators' | |
| - | |
| - | |
| - | |
| + | require_relative 'steam/configuration' |
| require 'sprockets' | |
| require 'sprockets-sass' | |
| @@ | @@ -19,5 +17,30 @@ require 'mime/types' |
| module Locomotive | |
| module Steam | |
| TEMPLATE_EXTENSIONS = %w(liquid haml) | |
| + | |
| + | class << self |
| + | attr_writer :configuration |
| + | end |
| + | |
| + | def self.configuration |
| + | @configuration ||= Configuration.new |
| + | end |
| + | |
| + | def self.reset |
| + | @configuration = Configuration.new |
| + | end |
| + | |
| + | def self.configure |
| + | yield(configuration) |
| + | end |
| + | |
| + | class << self |
| + | def method_missing(name, *args, &block) |
| + | Locomotive::Steam.configuration.public_send(name) |
| + | rescue |
| + | super |
| + | end |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/configuration.rb b/lib/locomotive/steam/configuration.rb
+13
-0
| @@ | @@ -0,0 +1,13 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class Configuration |
| + | attr_accessor :mode |
| + | |
| + | def initialize |
| + | self.mode = :production |
| + | end |
| + | end |
| + | |
| + | end |
| + | end |
locomotive/steam/middlewares/base.rb b/lib/locomotive/steam/middlewares/base.rb
+7
-1
| @@ | @@ -12,11 +12,17 @@ module Locomotive::Steam |
| end | |
| def call(env) | |
| - | dup._call(env) # thread-safe purpose |
| + | if Locomotive::Steam.mode == :test |
| + | _call(env) |
| + | else |
| + | dup._call(env) # thread-safe purpose |
| + | end |
| end | |
| def _call(env) | |
| + | code, headers, response = @app.call(env) |
| self.set_accessors(env) | |
| + | [code, headers, [response]] |
| end | |
| protected | |
spec/spec_helper.rb
+4
-0
| @@ | @@ -14,6 +14,10 @@ require_relative 'support' |
| Coveralls.wear! | |
| + | Locomotive::Steam.configure do |config| |
| + | config.mode = :test |
| + | end |
| + | |
| RSpec.configure do |config| | |
| config.include Spec::Helpers | |
spec/unit/middlewares/base_spec.rb
+20
-0
| @@ | @@ -0,0 +1,20 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../lib/locomotive/steam/middlewares/base' |
| + | |
| + | describe Locomotive::Steam::Middlewares::Base do |
| + | let(:app) { ->(env) { [200, env, 'app'] }} |
| + | |
| + | let :middleware do |
| + | Locomotive::Steam::Middlewares::Base.new(app) |
| + | end |
| + | |
| + | specify "return 200" do |
| + | code, headers, response = middleware.call env_for('http://www.example.com', { 'steam.path' => 'my path' }) |
| + | expect(code).to eq(200) |
| + | end |
| + | |
| + | def env_for url, opts={} |
| + | Rack::MockRequest.env_for(url, opts) |
| + | end |
| + | end |
spec/unit/middlewares/page_spec.rb
+39
-0
| @@ | @@ -0,0 +1,39 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../lib/locomotive/steam/middlewares/base' |
| + | require_relative '../../../lib/locomotive/steam/middlewares/page' |
| + | |
| + | describe Locomotive::Steam::Middlewares::Page do |
| + | let(:app) { ->(env) { [200, env, 'app'] }} |
| + | |
| + | let :middleware do |
| + | Locomotive::Steam::Middlewares::Page.new(app) |
| + | end |
| + | |
| + | let(:page) do |
| + | double(title: 'title', fullpath: 'fullpath') |
| + | end |
| + | |
| + | before do |
| + | expect(middleware).to receive(:fetch_page).with('wk') { page } |
| + | expect(Locomotive::Common::Logger).to receive(:info).with("Found page \"title\" [fullpath]") { nil } |
| + | end |
| + | |
| + | subject do |
| + | middleware.call env_for('http://www.example.com', { 'steam.locale' => 'wk' }) |
| + | end |
| + | |
| + | specify 'return 200' do |
| + | code, headers, response = subject |
| + | expect(code).to eq(200) |
| + | end |
| + | |
| + | specify 'set page' do |
| + | code, headers, response = subject |
| + | expect(headers['steam.page']).to eq(page) |
| + | end |
| + | |
| + | def env_for url, opts={} |
| + | Rack::MockRequest.env_for(url, opts) |
| + | end |
| + | end |