1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

corrected some further tests and docs for the ARes change to json change

This commit is contained in:
Josh Kalderimis 2011-05-17 19:50:34 -04:00
parent b641ab5bae
commit 00958696d8
7 changed files with 108 additions and 108 deletions

View file

@ -81,19 +81,19 @@ module ActiveResource
# <tt>post</tt>, <tt>put</tt> and <tt>\delete</tt> methods where you can specify a custom REST method
# name to invoke.
#
# # POST to the custom 'register' REST method, i.e. POST /people/new/register.xml.
# # POST to the custom 'register' REST method, i.e. POST /people/new/register.json.
# Person.new(:name => 'Ryan').post(:register)
# # => { :id => 1, :name => 'Ryan', :position => 'Clerk' }
#
# # PUT an update by invoking the 'promote' REST method, i.e. PUT /people/1/promote.xml?position=Manager.
# # PUT an update by invoking the 'promote' REST method, i.e. PUT /people/1/promote.json?position=Manager.
# Person.find(1).put(:promote, :position => 'Manager')
# # => { :id => 1, :name => 'Ryan', :position => 'Manager' }
#
# # GET all the positions available, i.e. GET /people/positions.xml.
# # GET all the positions available, i.e. GET /people/positions.json.
# Person.get(:positions)
# # => [{:name => 'Manager'}, {:name => 'Clerk'}]
#
# # DELETE to 'fire' a person, i.e. DELETE /people/1/fire.xml.
# # DELETE to 'fire' a person, i.e. DELETE /people/1/fire.json.
# Person.find(1).delete(:fire)
#
# For more information on using custom REST methods, see the
@ -164,7 +164,7 @@ module ActiveResource
# response code will be returned from the server which will raise an ActiveResource::ResourceNotFound
# exception.
#
# # GET http://api.people.com:3000/people/999.xml
# # GET http://api.people.com:3000/people/999.json
# ryan = Person.find(999) # 404, raises ActiveResource::ResourceNotFound
#
#
@ -218,7 +218,7 @@ module ActiveResource
# ryan.save # => false
#
# # When
# # PUT http://api.people.com:3000/people/1.xml
# # PUT http://api.people.com:3000/people/1.json
# # or
# # PUT http://api.people.com:3000/people/1.json
# # is requested with invalid values, the response is:
@ -489,7 +489,7 @@ module ActiveResource
# Person.format = ActiveResource::Formats::XmlFormat
# Person.find(1) # => GET /people/1.xml
#
# Default format is <tt>:xml</tt>.
# Default format is <tt>:json</tt>.
def format=(mime_type_reference_or_format)
format = mime_type_reference_or_format.is_a?(Symbol) ?
ActiveResource::Formats[mime_type_reference_or_format] : mime_type_reference_or_format
@ -570,7 +570,7 @@ module ActiveResource
attr_accessor_with_default(:primary_key, 'id') #:nodoc:
# Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.xml</tt>)
# Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>)
# This method is regenerated at runtime based on what the \prefix is set to.
def prefix(options={})
default = site.path
@ -587,7 +587,7 @@ module ActiveResource
prefix_source
end
# Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.xml</tt>).
# Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>).
# Default value is <tt>site.path</tt>.
def prefix=(value = '/')
# Replace :placeholders with '#{embedded options[:lookups]}'
@ -618,21 +618,21 @@ module ActiveResource
#
# ==== Options
# +prefix_options+ - A \hash to add a \prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
# would yield a URL like <tt>/accounts/19/purchases.xml</tt>).
# would yield a URL like <tt>/accounts/19/purchases.json</tt>).
# +query_options+ - A \hash to add items to the query string for the request.
#
# ==== Examples
# Post.element_path(1)
# # => /posts/1.xml
# # => /posts/1.json
#
# Comment.element_path(1, :post_id => 5)
# # => /posts/5/comments/1.xml
# # => /posts/5/comments/1.json
#
# Comment.element_path(1, :post_id => 5, :active => 1)
# # => /posts/5/comments/1.xml?active=1
# # => /posts/5/comments/1.json?active=1
#
# Comment.element_path(1, {:post_id => 5}, {:active => 1})
# # => /posts/5/comments/1.xml?active=1
# # => /posts/5/comments/1.json?active=1
#
def element_path(id, prefix_options = {}, query_options = nil)
check_prefix_options(prefix_options)
@ -645,14 +645,14 @@ module ActiveResource
#
# ==== Options
# * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
# would yield a URL like <tt>/accounts/19/purchases/new.xml</tt>).
# would yield a URL like <tt>/accounts/19/purchases/new.json</tt>).
#
# ==== Examples
# Post.new_element_path
# # => /posts/new.xml
# # => /posts/new.json
#
# Comment.collection_path(:post_id => 5)
# # => /posts/5/comments/new.xml
# # => /posts/5/comments/new.json
def new_element_path(prefix_options = {})
"#{prefix(prefix_options)}#{collection_name}/new.#{format.extension}"
end
@ -662,21 +662,21 @@ module ActiveResource
#
# ==== Options
# * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
# would yield a URL like <tt>/accounts/19/purchases.xml</tt>).
# would yield a URL like <tt>/accounts/19/purchases.json</tt>).
# * +query_options+ - A hash to add items to the query string for the request.
#
# ==== Examples
# Post.collection_path
# # => /posts.xml
# # => /posts.json
#
# Comment.collection_path(:post_id => 5)
# # => /posts/5/comments.xml
# # => /posts/5/comments.json
#
# Comment.collection_path(:post_id => 5, :active => 1)
# # => /posts/5/comments.xml?active=1
# # => /posts/5/comments.json?active=1
#
# Comment.collection_path({:post_id => 5}, {:active => 1})
# # => /posts/5/comments.xml?active=1
# # => /posts/5/comments.json?active=1
#
def collection_path(prefix_options = {}, query_options = nil)
check_prefix_options(prefix_options)
@ -745,34 +745,34 @@ module ActiveResource
#
# ==== Examples
# Person.find(1)
# # => GET /people/1.xml
# # => GET /people/1.json
#
# Person.find(:all)
# # => GET /people.xml
# # => GET /people.json
#
# Person.find(:all, :params => { :title => "CEO" })
# # => GET /people.xml?title=CEO
# # => GET /people.json?title=CEO
#
# Person.find(:first, :from => :managers)
# # => GET /people/managers.xml
# # => GET /people/managers.json
#
# Person.find(:last, :from => :managers)
# # => GET /people/managers.xml
# # => GET /people/managers.json
#
# Person.find(:all, :from => "/companies/1/people.xml")
# # => GET /companies/1/people.xml
# Person.find(:all, :from => "/companies/1/people.json")
# # => GET /companies/1/people.json
#
# Person.find(:one, :from => :leader)
# # => GET /people/leader.xml
# # => GET /people/leader.json
#
# Person.find(:all, :from => :developers, :params => { :language => 'ruby' })
# # => GET /people/developers.xml?language=ruby
# # => GET /people/developers.json?language=ruby
#
# Person.find(:one, :from => "/companies/1/manager.xml")
# # => GET /companies/1/manager.xml
# Person.find(:one, :from => "/companies/1/manager.json")
# # => GET /companies/1/manager.json
#
# StreetAddress.find(1, :params => { :person_id => 1 })
# # => GET /people/1/street_addresses/1.xml
# # => GET /people/1/street_addresses/1.json
#
# == Failure or missing data
# A failure to find the requested object raises a ResourceNotFound
@ -833,7 +833,7 @@ module ActiveResource
# my_event = Event.find(:first) # let's assume this is event with ID 7
# Event.delete(my_event.id) # sends DELETE /events/7
#
# # Let's assume a request to events/5/cancel.xml
# # Let's assume a request to events/5/cancel.json
# Event.delete(params[:id]) # sends DELETE /events/5
def delete(id, options = {})
connection.delete(element_path(id, options))
@ -1121,7 +1121,7 @@ module ActiveResource
# Saves (+POST+) or \updates (+PUT+) a resource. Delegates to +create+ if the object is \new,
# +update+ if it exists. If the response to the \save includes a body, it will be assumed that this body
# is XML for the final object as it looked after the \save (which would include attributes like +created_at+
# is Json for the final object as it looked after the \save (which would include attributes like +created_at+
# that weren't part of the original submit).
#
# ==== Examples

