2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2006-09-03 19:34:57 -04:00
|
|
|
|
|
|
|
class SessionTest < Test::Unit::TestCase
|
2008-12-07 17:37:48 -05:00
|
|
|
StubApp = lambda { |env|
|
2009-03-16 00:25:12 -04:00
|
|
|
[200, {"Content-Type" => "text/html", "Content-Length" => "13"}, ["Hello, World!"]]
|
2008-12-07 17:37:48 -05:00
|
|
|
}
|
2008-06-16 01:11:00 -04:00
|
|
|
|
2006-09-03 19:34:57 -04:00
|
|
|
def setup
|
2008-12-07 17:37:48 -05:00
|
|
|
@session = ActionController::Integration::Session.new(StubApp)
|
2006-09-03 19:34:57 -04:00
|
|
|
end
|
2008-06-16 01:11:00 -04:00
|
|
|
|
2006-09-03 19:34:57 -04:00
|
|
|
def test_https_bang_works_and_sets_truth_by_default
|
|
|
|
assert !@session.https?
|
|
|
|
@session.https!
|
|
|
|
assert @session.https?
|
|
|
|
@session.https! false
|
|
|
|
assert !@session.https?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_host!
|
|
|
|
assert_not_equal "glu.ttono.us", @session.host
|
|
|
|
@session.host! "rubyonrails.com"
|
|
|
|
assert_equal "rubyonrails.com", @session.host
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_follow_redirect_raises_when_no_redirect
|
|
|
|
@session.stubs(:redirect?).returns(false)
|
|
|
|
assert_raise(RuntimeError) { @session.follow_redirect! }
|
|
|
|
end
|
|
|
|
|
2007-12-16 19:39:19 -05:00
|
|
|
def test_request_via_redirect_uses_given_method
|
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
|
2009-04-30 15:28:42 -04:00
|
|
|
@session.expects(:process).with(:put, path, args, headers)
|
2007-12-16 19:39:19 -05:00
|
|
|
@session.stubs(:redirect?).returns(false)
|
|
|
|
@session.request_via_redirect(:put, path, args, headers)
|
|
|
|
end
|
2006-09-03 19:34:57 -04:00
|
|
|
|
2007-12-16 19:39:19 -05:00
|
|
|
def test_request_via_redirect_follows_redirects
|
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
|
2007-06-12 20:55:11 -04:00
|
|
|
@session.stubs(:redirect?).returns(true, true, false)
|
2006-09-03 19:34:57 -04:00
|
|
|
@session.expects(:follow_redirect!).times(2)
|
2007-12-16 19:39:19 -05:00
|
|
|
@session.request_via_redirect(:get, path, args, headers)
|
|
|
|
end
|
2006-09-03 19:34:57 -04:00
|
|
|
|
2007-12-16 19:39:19 -05:00
|
|
|
def test_request_via_redirect_returns_status
|
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
|
|
|
|
@session.stubs(:redirect?).returns(false)
|
2006-09-03 19:34:57 -04:00
|
|
|
@session.stubs(:status).returns(200)
|
2007-12-16 19:39:19 -05:00
|
|
|
assert_equal 200, @session.request_via_redirect(:get, path, args, headers)
|
2006-09-03 19:34:57 -04:00
|
|
|
end
|
|
|
|
|
2007-12-16 19:39:19 -05:00
|
|
|
def test_get_via_redirect
|
2007-10-27 16:38:08 -04:00
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
2007-12-16 19:39:19 -05:00
|
|
|
@session.expects(:request_via_redirect).with(:get, path, args, headers)
|
|
|
|
@session.get_via_redirect(path, args, headers)
|
|
|
|
end
|
2006-09-03 19:34:57 -04:00
|
|
|
|
2007-12-16 19:39:19 -05:00
|
|
|
def test_post_via_redirect
|
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
|
|
|
@session.expects(:request_via_redirect).with(:post, path, args, headers)
|
|
|
|
@session.post_via_redirect(path, args, headers)
|
|
|
|
end
|
2006-09-03 19:34:57 -04:00
|
|
|
|
2007-12-16 19:39:19 -05:00
|
|
|
def test_put_via_redirect
|
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
|
|
|
@session.expects(:request_via_redirect).with(:put, path, args, headers)
|
|
|
|
@session.put_via_redirect(path, args, headers)
|
|
|
|
end
|
2006-09-03 19:34:57 -04:00
|
|
|
|
2007-12-16 19:39:19 -05:00
|
|
|
def test_delete_via_redirect
|
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
|
|
|
@session.expects(:request_via_redirect).with(:delete, path, args, headers)
|
|
|
|
@session.delete_via_redirect(path, args, headers)
|
2006-09-03 19:34:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_url_for_with_controller
|
|
|
|
options = {:action => 'show'}
|
|
|
|
mock_controller = mock()
|
|
|
|
mock_controller.expects(:url_for).with(options).returns('/show')
|
|
|
|
@session.stubs(:controller).returns(mock_controller)
|
|
|
|
assert_equal '/show', @session.url_for(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_url_for_without_controller
|
|
|
|
options = {:action => 'show'}
|
|
|
|
mock_rewriter = mock()
|
|
|
|
mock_rewriter.expects(:rewrite).with(options).returns('/show')
|
|
|
|
@session.stubs(:generic_url_rewriter).returns(mock_rewriter)
|
|
|
|
@session.stubs(:controller).returns(nil)
|
|
|
|
assert_equal '/show', @session.url_for(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
@session.expects(:process).with(:get,path,params,headers)
|
|
|
|
@session.get(path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_post
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
@session.expects(:process).with(:post,path,params,headers)
|
|
|
|
@session.post(path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_put
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
@session.expects(:process).with(:put,path,params,headers)
|
|
|
|
@session.put(path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
@session.expects(:process).with(:delete,path,params,headers)
|
|
|
|
@session.delete(path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_head
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
@session.expects(:process).with(:head,path,params,headers)
|
|
|
|
@session.head(path,params,headers)
|
|
|
|
end
|
|
|
|
|
2007-05-21 05:15:16 -04:00
|
|
|
def test_xml_http_request_get
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
headers_after_xhr = headers.merge(
|
|
|
|
"X-Requested-With" => "XMLHttpRequest",
|
|
|
|
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
|
|
|
)
|
|
|
|
@session.expects(:process).with(:get,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:get,path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml_http_request_post
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
headers_after_xhr = headers.merge(
|
|
|
|
"X-Requested-With" => "XMLHttpRequest",
|
|
|
|
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
|
|
|
)
|
|
|
|
@session.expects(:process).with(:post,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:post,path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml_http_request_put
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
headers_after_xhr = headers.merge(
|
|
|
|
"X-Requested-With" => "XMLHttpRequest",
|
|
|
|
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
|
|
|
)
|
|
|
|
@session.expects(:process).with(:put,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:put,path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml_http_request_delete
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
headers_after_xhr = headers.merge(
|
|
|
|
"X-Requested-With" => "XMLHttpRequest",
|
|
|
|
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
|
|
|
)
|
|
|
|
@session.expects(:process).with(:delete,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:delete,path,params,headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml_http_request_head
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
headers_after_xhr = headers.merge(
|
|
|
|
"X-Requested-With" => "XMLHttpRequest",
|
|
|
|
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
|
|
|
)
|
|
|
|
@session.expects(:process).with(:head,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:head,path,params,headers)
|
2006-09-03 19:34:57 -04:00
|
|
|
end
|
2008-06-16 01:11:00 -04:00
|
|
|
|
2007-12-16 19:10:18 -05:00
|
|
|
def test_xml_http_request_override_accept
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah', "Accept" => "application/xml"}
|
|
|
|
headers_after_xhr = headers.merge(
|
|
|
|
"X-Requested-With" => "XMLHttpRequest"
|
|
|
|
)
|
|
|
|
@session.expects(:process).with(:post,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:post,path,params,headers)
|
|
|
|
end
|
2006-09-03 19:34:57 -04:00
|
|
|
end
|
|
|
|
|
2007-02-22 04:28:10 -05:00
|
|
|
class IntegrationTestTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@test = ::ActionController::IntegrationTest.new(:default_test)
|
|
|
|
@test.class.stubs(:fixture_table_names).returns([])
|
|
|
|
@session = @test.open_session
|
|
|
|
end
|
2007-05-21 05:15:16 -04:00
|
|
|
|
2007-02-22 04:28:10 -05:00
|
|
|
def test_opens_new_session
|
|
|
|
@test.class.expects(:fixture_table_names).times(2).returns(['foo'])
|
|
|
|
|
|
|
|
session1 = @test.open_session { |sess| }
|
|
|
|
session2 = @test.open_session # implicit session
|
|
|
|
|
|
|
|
assert_equal ::ActionController::Integration::Session, session1.class
|
|
|
|
assert_equal ::ActionController::Integration::Session, session2.class
|
|
|
|
assert_not_equal session1, session2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tests that integration tests don't call Controller test methods for processing.
|
|
|
|
# Integration tests have their own setup and teardown.
|
|
|
|
class IntegrationTestUsesCorrectClass < ActionController::IntegrationTest
|
|
|
|
def self.fixture_table_names
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_integration_methods_called
|
2008-02-19 16:43:13 -05:00
|
|
|
reset!
|
2008-12-07 17:37:48 -05:00
|
|
|
@integration_session.stubs(:generic_url_rewriter)
|
|
|
|
@integration_session.stubs(:process)
|
|
|
|
|
2007-02-22 04:28:10 -05:00
|
|
|
%w( get post head put delete ).each do |verb|
|
2008-08-31 16:15:26 -04:00
|
|
|
assert_nothing_raised("'#{verb}' should use integration test methods") { __send__(verb, '/') }
|
2007-02-22 04:28:10 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-16 01:11:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class IntegrationProcessTest < ActionController::IntegrationTest
|
|
|
|
class IntegrationController < ActionController::Base
|
|
|
|
def get
|
2008-08-19 19:53:46 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :text => "OK", :status => 200 }
|
|
|
|
format.js { render :text => "JS OK", :status => 200 }
|
|
|
|
end
|
2008-06-16 01:11:00 -04:00
|
|
|
end
|
|
|
|
|
2008-08-19 21:15:51 -04:00
|
|
|
def get_with_params
|
|
|
|
render :text => "foo: #{params[:foo]}", :status => 200
|
|
|
|
end
|
|
|
|
|
2008-06-16 01:11:00 -04:00
|
|
|
def post
|
|
|
|
render :text => "Created", :status => 201
|
|
|
|
end
|
|
|
|
|
|
|
|
def cookie_monster
|
|
|
|
cookies["cookie_1"] = nil
|
|
|
|
cookies["cookie_3"] = "chocolate"
|
|
|
|
render :text => "Gone", :status => 410
|
|
|
|
end
|
2008-08-17 20:18:18 -04:00
|
|
|
|
|
|
|
def redirect
|
|
|
|
redirect_to :action => "get"
|
|
|
|
end
|
2008-06-16 01:11:00 -04:00
|
|
|
end
|
2007-02-22 04:28:10 -05:00
|
|
|
|
2008-06-16 01:11:00 -04:00
|
|
|
def test_get
|
|
|
|
with_test_route_set do
|
|
|
|
get '/get'
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal "OK", status_message
|
2008-08-17 20:18:18 -04:00
|
|
|
assert_response 200
|
|
|
|
assert_response :success
|
|
|
|
assert_response :ok
|
2008-06-16 01:11:00 -04:00
|
|
|
assert_equal({}, cookies)
|
2009-02-01 02:01:49 -05:00
|
|
|
assert_equal "OK", body
|
2008-06-16 01:11:00 -04:00
|
|
|
assert_equal "OK", response.body
|
|
|
|
assert_kind_of HTML::Document, html_document
|
|
|
|
assert_equal 1, request_count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_post
|
|
|
|
with_test_route_set do
|
|
|
|
post '/post'
|
|
|
|
assert_equal 201, status
|
|
|
|
assert_equal "Created", status_message
|
2008-08-17 20:18:18 -04:00
|
|
|
assert_response 201
|
|
|
|
assert_response :success
|
|
|
|
assert_response :created
|
2008-06-16 01:11:00 -04:00
|
|
|
assert_equal({}, cookies)
|
2009-02-01 02:01:49 -05:00
|
|
|
assert_equal "Created", body
|
2008-06-16 01:11:00 -04:00
|
|
|
assert_equal "Created", response.body
|
|
|
|
assert_kind_of HTML::Document, html_document
|
|
|
|
assert_equal 1, request_count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cookie_monster
|
|
|
|
with_test_route_set do
|
|
|
|
self.cookies['cookie_1'] = "sugar"
|
|
|
|
self.cookies['cookie_2'] = "oatmeal"
|
|
|
|
get '/cookie_monster'
|
|
|
|
assert_equal 410, status
|
|
|
|
assert_equal "Gone", status_message
|
2008-08-17 20:18:18 -04:00
|
|
|
assert_response 410
|
|
|
|
assert_response :gone
|
2009-02-07 17:18:09 -05:00
|
|
|
assert_equal "cookie_1=; path=/\ncookie_3=chocolate; path=/", headers["Set-Cookie"]
|
2009-04-26 12:37:11 -04:00
|
|
|
assert_equal({"cookie_1"=>nil, "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies)
|
2008-06-16 01:11:00 -04:00
|
|
|
assert_equal "Gone", response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-17 20:18:18 -04:00
|
|
|
def test_redirect
|
|
|
|
with_test_route_set do
|
|
|
|
get '/redirect'
|
|
|
|
assert_equal 302, status
|
|
|
|
assert_equal "Found", status_message
|
|
|
|
assert_response 302
|
|
|
|
assert_response :redirect
|
|
|
|
assert_response :found
|
|
|
|
assert_equal "<html><body>You are being <a href=\"http://www.example.com/get\">redirected</a>.</body></html>", response.body
|
|
|
|
assert_kind_of HTML::Document, html_document
|
|
|
|
assert_equal 1, request_count
|
2008-12-10 19:38:28 -05:00
|
|
|
|
|
|
|
follow_redirect!
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "/get", path
|
2008-08-17 20:18:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-19 19:53:46 -04:00
|
|
|
def test_xml_http_request_get
|
|
|
|
with_test_route_set do
|
|
|
|
xhr :get, '/get'
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal "OK", status_message
|
|
|
|
assert_response 200
|
|
|
|
assert_response :success
|
|
|
|
assert_response :ok
|
|
|
|
assert_equal "JS OK", response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-19 21:15:51 -04:00
|
|
|
def test_get_with_query_string
|
|
|
|
with_test_route_set do
|
|
|
|
get '/get_with_params?foo=bar'
|
|
|
|
assert_equal '/get_with_params?foo=bar', request.env["REQUEST_URI"]
|
|
|
|
assert_equal '/get_with_params?foo=bar', request.request_uri
|
2009-04-24 21:24:03 -04:00
|
|
|
assert_equal "foo=bar", request.env["QUERY_STRING"]
|
2008-08-19 21:15:51 -04:00
|
|
|
assert_equal 'foo=bar', request.query_string
|
|
|
|
assert_equal 'bar', request.parameters['foo']
|
|
|
|
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal "foo: bar", response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_with_parameters
|
|
|
|
with_test_route_set do
|
|
|
|
get '/get_with_params', :foo => "bar"
|
|
|
|
assert_equal '/get_with_params', request.env["REQUEST_URI"]
|
|
|
|
assert_equal '/get_with_params', request.request_uri
|
|
|
|
assert_equal 'foo=bar', request.env["QUERY_STRING"]
|
|
|
|
assert_equal 'foo=bar', request.query_string
|
|
|
|
assert_equal 'bar', request.parameters['foo']
|
|
|
|
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal "foo: bar", response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-01 02:01:49 -05:00
|
|
|
def test_head
|
|
|
|
with_test_route_set do
|
|
|
|
head '/get'
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal "", body
|
|
|
|
|
|
|
|
head '/post'
|
|
|
|
assert_equal 201, status
|
|
|
|
assert_equal "", body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-16 01:11:00 -04:00
|
|
|
private
|
|
|
|
def with_test_route_set
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.with_options :controller => "IntegrationProcessTest::Integration" do |c|
|
2008-08-17 20:18:18 -04:00
|
|
|
c.connect "/:action"
|
2008-06-16 01:11:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2007-02-22 04:28:10 -05:00
|
|
|
end
|
|
|
|
|
2008-12-17 09:21:20 -05:00
|
|
|
class MetalTest < ActionController::IntegrationTest
|
2008-12-17 11:10:24 -05:00
|
|
|
class Poller
|
|
|
|
def self.call(env)
|
2008-12-17 09:21:20 -05:00
|
|
|
if env["PATH_INFO"] =~ /^\/success/
|
2009-03-16 00:48:07 -04:00
|
|
|
[200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, ["Hello World!"]]
|
2008-12-17 09:21:20 -05:00
|
|
|
else
|
2009-03-16 00:48:07 -04:00
|
|
|
[404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, []]
|
2008-12-17 09:21:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@integration_session = ActionController::Integration::Session.new(Poller)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_successful_get
|
|
|
|
get "/success"
|
|
|
|
assert_response 200
|
|
|
|
assert_response :success
|
|
|
|
assert_response :ok
|
|
|
|
assert_equal "Hello World!", response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_failed_get
|
|
|
|
get "/failure"
|
|
|
|
assert_response 404
|
|
|
|
assert_response :not_found
|
|
|
|
assert_equal '', response.body
|
|
|
|
end
|
|
|
|
end
|