2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2009-09-25 12:10:39 -04:00
|
|
|
require 'controller/fake_controllers'
|
2014-07-16 18:41:47 -04:00
|
|
|
require 'rails/engine'
|
2006-09-03 19:34:57 -04:00
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class SessionTest < ActiveSupport::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
|
2010-09-24 20:15:52 -04:00
|
|
|
@session = ActionDispatch::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
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def test_patch_via_redirect
|
|
|
|
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
|
|
|
@session.expects(:request_via_redirect).with(:patch, path, args, headers)
|
|
|
|
@session.patch_via_redirect(path, args, headers)
|
|
|
|
end
|
|
|
|
|
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_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
|
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def test_patch
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
@session.expects(:process).with(:patch,path,params,headers)
|
|
|
|
@session.patch(path,params,headers)
|
|
|
|
end
|
|
|
|
|
2006-09-03 19:34:57 -04:00
|
|
|
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(
|
2009-05-19 17:48:05 -04:00
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
|
|
|
|
"HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
|
2007-05-21 05:15:16 -04:00
|
|
|
)
|
|
|
|
@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(
|
2009-05-19 17:48:05 -04:00
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
|
|
|
|
"HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
|
2007-05-21 05:15:16 -04:00
|
|
|
)
|
|
|
|
@session.expects(:process).with(:post,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:post,path,params,headers)
|
|
|
|
end
|
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def test_xml_http_request_patch
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
headers_after_xhr = headers.merge(
|
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
|
|
|
|
"HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
|
|
|
|
)
|
|
|
|
@session.expects(:process).with(:patch,path,params,headers_after_xhr)
|
|
|
|
@session.xml_http_request(:patch,path,params,headers)
|
|
|
|
end
|
|
|
|
|
2007-05-21 05:15:16 -04:00
|
|
|
def test_xml_http_request_put
|
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
|
|
|
headers_after_xhr = headers.merge(
|
2009-05-19 17:48:05 -04:00
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
|
|
|
|
"HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
|
2007-05-21 05:15:16 -04:00
|
|
|
)
|
|
|
|
@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(
|
2009-05-19 17:48:05 -04:00
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
|
|
|
|
"HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
|
2007-05-21 05:15:16 -04:00
|
|
|
)
|
|
|
|
@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(
|
2009-05-19 17:48:05 -04:00
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
|
|
|
|
"HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
|
2007-05-21 05:15:16 -04:00
|
|
|
)
|
|
|
|
@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
|
2009-05-19 17:48:05 -04:00
|
|
|
path = "/index"; params = "blah"; headers = {:location => 'blah', "HTTP_ACCEPT" => "application/xml"}
|
2007-12-16 19:10:18 -05:00
|
|
|
headers_after_xhr = headers.merge(
|
2009-05-19 17:48:05 -04:00
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
|
2007-12-16 19:10:18 -05:00
|
|
|
)
|
|
|
|
@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
|
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class IntegrationTestTest < ActiveSupport::TestCase
|
2007-02-22 04:28:10 -05:00
|
|
|
def setup
|
2010-10-01 20:54:50 -04:00
|
|
|
@test = ::ActionDispatch::IntegrationTest.new(:app)
|
2007-02-22 04:28:10 -05:00
|
|
|
@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
|
|
|
|
session1 = @test.open_session { |sess| }
|
|
|
|
session2 = @test.open_session # implicit session
|
|
|
|
|
2010-05-17 14:40:56 -04:00
|
|
|
assert_respond_to session1, :assert_template, "open_session makes assert_template available"
|
|
|
|
assert_respond_to session2, :assert_template, "open_session makes assert_template available"
|
2010-03-22 20:04:56 -04:00
|
|
|
assert !session1.equal?(session2)
|
2007-02-22 04:28:10 -05:00
|
|
|
end
|
2009-08-30 23:46:48 -04:00
|
|
|
|
|
|
|
# RSpec mixes Matchers (which has a #method_missing) into
|
|
|
|
# IntegrationTest's superclass. Make sure IntegrationTest does not
|
|
|
|
# try to delegate these methods to the session object.
|
|
|
|
def test_does_not_prevent_method_missing_passing_up_to_ancestors
|
|
|
|
mixin = Module.new do
|
|
|
|
def method_missing(name, *args)
|
|
|
|
name.to_s == 'foo' ? 'pass' : super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@test.class.superclass.__send__(:include, mixin)
|
|
|
|
begin
|
|
|
|
assert_equal 'pass', @test.foo
|
|
|
|
ensure
|
|
|
|
# leave other tests as unaffected as possible
|
|
|
|
mixin.__send__(:remove_method, :method_missing)
|
|
|
|
end
|
|
|
|
end
|
2007-02-22 04:28:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Tests that integration tests don't call Controller test methods for processing.
|
|
|
|
# Integration tests have their own setup and teardown.
|
2010-09-24 20:15:52 -04:00
|
|
|
class IntegrationTestUsesCorrectClass < ActionDispatch::IntegrationTest
|
2007-02-22 04:28:10 -05:00
|
|
|
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)
|
|
|
|
|
2013-05-16 13:39:18 -04:00
|
|
|
%w( get post head patch 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
|
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class IntegrationProcessTest < ActionDispatch::IntegrationTest
|
2008-06-16 01:11:00 -04:00
|
|
|
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
|
|
|
|
|
2010-01-15 13:35:18 -05:00
|
|
|
def method
|
2010-04-03 23:23:23 -04:00
|
|
|
render :text => "method: #{request.method.downcase}"
|
2010-01-15 13:35:18 -05:00
|
|
|
end
|
|
|
|
|
2008-06-16 01:11:00 -04:00
|
|
|
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
|
|
|
|
2010-04-05 18:01:33 -04:00
|
|
|
def set_cookie
|
|
|
|
cookies["foo"] = 'bar'
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_cookie
|
|
|
|
render :text => cookies["foo"]
|
|
|
|
end
|
|
|
|
|
2008-08-17 20:18:18 -04:00
|
|
|
def redirect
|
2013-07-16 08:27:22 -04:00
|
|
|
redirect_to action_url('get')
|
2008-08-17 20:18:18 -04:00
|
|
|
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
|
2009-05-11 23:04:39 -04:00
|
|
|
assert_equal({}, cookies.to_hash)
|
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
|
2014-07-15 12:30:28 -04:00
|
|
|
assert_kind_of Nokogiri::HTML::Document, html_document
|
2008-06-16 01:11:00 -04:00
|
|
|
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
|
2009-05-11 23:04:39 -04:00
|
|
|
assert_equal({}, cookies.to_hash)
|
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
|
2014-07-15 12:30:28 -04:00
|
|
|
assert_kind_of Nokogiri::HTML::Document, html_document
|
2008-06-16 01:11:00 -04:00
|
|
|
assert_equal 1, request_count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-04 18:31:22 -05:00
|
|
|
test 'response cookies are added to the cookie jar for the next request' do
|
2008-06-16 01:11:00 -04:00
|
|
|
with_test_route_set do
|
|
|
|
self.cookies['cookie_1'] = "sugar"
|
|
|
|
self.cookies['cookie_2'] = "oatmeal"
|
|
|
|
get '/cookie_monster'
|
2009-02-07 17:18:09 -05:00
|
|
|
assert_equal "cookie_1=; path=/\ncookie_3=chocolate; path=/", headers["Set-Cookie"]
|
2009-05-11 23:04:39 -04:00
|
|
|
assert_equal({"cookie_1"=>"", "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies.to_hash)
|
2008-06-16 01:11:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-05 18:01:33 -04:00
|
|
|
test 'cookie persist to next request' do
|
|
|
|
with_test_route_set do
|
|
|
|
get '/set_cookie'
|
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
assert_equal "foo=bar; path=/", headers["Set-Cookie"]
|
|
|
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
|
|
|
|
|
|
|
get '/get_cookie'
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "bar", body
|
|
|
|
|
|
|
|
assert_equal nil, headers["Set-Cookie"]
|
|
|
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'cookie persist to next request on another domain' do
|
|
|
|
with_test_route_set do
|
|
|
|
host! "37s.backpack.test"
|
|
|
|
|
|
|
|
get '/set_cookie'
|
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
assert_equal "foo=bar; path=/", headers["Set-Cookie"]
|
|
|
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
|
|
|
|
|
|
|
get '/get_cookie'
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "bar", body
|
|
|
|
|
|
|
|
assert_equal nil, headers["Set-Cookie"]
|
|
|
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
|
|
|
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
|
2014-07-15 12:30:28 -04:00
|
|
|
assert_kind_of Nokogiri::HTML::Document, html_document
|
2008-08-17 20:18:18 -04:00
|
|
|
assert_equal 1, request_count
|
2008-12-10 19:38:28 -05:00
|
|
|
|
|
|
|
follow_redirect!
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "/get", path
|
2014-05-13 00:03:38 -04:00
|
|
|
|
|
|
|
get '/moved'
|
|
|
|
assert_response :redirect
|
|
|
|
assert_redirected_to '/method'
|
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
|
|
|
|
|
2011-07-28 00:01:29 -04:00
|
|
|
def test_request_with_bad_format
|
|
|
|
with_test_route_set do
|
|
|
|
xhr :get, '/get.php'
|
|
|
|
assert_equal 406, status
|
|
|
|
assert_response 406
|
|
|
|
assert_response :not_acceptable
|
|
|
|
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"]
|
2010-03-03 03:31:55 -05:00
|
|
|
assert_equal '/get_with_params?foo=bar', request.fullpath
|
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"
|
2010-03-02 21:57:02 -05:00
|
|
|
assert_equal '/get_with_params', request.env["PATH_INFO"]
|
|
|
|
assert_equal '/get_with_params', request.path_info
|
2008-08-19 21:15:51 -04:00
|
|
|
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
|
2010-01-15 13:35:18 -05:00
|
|
|
|
|
|
|
get '/get/method'
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal "method: get", body
|
|
|
|
|
|
|
|
head '/get/method'
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal "", body
|
2009-02-01 02:01:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-25 12:10:39 -04:00
|
|
|
def test_generate_url_with_controller
|
|
|
|
assert_equal 'http://www.example.com/foo', url_for(:controller => "foo")
|
|
|
|
end
|
|
|
|
|
2013-01-14 10:54:58 -05:00
|
|
|
def test_port_via_host!
|
|
|
|
with_test_route_set do
|
|
|
|
host! 'www.example.com:8080'
|
|
|
|
get '/get'
|
|
|
|
assert_equal 8080, request.port
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_port_via_process
|
|
|
|
with_test_route_set do
|
|
|
|
get 'http://www.example.com:8080/get'
|
|
|
|
assert_equal 8080, request.port
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_https_and_port_via_host_and_https!
|
|
|
|
with_test_route_set do
|
|
|
|
host! 'www.example.com'
|
|
|
|
https! true
|
|
|
|
|
|
|
|
get '/get'
|
|
|
|
assert_equal 443, request.port
|
|
|
|
assert_equal true, request.ssl?
|
|
|
|
|
|
|
|
host! 'www.example.com:443'
|
|
|
|
https! true
|
|
|
|
|
|
|
|
get '/get'
|
|
|
|
assert_equal 443, request.port
|
|
|
|
assert_equal true, request.ssl?
|
|
|
|
|
|
|
|
host! 'www.example.com:8443'
|
|
|
|
https! true
|
|
|
|
|
|
|
|
get '/get'
|
|
|
|
assert_equal 8443, request.port
|
|
|
|
assert_equal true, request.ssl?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_https_and_port_via_process
|
|
|
|
with_test_route_set do
|
|
|
|
get 'https://www.example.com/get'
|
|
|
|
assert_equal 443, request.port
|
|
|
|
assert_equal true, request.ssl?
|
|
|
|
|
|
|
|
get 'https://www.example.com:8443/get'
|
|
|
|
assert_equal 8443, request.port
|
|
|
|
assert_equal true, request.ssl?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-16 01:11:00 -04:00
|
|
|
private
|
|
|
|
def with_test_route_set
|
|
|
|
with_routing do |set|
|
2010-02-24 19:01:03 -05:00
|
|
|
controller = ::IntegrationProcessTest::IntegrationController.clone
|
|
|
|
controller.class_eval do
|
2010-02-26 18:00:33 -05:00
|
|
|
include set.url_helpers
|
2010-02-24 19:01:03 -05:00
|
|
|
end
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2014-05-13 00:03:38 -04:00
|
|
|
get 'moved' => redirect('/method')
|
|
|
|
|
2013-07-16 08:27:22 -04:00
|
|
|
match ':action', :to => controller, :via => [:get, :post], :as => :action
|
|
|
|
get 'get/:action', :to => controller, :as => :get_action
|
2008-06-16 01:11:00 -04:00
|
|
|
end
|
2010-02-25 20:51:56 -05:00
|
|
|
|
2010-02-26 18:00:33 -05:00
|
|
|
self.singleton_class.send(:include, set.url_helpers)
|
2010-02-25 20:51:56 -05:00
|
|
|
|
2008-06-16 01:11:00 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2007-02-22 04:28:10 -05:00
|
|
|
end
|
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class MetalIntegrationTest < ActionDispatch::IntegrationTest
|
2010-02-26 18:00:33 -05:00
|
|
|
include SharedTestRoutes.url_helpers
|
2010-02-25 20:51:56 -05:00
|
|
|
|
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
|
2009-09-26 21:51:05 -04:00
|
|
|
@app = Poller
|
2008-12-17 09:21:20 -05:00
|
|
|
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
|
2009-09-25 12:10:39 -04:00
|
|
|
|
|
|
|
def test_generate_url_without_controller
|
|
|
|
assert_equal 'http://www.example.com/foo', url_for(:controller => "foo")
|
|
|
|
end
|
2013-03-13 07:25:32 -04:00
|
|
|
|
|
|
|
def test_pass_headers
|
|
|
|
get "/success", {}, "Referer" => "http://www.example.com/foo", "Host" => "http://nohost.com"
|
|
|
|
|
|
|
|
assert_equal "http://nohost.com", @request.env["HTTP_HOST"]
|
|
|
|
assert_equal "http://www.example.com/foo", @request.env["HTTP_REFERER"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pass_env
|
|
|
|
get "/success", {}, "HTTP_REFERER" => "http://test.com/", "HTTP_HOST" => "http://test.com"
|
|
|
|
|
|
|
|
assert_equal "http://test.com", @request.env["HTTP_HOST"]
|
|
|
|
assert_equal "http://test.com/", @request.env["HTTP_REFERER"]
|
|
|
|
end
|
|
|
|
|
2008-12-17 09:21:20 -05:00
|
|
|
end
|
2010-03-30 14:58:18 -04:00
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class ApplicationIntegrationTest < ActionDispatch::IntegrationTest
|
2010-03-30 14:58:18 -04:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
def index
|
|
|
|
render :text => "index"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
routes.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.routes
|
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
|
|
|
end
|
|
|
|
|
2014-07-16 18:41:47 -04:00
|
|
|
class MountedApp < Rails::Engine
|
2012-06-01 09:53:52 -04:00
|
|
|
def self.routes
|
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
routes.draw do
|
|
|
|
get 'baz', :to => 'application_integration_test/test#index', :as => :baz
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(*)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-30 14:58:18 -04:00
|
|
|
routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '', :to => 'application_integration_test/test#index', :as => :empty_string
|
2011-09-24 00:34:49 -04:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'foo', :to => 'application_integration_test/test#index', :as => :foo
|
|
|
|
get 'bar', :to => 'application_integration_test/test#index', :as => :bar
|
2012-06-01 09:53:52 -04:00
|
|
|
|
|
|
|
mount MountedApp => '/mounted', :as => "mounted"
|
2010-03-30 14:58:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
self.class
|
|
|
|
end
|
|
|
|
|
|
|
|
test "includes route helpers" do
|
2011-06-01 09:50:49 -04:00
|
|
|
assert_equal '/', empty_string_path
|
2010-03-30 14:58:18 -04:00
|
|
|
assert_equal '/foo', foo_path
|
|
|
|
assert_equal '/bar', bar_path
|
|
|
|
end
|
|
|
|
|
2012-06-01 09:53:52 -04:00
|
|
|
test "includes mounted helpers" do
|
|
|
|
assert_equal '/mounted/baz', mounted.baz_path
|
|
|
|
end
|
|
|
|
|
2010-03-30 14:58:18 -04:00
|
|
|
test "route helpers after controller access" do
|
2011-06-01 09:50:49 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal '/', empty_string_path
|
2011-09-24 00:34:49 -04:00
|
|
|
|
2010-03-30 14:58:18 -04:00
|
|
|
get '/foo'
|
|
|
|
assert_equal '/foo', foo_path
|
|
|
|
|
|
|
|
get '/bar'
|
|
|
|
assert_equal '/bar', bar_path
|
|
|
|
end
|
|
|
|
|
|
|
|
test "missing route helper before controller access" do
|
|
|
|
assert_raise(NameError) { missing_path }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "missing route helper after controller access" do
|
|
|
|
get '/foo'
|
|
|
|
assert_raise(NameError) { missing_path }
|
|
|
|
end
|
2011-04-06 15:37:55 -04:00
|
|
|
|
2011-09-24 00:34:49 -04:00
|
|
|
test "process do not modify the env passed as argument" do
|
2011-04-06 15:37:55 -04:00
|
|
|
env = { :SERVER_NAME => 'server', 'action_dispatch.custom' => 'custom' }
|
2011-09-24 00:34:49 -04:00
|
|
|
old_env = env.dup
|
2011-04-06 15:37:55 -04:00
|
|
|
get '/foo', nil, env
|
2011-09-24 00:34:49 -04:00
|
|
|
assert_equal old_env, env
|
2011-04-06 15:37:55 -04:00
|
|
|
end
|
2010-03-30 14:58:18 -04:00
|
|
|
end
|
2011-09-14 17:50:56 -04:00
|
|
|
|
|
|
|
class EnvironmentFilterIntegrationTest < ActionDispatch::IntegrationTest
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
def post
|
|
|
|
render :text => "Created", :status => 201
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
env["action_dispatch.parameter_filter"] = [:password]
|
|
|
|
routes.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.routes
|
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
routes.draw do
|
|
|
|
match '/post', :to => 'environment_filter_integration_test/test#post', :via => :post
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
self.class
|
|
|
|
end
|
|
|
|
|
|
|
|
test "filters rack request form vars" do
|
|
|
|
post "/post", :username => 'cjolly', :password => 'secret'
|
|
|
|
|
|
|
|
assert_equal 'cjolly', request.filtered_parameters['username']
|
|
|
|
assert_equal '[FILTERED]', request.filtered_parameters['password']
|
|
|
|
assert_equal '[FILTERED]', request.filtered_env['rack.request.form_vars']
|
|
|
|
end
|
|
|
|
end
|
2012-05-10 15:28:53 -04:00
|
|
|
|
|
|
|
class UrlOptionsIntegrationTest < ActionDispatch::IntegrationTest
|
|
|
|
class FooController < ActionController::Base
|
|
|
|
def index
|
|
|
|
render :text => "foo#index"
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render :text => "foo#show"
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
render :text => "foo#show"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class BarController < ActionController::Base
|
|
|
|
def default_url_options
|
|
|
|
{ :host => "bar.com" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
render :text => "foo#index"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.routes
|
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
routes.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
self.class
|
|
|
|
end
|
|
|
|
|
|
|
|
routes.draw do
|
|
|
|
default_url_options :host => "foo.com"
|
|
|
|
|
|
|
|
scope :module => "url_options_integration_test" do
|
|
|
|
get "/foo" => "foo#index", :as => :foos
|
|
|
|
get "/foo/:id" => "foo#show", :as => :foo
|
|
|
|
get "/foo/:id/edit" => "foo#edit", :as => :edit_foo
|
|
|
|
get "/bar" => "bar#index", :as => :bars
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "session uses default url options from routes" do
|
|
|
|
assert_equal "http://foo.com/foo", foos_url
|
|
|
|
end
|
|
|
|
|
|
|
|
test "current host overrides default url options from routes" do
|
|
|
|
get "/foo"
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "http://www.example.com/foo", foos_url
|
|
|
|
end
|
|
|
|
|
|
|
|
test "controller can override default url options from request" do
|
|
|
|
get "/bar"
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "http://bar.com/foo", foos_url
|
|
|
|
end
|
|
|
|
|
2013-01-21 08:49:30 -05:00
|
|
|
def test_can_override_default_url_options
|
|
|
|
original_host = default_url_options.dup
|
|
|
|
|
2012-05-10 15:28:53 -04:00
|
|
|
default_url_options[:host] = "foobar.com"
|
|
|
|
assert_equal "http://foobar.com/foo", foos_url
|
|
|
|
|
|
|
|
get "/bar"
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "http://foobar.com/foo", foos_url
|
2013-01-21 08:49:30 -05:00
|
|
|
ensure
|
|
|
|
ActionDispatch::Integration::Session.default_url_options = self.default_url_options = original_host
|
2012-05-10 15:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "current request path parameters are recalled" do
|
|
|
|
get "/foo/1"
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "/foo/1/edit", url_for(:action => 'edit', :only_path => true)
|
|
|
|
end
|
|
|
|
end
|
2014-05-15 10:08:53 -04:00
|
|
|
|
|
|
|
class HeadWithStatusActionIntegrationTest < ActionDispatch::IntegrationTest
|
|
|
|
class FooController < ActionController::Base
|
|
|
|
def status
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.routes
|
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
routes.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
self.class
|
|
|
|
end
|
|
|
|
|
|
|
|
routes.draw do
|
|
|
|
get "/foo/status" => 'head_with_status_action_integration_test/foo#status'
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get /foo/status with head result does not cause stack overflow error" do
|
|
|
|
assert_nothing_raised do
|
|
|
|
get '/foo/status'
|
|
|
|
end
|
|
|
|
assert_response :ok
|
|
|
|
end
|
|
|
|
end
|