View file

@ -11,10 +11,10 @@ module ActiveResource
#
# This route set creates routes for the following HTTP requests:
#
# POST /people/new/register.xml # PeopleController.register
# PUT /people/1/promote.xml # PeopleController.promote with :id => 1
# DELETE /people/1/deactivate.xml # PeopleController.deactivate with :id => 1
# GET /people/active.xml # PeopleController.active
# POST /people/new/register.json # PeopleController.register
# PUT /people/1/promote.json # PeopleController.promote with :id => 1
# DELETE /people/1/deactivate.json # PeopleController.deactivate with :id => 1
# GET /people/active.json # PeopleController.active
#
# Using this module, Active Resource can use these custom REST methods just like the
# standard methods.
@ -23,13 +23,13 @@ module ActiveResource
# self.site = "http://37s.sunrise.i:3000"
# end
#
# Person.new(:name => 'Ryan).post(:register) # POST /people/new/register.xml
# Person.new(:name => 'Ryan).post(:register) # POST /people/new/register.json
# # => { :id => 1, :name => 'Ryan' }
#
# Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.xml
# Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.xml
# Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.json
# Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.json
#
# Person.get(:active) # GET /people/active.xml
# Person.get(:active) # GET /people/active.json
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
#
module CustomMethods
@ -41,10 +41,10 @@ module ActiveResource
# Invokes a GET to a given custom REST method. For example:
#
# Person.get(:active) # GET /people/active.xml
# Person.get(:active) # GET /people/active.json
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
#
# Person.get(:active, :awesome => true) # GET /people/active.xml?awesome=true
# Person.get(:active, :awesome => true) # GET /people/active.json?awesome=true
# # => [{:id => 1, :name => 'Ryan'}]
#
# Note: the objects returned from this method are not automatically converted

