Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mobvious/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call(env)

status, headers, body = @app.call(env)

response = Rack::Response.new(body, status, headers)
response = (body.is_a?(Array) ? Rack::Response.new(body, status, headers) : body)
response_callback(request, response)

[status, headers, body]
Expand Down
19 changes: 13 additions & 6 deletions lib/mobvious/strategies/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ def get_device_type(request)
# @param request [Rack::Request]
# @param response [Rack::Response]
def response_callback(request, response)
response_cookie_already_set = !!response.headers["Set-Cookie"] &&
!!response.headers["Set-Cookie"]["mobvious.device_type"]
request_cookie = request.cookies['mobvious.device_type']

# re-set the cookie to renew the expiration date
if request_cookie && !response_cookie_already_set
set_device_type(response, request_cookie)
# Sprockets::StaticAsset has no method :headers

if response.respond_to? :headers

response_cookie_already_set = !!response.headers["Set-Cookie"] &&
!!response.headers["Set-Cookie"]["mobvious.device_type"]
request_cookie = request.cookies['mobvious.device_type']

# re-set the cookie to renew the expiration date
if request_cookie && !response_cookie_already_set
set_device_type(response, request_cookie)
end

end
end

Expand Down
152 changes: 83 additions & 69 deletions spec/mobvious/manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,104 @@

module Mobvious
class ManagerSpec < MiniTest::Spec
describe Manager do
before do
@app = mock 'app'
@env = mock 'env'
@manager = Mobvious::Manager.new(@app)
@return_value = [200, ['My-Header'], ['body_part_1']]

@app.stubs(:call).with(@env).returns(@return_value)
end

after do
Mobvious.config.clear
end
describe Manager do
before do
@app = mock 'app'
@env = mock 'env'
@manager = Mobvious::Manager.new(@app)
@return_value = [200, ['My-Header'], ['body_part_1']]

it "calls the app and returns what app returns" do
@env.stub_everything
@app.expects(:call).with(@env).returns(@return_value)
@manager.call(@env).must_equal @return_value
end
@rails_return_value = @return_value.clone
@rails_return_value[2] = Rack::Response.new(@rails_return_value.rotate(-1))

describe "having strategies" do
before do
@strategy1 = mock 'strategy1'
@strategy2 = mock 'strategy2'
@strategy3 = mock 'strategy3'
Mobvious.config.strategies << @strategy1
Mobvious.config.strategies << @strategy2
Mobvious.config.strategies << @strategy3
@app.stubs(:call).with(@env).returns(@return_value)
end

it "uses the result of the first successful strategy" do
@app.stub_everything
@app.expects(:call).with(@env).returns(@return_value)
@strategy1.expects(:get_device_type).returns(nil)
@strategy2.expects(:get_device_type).returns(:strategy_2_result)
@env.expects('[]=').with('mobvious.device_type', :strategy_2_result)
@manager.call(@env)
after do
Mobvious.config.clear
end

it "calls strategies with a request object" do
@app.stub_everything
@env.stub_everything
@strategy1.expects(:get_device_type).returns(:result).with() {|param|
param.must_be_instance_of Rack::Request
(param.env == @env).must_equal true
}
@manager.call(@env)
describe "for an app returning a Rack::Response as the body" do
it "calls the app and returns what app returns" do
@env.stub_everything
@app.expects(:call).with(@env).returns(@rails_return_value)
@manager.call(@env).must_equal @rails_return_value
end
end

it "uses default value if no strategy is successful" do
@app.stub_everything
@strategy1.stub_everything
@strategy2.stub_everything
@strategy3.stub_everything
Mobvious.config.default_device_type = :test_default_type
@env.expects('[]=').with('mobvious.device_type', :test_default_type)
@manager.call(@env)
describe "for an app returning an Array as the body" do
it "calls the app and returns what app returns" do
@env.stub_everything
@app.expects(:call).with(@env).returns(@return_value)
@manager.call(@env).must_equal @return_value
end
end

it "calls the response callback on strategies that have it defined" do
@env.stub_everything
@strategy1.stubs(:get_device_type)
@strategy1.stubs(:respond_to?).with(:response_callback).returns(true)
@strategy1.expects(:response_callback).with() {|request, response|
request.must_be_instance_of Rack::Request
(request.env == @env).must_equal true
response.must_be_instance_of Rack::Response
response.body.must_equal @return_value[2]
}
describe "having strategies" do
before do
@strategy1 = mock 'strategy1'
@strategy2 = mock 'strategy2'
@strategy3 = mock 'strategy3'
Mobvious.config.strategies << @strategy1
Mobvious.config.strategies << @strategy2
Mobvious.config.strategies << @strategy3
end

@strategy2.stub_everything
@strategy3.stub_everything
@manager.call(@env)
it "uses the result of the first successful strategy" do
@app.stub_everything
@app.expects(:call).with(@env).returns(@return_value)
@strategy1.expects(:get_device_type).returns(nil)
@strategy2.expects(:get_device_type).returns(:strategy_2_result)
@env.expects('[]=').with('mobvious.device_type', :strategy_2_result)
@manager.call(@env)
end

it "calls strategies with a request object" do
@app.stub_everything
@env.stub_everything
@strategy1.expects(:get_device_type).returns(:result).with() {|param|
param.must_be_instance_of Rack::Request
(param.env == @env).must_equal true
}
@manager.call(@env)
end

it "uses default value if no strategy is successful" do
@app.stub_everything
@strategy1.stub_everything
@strategy2.stub_everything
@strategy3.stub_everything
Mobvious.config.default_device_type = :test_default_type
@env.expects('[]=').with('mobvious.device_type', :test_default_type)
@manager.call(@env)
end

it "calls the response callback on strategies that have it defined" do
@env.stub_everything
@strategy1.stubs(:get_device_type)
@strategy1.stubs(:respond_to?).with(:response_callback).returns(true)
@strategy1.expects(:response_callback).with() {|request, response|
request.must_be_instance_of Rack::Request
(request.env == @env).must_equal true
response.must_be_instance_of Rack::Response
response.body.must_equal @return_value[2]
}

@strategy2.stub_everything
@strategy3.stub_everything
@manager.call(@env)
end
end
end

describe "not having strategies" do
it "uses default value" do
@app.stub_everything
Mobvious.config.default_device_type = :test_default_type
@env.expects('[]=').with('mobvious.device_type', :test_default_type)
@manager.call(@env)
describe "not having strategies" do
it "uses default value" do
@app.stub_everything
Mobvious.config.default_device_type = :test_default_type
@env.expects('[]=').with('mobvious.device_type', :test_default_type)
@manager.call(@env)
end
end
end
end
end
end