Skip to content
Browse files

test refactoring and cleanup

Man. These tests kind of suck. Someone needs to organize
this shit semi-logically.
  • Loading branch information...
1 parent 491023a commit eef025c7f515f777fc780dec526f379dd7048648 @rtomayko rtomayko committed Sep 7, 2008
Showing with 93 additions and 90 deletions.
  1. +40 −22 lib/sinatra/test/methods.rb
  2. +20 −8 test/app_test.rb
  3. +31 −0 test/application_test.rb
  4. +1 −1 test/custom_error_test.rb
  5. +0 −41 test/diddy_test.rb
  6. +0 −17 test/rest_test.rb
  7. +1 −1 test/template_test.rb
View
62 lib/sinatra/test/methods.rb
@@ -4,37 +4,51 @@ module Test
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 @@ def follow!
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
View
28 test/app_test.rb
@@ -129,7 +129,7 @@
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,25 +236,25 @@
end
- specify "put'n with POST" do
+ specify "supports PUT" do
put '/' do
'puted'
end
- post_it '/', :_method => 'PUT'
+ put_it '/'
assert_equal 'puted', body
end
- specify "put'n wth PUT" do
+ specify "rewrites POSTs with _method param to PUT" do
put '/' do
'puted'
end
- put_it '/'
+ 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 @@
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 @@
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 @@
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
View
31 test/application_test.rb
@@ -216,6 +216,37 @@ def foo
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
View
2 test/custom_error_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
-context "Custom Errors (in general)" do
+context "Custom Errors" do
setup do
Sinatra.application = nil
View
41 test/diddy_test.rb
@@ -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
-
View
17 test/rest_test.rb
@@ -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
-
View
2 test/template_test.rb
@@ -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

0 comments on commit eef025c

Please sign in to comment.
Something went wrong with that request. Please try again.