| Module | Sinatra::Test |
| In: |
lib/sinatra/test.rb
lib/sinatra/test/bacon.rb lib/sinatra/test/spec.rb |
| RACK_OPT_NAMES | = | { :accept => "HTTP_ACCEPT", :agent => "HTTP_USER_AGENT", :host => "HTTP_HOST", :session => "HTTP_COOKIE", :cookies => "HTTP_COOKIE", :content_type => "CONTENT_TYPE" |
| app | [R] | |
| request | [R] | |
| response | [R] |
# File lib/sinatra/test.rb, line 45
45: def delete(path, *args, &b) ; test_request('DELETE', path, *args, &b) ; end
# File lib/sinatra/test.rb, line 85
85: def env_for(opts={})
86: opts = rack_opts(opts)
87: Rack::MockRequest.env_for(opts)
88: end
# File lib/sinatra/test.rb, line 47
47: def follow!
48: test_request 'GET', @response.location
49: end
# File lib/sinatra/test.rb, line 41
41: def get(path, *args, &b) ; test_request('GET', path, *args, &b) ; end
# File lib/sinatra/test.rb, line 42
42: def head(path, *args, &b) ; test_request('HEAD', path, *args, &b) ; end
Delegate other missing methods to @response.
# File lib/sinatra/test.rb, line 55
55: def method_missing(name, *args, &block)
56: if @response && @response.respond_to?(name)
57: @response.send(name, *args, &block)
58: else
59: super
60: end
61: end
# File lib/sinatra/test.rb, line 90
90: def param_string(hash)
91: hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
92: end
# File lib/sinatra/test.rb, line 43
43: def post(path, *args, &b) ; test_request('POST', path, *args, &b) ; end
# File lib/sinatra/test.rb, line 44
44: def put(path, *args, &b) ; test_request('PUT', path, *args, &b) ; end
# File lib/sinatra/test.rb, line 77
77: def rack_opts(opts)
78: opts.inject({}) do |hash,(key,val)|
79: key = RACK_OPT_NAMES[key] || key
80: hash[key] = val
81: hash
82: end
83: end
Also check @response since we delegate there.
# File lib/sinatra/test.rb, line 64
64: def respond_to?(symbol, include_private=false)
65: super || (@response && @response.respond_to?(symbol, include_private))
66: end
# File lib/sinatra/test.rb, line 10
10: def test_request(verb, path, *args)
11: @app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
12: fail "@app not set - cannot make request" if @app.nil?
13: @request = Rack::MockRequest.new(@app)
14: opts, input =
15: case args.size
16: when 2 # input, env
17: input, env = args
18: if input.kind_of?(Hash) # params, env
19: [env, param_string(input)]
20: else
21: [env, input]
22: end
23: when 1 # params
24: if (data = args.first).kind_of?(Hash)
25: env = (data.delete(:env) || {})
26: [env, param_string(data)]
27: else
28: [{}, data]
29: end
30: when 0
31: [{}, '']
32: else
33: raise ArgumentError, "zero, one, or two arguments expected"
34: end
35: opts = rack_opts(opts)
36: opts[:input] ||= input
37: yield @request if block_given?
38: @response = @request.request(verb, path, opts)
39: end