Permalink
Browse files

RESTful testing

To be used in one of two ways:

  post_it '/', :foo => 'bar', :env => { :agent => 'Mac Intel' }

  or

  post_it '/', '<myxml></myxml>', :agent => 'Mac Intel'

to receive this in your EventContext

  For query params:

  post '/' do
    params.inspect
  end

  For XML/JSON/etc.. :

  post '/' do
    request.body.string
  end
  • Loading branch information...
1 parent d3f824c commit 307f93c6f100d7144814d06863e821e65753c1b8 Blake Mizerany committed Apr 10, 2008
Showing with 44 additions and 27 deletions.
  1. +4 −0 lib/sinatra.rb
  2. +22 −25 lib/sinatra/test/methods.rb
  3. +2 −2 test/application_test.rb
  4. +16 −0 test/rest_test.rb
View
@@ -664,6 +664,10 @@ def params
h.merge(@route_params.merge(@request.params))
end
end
+
+ def data
+ @data ||= params.keys.first
+ end
def stop(*args)
throw :halt, args
@@ -13,35 +13,32 @@ module Sinatra
module Test
module Methods
-
- def get_it(path, params = {}, options = {})
- agent = params.delete(:agent)
- @request = Rack::MockRequest.new(Sinatra.build_application)
- @response = @request.get(path, options.merge(:input => params.to_params, :agent => agent))
- end
-
- def head_it(path, params = {}, options = {})
- agent = params.delete(:agent)
- @request = Rack::MockRequest.new(Sinatra.build_application)
- @response = @request.request('HEAD', path, options.merge(:input => params.to_params, :agent => agent))
- end
- def post_it(path, params = {}, options = {})
- agent = params.delete(:agent)
- @request = Rack::MockRequest.new(Sinatra.build_application)
- @response = @request.post(path, options.merge(:input => params.to_params, :agent => agent))
+ def easy_env_map
+ {
+ :accept => 'HTTP_ACCEPT',
+ :agent => 'HTTP_AGENT',
+ :host => 'HTTP_POST'
+ }
end
-
- def put_it(path, params = {}, options = {})
- agent = params.delete(:agent)
- @request = Rack::MockRequest.new(Sinatra.build_application)
- @response = @request.put(path, options.merge(:input => params.to_params, :agent => agent))
+
+ def map_easys(params)
+ easy_env_map.inject(params.dup) do |m, (from, to)|
+ m[to] = m.delete(from) if m.has_key?(from); m
+ end
end
- def delete_it(path, params = {}, options = {})
- agent = params.delete(:agent)
- @request = Rack::MockRequest.new(Sinatra.build_application)
- @response = @request.delete(path, options.merge(:input => params.to_params, :agent => agent))
+ %w(get head post put delete).each do |m|
+ define_method("#{m}_it") do |path, *args|
+ request = Rack::MockRequest.new(Sinatra.application)
+ env, input = if args.size == 2
+ [args.last, args.first]
+ elsif args.size == 1
+ data = args.first
+ data.is_a?(Hash) ? [data.delete(:env), data.to_params] : [nil, data]
+ end
+ @response = request.request(m.upcase, path, {:input => input}.merge(env || {}))
+ end
end
def follow!
View
@@ -134,7 +134,7 @@ def foo
request.env['HTTP_USER_AGENT']
end
- get_it '/', :agent => 'Windows'
+ get_it '/', :env => { :agent => 'Windows' }
should.be.ok
body.should.equal 'Windows'
@@ -149,7 +149,7 @@ def foo
params[:agent].first
end
- get_it '/', :agent => 'Windows NT'
+ get_it '/', :env => { :agent => 'Windows NT' }
body.should.equal 'NT'
View
@@ -0,0 +1,16 @@
+require File.dirname(__FILE__) + '/helper'
+
+context "RESTful tests" do
+
+ specify "should take xml" do
+ post '/foo.xml' do
+ request.body.string
+ end
+
+ post_it '/foo.xml', '<myxml></myxml>'
+ assert ok?
+ assert_equal('<myxml></myxml>', body)
+ end
+
+end
+

0 comments on commit 307f93c

Please sign in to comment.