View file

@ -20,10 +20,10 @@ module ActiveResource
# * <tt>path</tt> - A string, starting with a "/", defining the URI that is expected to be
# called.
# * <tt>request_headers</tt> - Headers that are expected along with the request. This argument uses a
# hash format, such as <tt>{ "Content-Type" => "application/xml" }</tt>. This mock will only trigger
# hash format, such as <tt>{ "Content-Type" => "application/json" }</tt>. This mock will only trigger
# if your tests sends a request with identical headers.
# * <tt>body</tt> - The data to be returned. This should be a string of Active Resource parseable content,
# such as XML.
# such as Json.
# * <tt>status</tt> - The HTTP response code, as an integer, to return with the response.
# * <tt>response_headers</tt> - Headers to be returned with the response. Uses the same hash format as
# <tt>request_headers</tt> listed above.
@ -35,12 +35,12 @@ module ActiveResource
#
# ==== Example
# def setup
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
# ActiveResource::HttpMock.respond_to do |mock|
# mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml"
# mock.get "/people/1.xml", {}, @matz
# mock.put "/people/1.xml", {}, nil, 204
# mock.delete "/people/1.xml", {}, nil, 200
# mock.post "/people.json", {}, @matz, 201, "Location" => "/people/1.json"
# mock.get "/people/1.json", {}, @matz
# mock.put "/people/1.json", {}, nil, 204
# mock.delete "/people/1.json", {}, nil, 200
# end
# end
#
@ -85,9 +85,9 @@ module ActiveResource
#
# ==== Example
# def setup
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
# ActiveResource::HttpMock.respond_to do |mock|
# mock.get "/people/1.xml", {}, @matz
# mock.get "/people/1.json", {}, @matz
# end
# end
#
@ -95,7 +95,7 @@ module ActiveResource
# person = Person.find(1) # Call the remote service
#
# # This request object has the same HTTP method and path as declared by the mock
# expected_request = ActiveResource::Request.new(:get, "/people/1.xml")
# expected_request = ActiveResource::Request.new(:get, "/people/1.json")
#
# # Assert that the mock received, and responded to, the expected request from the model
# assert ActiveResource::HttpMock.requests.include?(expected_request)
@ -117,12 +117,12 @@ module ActiveResource
#
# === Example
#
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
# ActiveResource::HttpMock.respond_to do |mock|
# mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml"
# mock.get "/people/1.xml", {}, @matz
# mock.put "/people/1.xml", {}, nil, 204
# mock.delete "/people/1.xml", {}, nil, 200
# mock.post "/people.json", {}, @matz, 201, "Location" => "/people/1.json"
# mock.get "/people/1.json", {}, @matz
# mock.put "/people/1.json", {}, nil, 204
# mock.delete "/people/1.json", {}, nil, 200
# end
#
# Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate
@ -135,11 +135,11 @@ module ActiveResource
#
# Request.new(:#{method}, path, nil, request_headers)
#
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
#
# create_matz = ActiveResource::Request.new(:post, '/people.xml', @matz, {})
# created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
# get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
# create_matz = ActiveResource::Request.new(:post, '/people.json', @matz, {})
# created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.json"})
# get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
# ok_response = ActiveResource::Response.new("", 200, {})
#
# pairs = {create_matz => created_response, get_matz => ok_response}
@ -154,12 +154,12 @@ module ActiveResource
# === Example
#
# ActiveResource::HttpMock.respond_to do |mock|
# mock.send(:get, "/people/1", {}, "XML1")
# mock.send(:get, "/people/1", {}, "JSON1")
# end
# ActiveResource::HttpMock.responses.length #=> 1
#
# ActiveResource::HttpMock.respond_to(false) do |mock|
# mock.send(:get, "/people/2", {}, "XML2")
# mock.send(:get, "/people/2", {}, "JSON2")
# end
# ActiveResource::HttpMock.responses.length #=> 2
#
@ -169,11 +169,11 @@ module ActiveResource
# === Example
#
# ActiveResource::HttpMock.respond_to do |mock|
# mock.send(:get, "/people/1", {}, "XML1")
# mock.send(:get, "/people/1", {}, "JSON1")
# end
# ActiveResource::HttpMock.responses.length #=> 1
#
# get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
# get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
# ok_response = ActiveResource::Response.new("", 200, {})
#
# pairs = {get_matz => ok_response}

