Updating rdoc
[sinatra.git] / lib / sinatra / test / methods.rb
blobeb1cce95102c470d156fa3a3f0fbde65c665a6ab
1 require 'uri'
3 module Sinatra
5   # These methods are for integration testing without an internet connection.  They are available in Test::Unit::TestCase and when in Irb.
6   module Test
7     
8     module Methods
10       # get_it, post_it, put_it, delete_it
11       # Executes the method and returns the result of the body
12       # 
13       # options:
14       # +:params+ a hash of name parameters
15       #
16       # Example:
17       #   get_it '/', :name => 'Blake' # => 'Hello Blake!'
18       #
19       %w(get post put delete).each do |verb|
20         module_eval <<-end_eval
21           def #{verb}_it(path, params = {})
22             request = Rack::MockRequest.new(Sinatra::Dispatcher.new)
23             @response = request.#{verb} path, :input => generate_input(params)
24             body
25           end
26         end_eval
27       end
28         
29       # The response returned by the Event
30       def response
31         @response || Rack::MockResponse.new(404, {}, '')
32       end
34       # The status returned by the event
35       def status
36         response.status
37       end
39       # The text returned by the event
40       def text
41         response.body
42       end
43       alias :xml :text
44       alias :html :text
45       alias :body :text
47       # The headers returned by the event
48       def headers
49         response.headers
50       end
51     
52       private
53     
54         def generate_input(params)
55           params.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
56         end
58     end
60   end
61   
62 end