Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a helper to make OPTIONS calls in controller tests #1277

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions actionpack/lib/action_controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ def head(action, parameters = nil, session = nil, flash = nil)
process(action, "HEAD", parameters, session, flash)
end

# Executes a request simulating OPTIONS HTTP method and set/volley the response
def options(action, parameters = nil, session = nil, flash = nil)
process(action, parameters, session, flash, "OPTIONS")
end

def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
@request.env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
Expand Down
12 changes: 12 additions & 0 deletions actionpack/lib/action_dispatch/testing/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def delete(path, parameters = nil, headers = nil)
process :delete, path, parameters, headers
end

# Performs a OPTIONS request with the given parameters. See +#get+ for
# more details.
def options(path, parameters = nil, headers = nil)
process :options, path, parameters, headers
end

# Performs a HEAD request with the given parameters. See +#get+ for more
# details.
def head(path, parameters = nil, headers = nil)
Expand Down Expand Up @@ -131,6 +137,12 @@ def put_via_redirect(path, parameters = nil, headers = nil)
def delete_via_redirect(path, parameters = nil, headers = nil)
request_via_redirect(:delete, path, parameters, headers)
end

# Performs a OPTIONS request, following any subsequent redirect.
# See +request_via_redirect+ for more information.
def options_via_redirect(path, parameters = nil, headers = nil)
request_via_redirect(:options, path, parameters, headers)
end
end

# An instance of this class represents a set of requests and responses
Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/controller/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def test_delete_via_redirect
@session.delete_via_redirect(path, args, headers)
end

def test_options_via_redirect
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
@session.expects(:request_via_redirect).with(:options, path, args, headers)
@session.options_via_redirect(path, args, headers)
end

def test_get
path = "/index"; params = "blah"; headers = {:location => 'blah'}
@session.expects(:process).with(:get,path,params,headers)
Expand Down Expand Up @@ -111,6 +117,12 @@ def test_delete
@session.delete(path,params,headers)
end

def test_options
path = "/index"; params = "blah"; headers = {:location => 'blah'}
@session.expects(:process).with(:options, path, params, headers)
@session.options(path, params, headers)
end

def test_head
path = "/index"; params = "blah"; headers = {:location => 'blah'}
@session.expects(:process).with(:head,path,params,headers)
Expand Down Expand Up @@ -173,6 +185,16 @@ def test_xml_http_request_delete
@session.xml_http_request(:delete,path,params,headers)
end

def test_xml_http_request_options
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(:options, path, params, headers_after_xhr)
@session.xml_http_request(:options, path, params, headers)
end

def test_xml_http_request_head
path = "/index"; params = "blah"; headers = {:location => 'blah'}
headers_after_xhr = headers.merge(
Expand Down