Use mate or show! in irb to open in EDITOR
[sinatra.git] / lib / sinatra / test_methods.rb
blobfdd20dd927c1a77f22b77a11c8efef53974fee61
1 require 'uri'
3 module Sinatra
4   
5   module TestMethods
7     %w(get post put delete).each do |verb|
8       module_eval <<-end_eval
9         def #{verb}_it(path, params = {})
10           request = Rack::MockRequest.new(Sinatra::Dispatcher.new)
11           @response = request.#{verb} path, :input => generate_input(params)
12           body
13         end
14       end_eval
15     end
16     
17     def show!(editor = nil)
18       editor = editor || ENV['EDITOR']
19       IO.popen(editor, 'w') do |f| 
20         f.puts "<!--"
21         f.puts result_info
22         f.puts "-->"
23         f.puts
24         f.puts body
25       end
26     end
27     alias :mate :show!
28     
29     def result_info
30       info = <<-end_info
31       # Status: #{status}
32       # Headers: #{headers.inspect}
33       end_info
34     end
36     def response
37       @response || Rack::MockResponse.new(404, {}, '')
38     end
40     def status
41       response.status
42     end
44     def text
45       response.body
46     end
47     alias :xml :text
48     alias :html :text
49     alias :body :text
51     def headers
52       response.headers
53     end
54     
55     private
56     
57       def generate_input(params)
58         params.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
59       end
61   end
62   
63 end