View file

@ -48,7 +48,7 @@ class AuthorizationTest < Test::Unit::TestCase
end
def test_authorization_header
authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization']
authorization = authorization_header["Authorization"].to_s.split
@ -58,7 +58,7 @@ class AuthorizationTest < Test::Unit::TestCase
def test_authorization_header_with_username_but_no_password
@conn = ActiveResource::Connection.new("http://david:@localhost")
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
authorization = authorization_header["Authorization"].to_s.split
assert_equal "Basic", authorization[0]
@ -67,7 +67,7 @@ class AuthorizationTest < Test::Unit::TestCase
def test_authorization_header_with_password_but_no_username
@conn = ActiveResource::Connection.new("http://:test123@localhost")
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
authorization = authorization_header["Authorization"].to_s.split
assert_equal "Basic", authorization[0]
@ -76,7 +76,7 @@ class AuthorizationTest < Test::Unit::TestCase
def test_authorization_header_with_decoded_credentials_from_url
@conn = ActiveResource::Connection.new("http://my%40email.com:%31%32%33@localhost")
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
authorization = authorization_header["Authorization"].to_s.split
assert_equal "Basic", authorization[0]
@ -87,7 +87,7 @@ class AuthorizationTest < Test::Unit::TestCase
@authenticated_conn = ActiveResource::Connection.new("http://@localhost")
@authenticated_conn.user = 'david'
@authenticated_conn.password = 'test123'
authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization']
authorization = authorization_header["Authorization"].to_s.split
@ -98,7 +98,7 @@ class AuthorizationTest < Test::Unit::TestCase
def test_authorization_header_explicitly_setting_username_but_no_password
@conn = ActiveResource::Connection.new("http://@localhost")
@conn.user = "david"
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
authorization = authorization_header["Authorization"].to_s.split
assert_equal "Basic", authorization[0]
@ -108,7 +108,7 @@ class AuthorizationTest < Test::Unit::TestCase
def test_authorization_header_explicitly_setting_password_but_no_username
@conn = ActiveResource::Connection.new("http://@localhost")
@conn.password = "test123"
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
authorization = authorization_header["Authorization"].to_s.split
assert_equal "Basic", authorization[0]
@ -117,7 +117,7 @@ class AuthorizationTest < Test::Unit::TestCase
def test_authorization_header_if_credentials_supplied_and_auth_type_is_basic
@authenticated_conn.auth_type = :basic
authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json'))
assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization']
authorization = authorization_header["Authorization"].to_s.split

View file

