-
Notifications
You must be signed in to change notification settings - Fork 19
/
test_helper.rb
95 lines (77 loc) · 2.01 KB
/
test_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
begin
# Require the preresolved locked set of gems.
require File.expand_path('../../.bundle/environment', __FILE__)
rescue LoadError
# Fallback on doing the resolve at runtime.
require 'rubygems'
require 'bundler'
Bundler.setup
end
Bundler.require(:default, :test)
require 'rack/test'
require 'riot'
require 'warden'
require 'timecop'
require 'rack/utils'
begin
require 'simplecov'
require 'simplecov-html'
SimpleCov.start
rescue LoadError => e
# swallow, code coverage is only supported on mri
end
class Riot::Situation
include Rack::Test::Methods
include Warden::Test::Helpers
FAILURE_APP = lambda{|e|[401, {"Content-Type" => "text/plain"}, ["You Fail!"]] }
def env_with_params(path = "/", params = {}, env = {})
method = params.delete(:method) || "GET"
env = { 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => "#{method}" }.merge(env)
Rack::MockRequest.env_for("#{path}?#{Rack::Utils.build_query(params)}", env)
end
def setup_rack(app = nil, opts = {}, &block)
app ||= block if block_given?
opts[:failure_app] ||= failure_app
opts[:default_strategies] ||= [:password]
opts[:default_serializers] ||= [:session]
Rack::Builder.new do
use opts[:session] || Warden::Spec::Helpers::Session
use Warden::Manager, opts
run app
end
end
def valid_response
Rack::Response.new("OK").finish
end
def failure_app
Warden::Spec::Helpers::FAILURE_APP
end
def success_app
lambda{|e| [200, {"Content-Type" => "text/plain"}, ["You Win"]]}
end
class Session
attr_accessor :app
def initialize(app,configs = {})
@app = app
end
def call(e)
e['rack.session'] ||= {}
@app.call(e)
end
end # session
def app
@app
end
end
class Riot::Context
# Set the Rack app which is to be tested.
#
# context "MyApp" do
# app { [200, {}, "Hello!"] }
# setup { get '/' }
# asserts(:status).equals(200)
# end
def app(app=nil, &block)
setup { @app = (app || block) }
end
end