mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
test refactoring and cleanup
Man. These tests kind of suck. Someone needs to organize this shit semi-logically.
This commit is contained in:
parent
491023a17b
commit
eef025c7f5
7 changed files with 98 additions and 95 deletions
|
@ -4,37 +4,51 @@ module Sinatra
|
|||
|
||||
module Methods
|
||||
|
||||
def easy_env_map
|
||||
{
|
||||
:accept => "HTTP_ACCEPT",
|
||||
:agent => "HTTP_USER_AGENT",
|
||||
:host => "HTTP_HOST",
|
||||
:session => "HTTP_COOKIE",
|
||||
:cookies => "HTTP_COOKIE"
|
||||
}
|
||||
end
|
||||
ENV_KEY_NAMES = {
|
||||
:accept => "HTTP_ACCEPT",
|
||||
:agent => "HTTP_USER_AGENT",
|
||||
:host => "HTTP_HOST",
|
||||
:session => "HTTP_COOKIE",
|
||||
:cookies => "HTTP_COOKIE",
|
||||
:content_type => "CONTENT_TYPE"
|
||||
}
|
||||
|
||||
def session(data, key = 'rack.session')
|
||||
data = data.from_params if data.respond_to?(:from_params)
|
||||
"#{Rack::Utils.escape(key)}=#{[Marshal.dump(data)].pack("m*")}"
|
||||
end
|
||||
|
||||
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
|
||||
def normalize_rack_environment(env)
|
||||
env.inject({}) do |hash,(k,v)|
|
||||
hash[ENV_KEY_NAMES[k] || k] = v
|
||||
hash
|
||||
end
|
||||
end
|
||||
|
||||
%w(get head post put delete).each do |m|
|
||||
define_method("#{m}_it") do |path, *args|
|
||||
env, input = if args.size == 2
|
||||
[args.last, args.first]
|
||||
elsif args.size == 1
|
||||
data = args.first
|
||||
data.is_a?(Hash) ? [map_easys(data.delete(:env) || {}), data.to_params] : [nil, data]
|
||||
end
|
||||
%w(get head post put delete).each do |verb|
|
||||
http_method = verb.upcase
|
||||
define_method("#{verb}_it") do |path, *args|
|
||||
@request = Rack::MockRequest.new(Sinatra.build_application)
|
||||
@response = @request.request(m.upcase, path, {:input => input}.merge(env || {}))
|
||||
opts, input =
|
||||
case args.size
|
||||
when 2 # input, env
|
||||
input, env = args
|
||||
[env, input]
|
||||
when 1 # params
|
||||
if (data = args.first).kind_of?(Hash)
|
||||
env = (data.delete(:env) || {})
|
||||
[env, data.to_params]
|
||||
else
|
||||
[{}, data]
|
||||
end
|
||||
when 0
|
||||
[{}, '']
|
||||
else
|
||||
raise ArgumentError, "zero, one, or two arguments expected"
|
||||
end
|
||||
opts = normalize_rack_environment(opts)
|
||||
opts[:input] ||= input
|
||||
@response = @request.request(http_method, path, opts)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -43,7 +57,11 @@ module Sinatra
|
|||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
@response.send(name, *args) rescue super
|
||||
if @response.respond_to?(name)
|
||||
@response.send(name, *args)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -129,7 +129,7 @@ context "Sinatra" do
|
|||
body.should.be.empty
|
||||
end
|
||||
|
||||
specify "body sets content and ends event" do
|
||||
specify "stop sets content and ends event" do
|
||||
|
||||
Sinatra::EventContext.any_instance.expects(:foo).never
|
||||
|
||||
|
@ -236,15 +236,7 @@ context "Sinatra" do
|
|||
end
|
||||
|
||||
|
||||
specify "put'n with POST" do
|
||||
put '/' do
|
||||
'puted'
|
||||
end
|
||||
post_it '/', :_method => 'PUT'
|
||||
assert_equal 'puted', body
|
||||
end
|
||||
|
||||
specify "put'n wth PUT" do
|
||||
specify "supports PUT" do
|
||||
put '/' do
|
||||
'puted'
|
||||
end
|
||||
|
@ -252,9 +244,17 @@ context "Sinatra" do
|
|||
assert_equal 'puted', body
|
||||
end
|
||||
|
||||
specify "rewrites POSTs with _method param to PUT" do
|
||||
put '/' do
|
||||
'puted'
|
||||
end
|
||||
post_it '/', :_method => 'PUT'
|
||||
assert_equal 'puted', body
|
||||
end
|
||||
|
||||
# Some Ajax libraries downcase the _method parameter value. Make
|
||||
# sure we can handle that.
|
||||
specify "put'n with POST and lowercase _method param" do
|
||||
specify "rewrites POSTs with lowercase _method param to PUT" do
|
||||
put '/' do
|
||||
'puted'
|
||||
end
|
||||
|
@ -263,7 +263,7 @@ context "Sinatra" do
|
|||
end
|
||||
|
||||
# Ignore any _method parameters specified in GET requests or on the query string in POST requests.
|
||||
specify "not put'n with GET" do
|
||||
specify "does not rewrite GETs with _method param to PUT" do
|
||||
get '/' do
|
||||
'getted'
|
||||
end
|
||||
|
@ -272,7 +272,7 @@ context "Sinatra" do
|
|||
body.should.equal 'getted'
|
||||
end
|
||||
|
||||
specify "_method query string parameter ignored on POST" do
|
||||
specify "ignores _method query string parameter on non-POST requests" do
|
||||
post '/' do
|
||||
'posted'
|
||||
end
|
||||
|
@ -284,4 +284,16 @@ context "Sinatra" do
|
|||
body.should.equal 'posted'
|
||||
end
|
||||
|
||||
specify "does not read body if content type is not url encoded" do
|
||||
post '/foo.xml' do
|
||||
request.env['CONTENT_TYPE'].should.be == 'application/xml'
|
||||
request.content_type.should.be == 'application/xml'
|
||||
request.body.read
|
||||
end
|
||||
|
||||
post_it '/foo.xml', '<foo></foo>', :content_type => 'application/xml'
|
||||
@response.should.be.ok
|
||||
@response.body.should.be == '<foo></foo>'
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -216,6 +216,37 @@ context "Events in an app" do
|
|||
body.should.equal "Look ma, a path with spaces!"
|
||||
end
|
||||
|
||||
specify "route based on host" do
|
||||
|
||||
get '/' do
|
||||
'asdf'
|
||||
end
|
||||
|
||||
get_it '/'
|
||||
assert ok?
|
||||
assert_equal('asdf', body)
|
||||
|
||||
get '/foo', :host => 'foo.sinatrarb.com' do
|
||||
'in foo!'
|
||||
end
|
||||
|
||||
get '/foo', :host => 'bar.sinatrarb.com' do
|
||||
'in bar!'
|
||||
end
|
||||
|
||||
get_it '/foo', {}, 'HTTP_HOST' => 'foo.sinatrarb.com'
|
||||
assert ok?
|
||||
assert_equal 'in foo!', body
|
||||
|
||||
get_it '/foo', {}, 'HTTP_HOST' => 'bar.sinatrarb.com'
|
||||
assert ok?
|
||||
assert_equal 'in bar!', body
|
||||
|
||||
get_it '/foo'
|
||||
assert not_found?
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
context "Custom Errors (in general)" do
|
||||
context "Custom Errors" do
|
||||
|
||||
setup do
|
||||
Sinatra.application = nil
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
context "Diddy" do
|
||||
|
||||
setup do
|
||||
Sinatra.application = nil
|
||||
end
|
||||
|
||||
specify "should map urls to different apps" do
|
||||
|
||||
get '/' do
|
||||
'asdf'
|
||||
end
|
||||
|
||||
get_it '/'
|
||||
assert ok?
|
||||
assert_equal('asdf', body)
|
||||
|
||||
get '/foo', :host => 'foo.sinatrarb.com' do
|
||||
'in foo!'
|
||||
end
|
||||
|
||||
get '/foo', :host => 'bar.sinatrarb.com' do
|
||||
'in bar!'
|
||||
end
|
||||
|
||||
get_it '/foo', {}, 'HTTP_HOST' => 'foo.sinatrarb.com'
|
||||
assert ok?
|
||||
assert_equal 'in foo!', body
|
||||
|
||||
get_it '/foo', {}, 'HTTP_HOST' => 'bar.sinatrarb.com'
|
||||
assert ok?
|
||||
assert_equal 'in bar!', body
|
||||
|
||||
get_it '/foo'
|
||||
assert not_found?
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
# XXX: How does any of this have anything to do with REST?
|
||||
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
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
context "Templates (in general)" do
|
||||
context "Templates" do
|
||||
|
||||
specify "are read from files if Symbols" do
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue