Skip to content

Commit

Permalink
RESTful testing
Browse files Browse the repository at this point in the history
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
Blake Mizerany committed Apr 10, 2008
1 parent d3f824c commit 307f93c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
4 changes: 4 additions & 0 deletions lib/sinatra.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ def params
h.merge(@route_params.merge(@request.params)) h.merge(@route_params.merge(@request.params))
end end
end end

def data
@data ||= params.keys.first
end


def stop(*args) def stop(*args)
throw :halt, args throw :halt, args
Expand Down
47 changes: 22 additions & 25 deletions lib/sinatra/test/methods.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,35 +13,32 @@ module Sinatra
module Test module Test


module Methods 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 = {}) def easy_env_map
agent = params.delete(:agent) {
@request = Rack::MockRequest.new(Sinatra.build_application) :accept => 'HTTP_ACCEPT',
@response = @request.post(path, options.merge(:input => params.to_params, :agent => agent)) :agent => 'HTTP_AGENT',
:host => 'HTTP_POST'
}
end end

def put_it(path, params = {}, options = {}) def map_easys(params)
agent = params.delete(:agent) easy_env_map.inject(params.dup) do |m, (from, to)|
@request = Rack::MockRequest.new(Sinatra.build_application) m[to] = m.delete(from) if m.has_key?(from); m
@response = @request.put(path, options.merge(:input => params.to_params, :agent => agent)) end
end end


def delete_it(path, params = {}, options = {}) %w(get head post put delete).each do |m|
agent = params.delete(:agent) define_method("#{m}_it") do |path, *args|
@request = Rack::MockRequest.new(Sinatra.build_application) request = Rack::MockRequest.new(Sinatra.application)
@response = @request.delete(path, options.merge(:input => params.to_params, :agent => agent)) 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 end


def follow! def follow!
Expand Down
4 changes: 2 additions & 2 deletions test/application_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def foo
request.env['HTTP_USER_AGENT'] request.env['HTTP_USER_AGENT']
end end


get_it '/', :agent => 'Windows' get_it '/', :env => { :agent => 'Windows' }
should.be.ok should.be.ok
body.should.equal 'Windows' body.should.equal 'Windows'


Expand All @@ -149,7 +149,7 @@ def foo
params[:agent].first params[:agent].first
end end


get_it '/', :agent => 'Windows NT' get_it '/', :env => { :agent => 'Windows NT' }


body.should.equal 'NT' body.should.equal 'NT'


Expand Down
16 changes: 16 additions & 0 deletions test/rest_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -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.