Module: Sinatra::Test

Includes:
Rack::Utils
Included in:
TestHarness
Defined in:
lib/sinatra/test.rb,
lib/sinatra/test/spec.rb,
lib/sinatra/test/bacon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegate other missing methods to @response.



55
56
57
58
59
60
61
# File 'lib/sinatra/test.rb', line 55

def method_missing(name, *args, &block)
  if @response && @response.respond_to?(name)
    @response.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/sinatra/test.rb', line 8

def app
  @app
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/sinatra/test.rb', line 8

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/sinatra/test.rb', line 8

def response
  @response
end

Instance Method Details

#bodyObject



51
# File 'lib/sinatra/test.rb', line 51

def body ; @response.body ; end

#delete(path, *args, &b) ⇒ Object



45
# File 'lib/sinatra/test.rb', line 45

def delete(path, *args, &b) ; make_request('DELETE', path, *args, &b) ; end

#follow!Object



47
48
49
# File 'lib/sinatra/test.rb', line 47

def follow!
  make_request 'GET', @response.location
end

#get(path, *args, &b) ⇒ Object



41
# File 'lib/sinatra/test.rb', line 41

def get(path, *args, &b)  ; make_request('GET', path, *args, &b) ; end

#head(path, *args, &b) ⇒ Object



42
# File 'lib/sinatra/test.rb', line 42

def head(path, *args, &b) ; make_request('HEAD', path, *args, &b) ; end

#make_request(verb, path, *args) {|@request| ... } ⇒ Object

Yields:



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
# File 'lib/sinatra/test.rb', line 10

def make_request(verb, path, *args)
  @app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
  fail "@app not set - cannot make request" if @app.nil?
  @request = Rack::MockRequest.new(@app)
  opts, input =
    case args.size
    when 2 # input, env
      input, env = args
      if input.kind_of?(Hash) # params, env
        [env, param_string(input)]
      else
        [env, input]
      end
    when 1 # params
      if (data = args.first).kind_of?(Hash)
        env = (data.delete(:env) || {})
        [env, param_string(data)]
      else
        [{}, data]
      end
    when 0
      [{}, '']
    else
      raise ArgumentError, "zero, one, or two arguments expected"
    end
  opts = rack_opts(opts)
  opts[:input] ||= input
  yield @request if block_given?
  @response = @request.request(verb, path, opts)
end

#post(path, *args, &b) ⇒ Object



43
# File 'lib/sinatra/test.rb', line 43

def post(path, *args, &b) ; make_request('POST', path, *args, &b) ; end

#put(path, *args, &b) ⇒ Object



44
# File 'lib/sinatra/test.rb', line 44

def put(path, *args, &b)  ; make_request('PUT', path, *args, &b) ; end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Also check @response since we delegate there.

Returns:

  • (Boolean)


64
65
66
# File 'lib/sinatra/test.rb', line 64

def respond_to?(symbol, include_private=false)
  super || (@response && @response.respond_to?(symbol, include_private))
end

#shouldObject



6
7
8
# File 'lib/sinatra/test/spec.rb', line 6

def should
  @response.should
end

#statusObject



52
# File 'lib/sinatra/test.rb', line 52

def status ; @response.status ; end