@ -14,7 +14,7 @@ class ConnectionTest < Test::Unit::TestCase
@david = david.to_json
@header = { 'key' => 'value' }.freeze
@default_request_headers = { 'Content-Type' => 'application/xml' }
@default_request_headers = { 'Content-Type' => 'application/json' }
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/2.json", @header, @david
mock.get "/people.json", {}, @people

View file

@ -108,9 +108,9 @@ class FinderTest < Test::Unit::TestCase
end
def test_find_all_by_from_with_options
ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david }
ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.json", {}, @people_david }
people = Person.find(:all, :from => "/companies/1/people.xml")
people = Person.find(:all, :from => "/companies/1/people.json")
assert_equal 1, people.size
assert_equal "David", people.first.name
end

View file

@ -11,10 +11,10 @@ class HttpMockTest < ActiveSupport::TestCase
[:post, :put, :get, :delete, :head].each do |method|
test "responds to simple #{method} request" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "Response")
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Response")
end
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
end
test "adds format header by default to #{method} request" do
@ -22,7 +22,7 @@ class HttpMockTest < ActiveSupport::TestCase
mock.send(method, "/people/1", {}, "Response")
end
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
end
test "respond only when headers match header by default to #{method} request" do
@ -53,8 +53,8 @@ class HttpMockTest < ActiveSupport::TestCase
test "responds correctly when format header is given to #{method} request" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML")
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Json")
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/xml" }, "XML")
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Json")
end
assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
@ -63,22 +63,22 @@ class HttpMockTest < ActiveSupport::TestCase
test "raises InvalidRequestError if no response found for the #{method} request" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML")
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "json")
end
assert_raise(::ActiveResource::InvalidRequestError) do
request(method, "/people/1", FORMAT_HEADER[method] => "application/json")
request(method, "/people/1", FORMAT_HEADER[method] => "application/xml")
end
end
end
test "allows you to send in pairs directly to the respond_to method" do
matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
create_matz = ActiveResource::Request.new(:post, '/people.xml', matz, {})
created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
create_matz = ActiveResource::Request.new(:post, '/people.json', matz, {})
created_response = ActiveResource::Response.new("", 201, { "Location" => "/people/1.json" })
get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
ok_response = ActiveResource::Response.new(matz, 200, {})
pairs = {create_matz => created_response, get_matz => ok_response}
@ -91,24 +91,24 @@ class HttpMockTest < ActiveSupport::TestCase
test "resets all mocked responses on each call to respond_to with a block by default" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/2", {}, "XML2")
mock.send(:get, "/people/2", {}, "JSON2")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
end
test "resets all mocked responses on each call to respond_to by passing pairs by default" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
ok_response = ActiveResource::Response.new(matz, 200, {})
ActiveResource::HttpMock.respond_to({get_matz => ok_response})
@ -117,24 +117,24 @@ class HttpMockTest < ActiveSupport::TestCase
test "allows you to add new responses to the existing responses by calling a block" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
ActiveResource::HttpMock.respond_to(false) do |mock|
mock.send(:get, "/people/2", {}, "XML2")
mock.send(:get, "/people/2", {}, "JSON2")
end
assert_equal 2, ActiveResource::HttpMock.responses.length
end
test "allows you to add new responses to the existing responses by passing pairs" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
ok_response = ActiveResource::Response.new(matz, 200, {})
ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
@ -143,23 +143,23 @@ class HttpMockTest < ActiveSupport::TestCase
test "allows you to replace the existing reponse with the same request by calling a block" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
ActiveResource::HttpMock.respond_to(false) do |mock|
mock.send(:get, "/people/1", {}, "XML2")
mock.send(:get, "/people/1", {}, "JSON2")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
end
test "allows you to replace the existing reponse with the same request by passing pairs" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
get_matz = ActiveResource::Request.new(:get, '/people/1', nil)
ok_response = ActiveResource::Response.new(matz, 200, {})
@ -169,19 +169,19 @@ class HttpMockTest < ActiveSupport::TestCase
test "do not replace the response with the same path but different method by calling a block" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
ActiveResource::HttpMock.respond_to(false) do |mock|
mock.send(:put, "/people/1", {}, "XML2")
mock.send(:put, "/people/1", {}, "JSON2")
end
assert_equal 2, ActiveResource::HttpMock.responses.length
end
test "do not replace the response with the same path but different method by passing pairs" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
mock.send(:get, "/people/1", {}, "JSON1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length