MUCH LOVE for testing
[sinatra.git] / lib / sinatra / test / methods.rb
blob90502827d285fd2726afc39a6d71b2f28f7d1136
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     
7   module Test
8     
9     module Methods
11       # get_it, post_it, put_it, delete_it
12       # Executes the method and returns the result of the body
13       # 
14       # options:
15       # +:params+ a hash of name parameters
16       #
17       # Example:
18       #   get_it '/', :name => 'Blake' # => 'Hello Blake!'
19       #
20       %w(get post put delete).each do |verb|
21         module_eval <<-end_eval
22           def #{verb}_it(path, params = {})
23             request = Rack::MockRequest.new(Sinatra::Dispatcher.new)
24             @response = request.#{verb} path, :input => generate_input(params)
25             body
26           end
27         end_eval
28       end
29         
30       def response
31         @response || Rack::MockResponse.new(404, {}, '')
32       end
34       def status
35         response.status
36       end
38       def text
39         response.body
40       end
41       alias :xml :text
42       alias :html :text
43       alias :body :text
45       def headers
46         response.headers
47       end
48     
49       private
50     
51         def generate_input(params)
52           params.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
53         end
55     end
57   end
58   
59 end