2009-05-06 03:14:55 -04:00
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2011-04-10 12:52:42 -04:00
|
|
|
require 'active_support/core_ext/object/inclusion'
|
2006-06-25 10:44:22 -04:00
|
|
|
|
|
|
|
module ActiveResource
|
2007-01-16 01:34:10 -05:00
|
|
|
class InvalidRequestError < StandardError; end #:nodoc:
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2008-05-25 07:29:00 -04:00
|
|
|
# One thing that has always been a pain with remote web services is testing. The HttpMock
|
|
|
|
# class makes it easy to test your Active Resource models by creating a set of mock responses to specific
|
|
|
|
# requests.
|
|
|
|
#
|
|
|
|
# To test your Active Resource model, you simply call the ActiveResource::HttpMock.respond_to
|
2011-05-01 14:46:01 -04:00
|
|
|
# method with an attached block. The block declares a set of URIs with expected input, and the output
|
|
|
|
# each request should return. The passed in block has any number of entries in the following generalized
|
2008-05-25 07:29:00 -04:00
|
|
|
# format:
|
|
|
|
#
|
|
|
|
# mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})
|
|
|
|
#
|
|
|
|
# * <tt>http_method</tt> - The HTTP method to listen for. This can be +get+, +post+, +put+, +delete+ or
|
|
|
|
# +head+.
|
|
|
|
# * <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
|
2011-05-17 19:50:34 -04:00
|
|
|
# hash format, such as <tt>{ "Content-Type" => "application/json" }</tt>. This mock will only trigger
|
2008-05-25 07:29:00 -04:00
|
|
|
# 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,
|
2011-05-17 19:50:34 -04:00
|
|
|
# such as Json.
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <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.
|
|
|
|
#
|
|
|
|
# In order for a mock to deliver its content, the incoming request must match by the <tt>http_method</tt>,
|
2011-05-01 14:46:01 -04:00
|
|
|
# +path+ and <tt>request_headers</tt>. If no match is found an +InvalidRequestError+ exception
|
2010-08-30 01:35:15 -04:00
|
|
|
# will be raised showing you what request it could not find a response for and also what requests and response
|
|
|
|
# pairs have been recorded so you can create a new mock for that request.
|
2008-05-25 07:29:00 -04:00
|
|
|
#
|
|
|
|
# ==== Example
|
|
|
|
# def setup
|
2011-05-17 19:50:34 -04:00
|
|
|
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
2008-05-25 07:29:00 -04:00
|
|
|
# ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
# 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
|
2008-05-25 07:29:00 -04:00
|
|
|
# end
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# def test_get_matz
|
|
|
|
# person = Person.find(1)
|
|
|
|
# assert_equal "Matz", person.name
|
|
|
|
# end
|
|
|
|
#
|
2006-06-25 10:44:22 -04:00
|
|
|
class HttpMock
|
2008-05-25 07:29:00 -04:00
|
|
|
class Responder #:nodoc:
|
2006-08-31 21:15:10 -04:00
|
|
|
def initialize(responses)
|
|
|
|
@responses = responses
|
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2011-05-24 00:08:48 -04:00
|
|
|
[ :post, :put, :get, :delete, :head ].each do |method|
|
2008-12-28 14:48:05 -05:00
|
|
|
# def post(path, request_headers = {}, body = nil, status = 200, response_headers = {})
|
|
|
|
# @responses[Request.new(:post, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
|
|
|
|
# end
|
2010-04-09 14:56:50 -04:00
|
|
|
module_eval <<-EOE, __FILE__, __LINE__ + 1
|
2006-09-29 12:25:49 -04:00
|
|
|
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
|
2011-01-18 03:21:16 -05:00
|
|
|
request = Request.new(:#{method}, path, nil, request_headers)
|
|
|
|
response = Response.new(body || "", status, response_headers)
|
|
|
|
|
|
|
|
delete_duplicate_responses(request)
|
|
|
|
|
|
|
|
@responses << [request, response]
|
2006-08-31 21:15:10 -04:00
|
|
|
end
|
|
|
|
EOE
|
|
|
|
end
|
2011-01-18 03:21:16 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def delete_duplicate_responses(request)
|
|
|
|
@responses.delete_if {|r| r[0] == request }
|
|
|
|
end
|
2006-08-31 21:15:10 -04:00
|
|
|
end
|
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
class << self
|
2008-05-25 07:29:00 -04:00
|
|
|
|
2011-05-01 14:46:01 -04:00
|
|
|
# Returns an array of all request objects that have been sent to the mock. You can use this to check
|
2008-07-16 08:00:36 -04:00
|
|
|
# if your model actually sent an HTTP request.
|
2008-05-25 07:29:00 -04:00
|
|
|
#
|
|
|
|
# ==== Example
|
|
|
|
# def setup
|
2011-05-17 19:50:34 -04:00
|
|
|
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
2008-05-25 07:29:00 -04:00
|
|
|
# ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
# mock.get "/people/1.json", {}, @matz
|
2008-05-25 07:29:00 -04:00
|
|
|
# end
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# def test_should_request_remote_service
|
|
|
|
# person = Person.find(1) # Call the remote service
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# # This request object has the same HTTP method and path as declared by the mock
|
2011-05-17 19:50:34 -04:00
|
|
|
# expected_request = ActiveResource::Request.new(:get, "/people/1.json")
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# # Assert that the mock received, and responded to, the expected request from the model
|
|
|
|
# assert ActiveResource::HttpMock.requests.include?(expected_request)
|
|
|
|
# end
|
2006-06-25 10:44:22 -04:00
|
|
|
def requests
|
|
|
|
@@requests ||= []
|
|
|
|
end
|
|
|
|
|
2009-02-06 15:47:01 -05:00
|
|
|
# Returns the list of requests and their mocked responses. Look up a
|
2011-05-01 14:46:01 -04:00
|
|
|
# response for a request using <tt>responses.assoc(request)</tt>.
|
2006-06-25 10:44:22 -04:00
|
|
|
def responses
|
2009-02-06 15:47:01 -05:00
|
|
|
@@responses ||= []
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
2010-08-30 01:35:15 -04:00
|
|
|
# Accepts a block which declares a set of requests and responses for the HttpMock to respond to in
|
|
|
|
# the following format:
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# === Example
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
2010-08-30 01:35:15 -04:00
|
|
|
# ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
# 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
|
2010-08-30 01:35:15 -04:00
|
|
|
# end
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate
|
|
|
|
# these the following format:
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# ActiveResource::Request.new(method, path, body, request_headers)
|
|
|
|
# ActiveResource::Response.new(body, status, response_headers)
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# === Example
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# Request.new(:#{method}, path, nil, request_headers)
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
2010-08-30 01:35:15 -04:00
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# 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)
|
2010-08-30 01:35:15 -04:00
|
|
|
# ok_response = ActiveResource::Response.new("", 200, {})
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# pairs = {create_matz => created_response, get_matz => ok_response}
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:15 -04:00
|
|
|
# ActiveResource::HttpMock.respond_to(pairs)
|
2010-08-30 01:35:36 -04:00
|
|
|
#
|
|
|
|
# Note, by default, every time you call +respond_to+, any previous request and response pairs stored
|
|
|
|
# in HttpMock will be deleted giving you a clean slate to work on.
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2011-07-24 06:21:42 -04:00
|
|
|
# If you want to override this behavior, pass in +false+ as the last argument to +respond_to+
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
# === Example
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
# ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
# mock.send(:get, "/people/1", {}, "JSON1")
|
2010-08-30 01:35:36 -04:00
|
|
|
# end
|
|
|
|
# ActiveResource::HttpMock.responses.length #=> 1
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
# ActiveResource::HttpMock.respond_to(false) do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
# mock.send(:get, "/people/2", {}, "JSON2")
|
2010-08-30 01:35:36 -04:00
|
|
|
# end
|
|
|
|
# ActiveResource::HttpMock.responses.length #=> 2
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
# This also works with passing in generated pairs of requests and responses, again, just pass in false
|
|
|
|
# as the last argument:
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
# === Example
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
# ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
# mock.send(:get, "/people/1", {}, "JSON1")
|
2010-08-30 01:35:36 -04:00
|
|
|
# end
|
|
|
|
# ActiveResource::HttpMock.responses.length #=> 1
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
|
2010-08-30 01:35:36 -04:00
|
|
|
# ok_response = ActiveResource::Response.new("", 200, {})
|
2010-10-06 18:14:12 -04:00
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
# pairs = {get_matz => ok_response}
|
|
|
|
#
|
|
|
|
# ActiveResource::HttpMock.respond_to(pairs, false)
|
|
|
|
# ActiveResource::HttpMock.responses.length #=> 2
|
2010-10-06 18:27:25 -04:00
|
|
|
#
|
|
|
|
# # If you add a response with an existing request, it will be replaced
|
|
|
|
#
|
|
|
|
# fail_response = ActiveResource::Response.new("", 404, {})
|
|
|
|
# pairs = {get_matz => fail_response}
|
|
|
|
#
|
|
|
|
# ActiveResource::HttpMock.respond_to(pairs, false)
|
|
|
|
# ActiveResource::HttpMock.responses.length #=> 2
|
|
|
|
#
|
2010-08-30 01:35:36 -04:00
|
|
|
def respond_to(*args) #:yields: mock
|
|
|
|
pairs = args.first || {}
|
|
|
|
reset! if args.last.class != FalseClass
|
2010-10-06 18:14:12 -04:00
|
|
|
|
2007-09-20 19:18:05 -04:00
|
|
|
if block_given?
|
2007-12-10 00:53:56 -05:00
|
|
|
yield Responder.new(responses)
|
2007-09-20 19:18:05 -04:00
|
|
|
else
|
2011-01-18 03:21:16 -05:00
|
|
|
delete_responses_to_replace pairs.to_a
|
|
|
|
responses.concat pairs.to_a
|
2007-09-20 19:18:05 -04:00
|
|
|
Responder.new(responses)
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
2010-10-06 18:14:12 -04:00
|
|
|
def delete_responses_to_replace(new_responses)
|
|
|
|
new_responses.each{|nr|
|
|
|
|
request_to_remove = nr[0]
|
|
|
|
@@responses = responses.delete_if{|r| r[0] == request_to_remove}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-05-25 07:29:00 -04:00
|
|
|
# Deletes all logged requests and responses.
|
2006-06-25 10:44:22 -04:00
|
|
|
def reset!
|
|
|
|
requests.clear
|
|
|
|
responses.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-06 15:47:01 -05:00
|
|
|
# body? methods
|
|
|
|
{ true => %w(post put),
|
|
|
|
false => %w(get delete head) }.each do |has_body, methods|
|
|
|
|
methods.each do |method|
|
|
|
|
# def post(path, body, headers)
|
|
|
|
# request = ActiveResource::Request.new(:post, path, body, headers)
|
|
|
|
# self.class.requests << request
|
2010-08-09 09:14:00 -04:00
|
|
|
# if response = self.class.responses.assoc(request)
|
|
|
|
# response[1]
|
|
|
|
# else
|
2010-08-24 22:05:23 -04:00
|
|
|
# raise InvalidRequestError.new("Could not find a response recorded for #{request.to_s} - Responses recorded are: - #{inspect_responses}")
|
2010-08-09 09:14:00 -04:00
|
|
|
# end
|
2009-02-06 15:47:01 -05:00
|
|
|
# end
|
2010-04-09 14:56:50 -04:00
|
|
|
module_eval <<-EOE, __FILE__, __LINE__ + 1
|
2009-02-06 15:47:01 -05:00
|
|
|
def #{method}(path, #{'body, ' if has_body}headers)
|
|
|
|
request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers)
|
|
|
|
self.class.requests << request
|
2009-05-06 03:14:55 -04:00
|
|
|
if response = self.class.responses.assoc(request)
|
|
|
|
response[1]
|
|
|
|
else
|
2010-08-24 22:05:23 -04:00
|
|
|
raise InvalidRequestError.new("Could not find a response recorded for \#{request.to_s} - Responses recorded are: \#{inspect_responses}")
|
2009-05-06 03:14:55 -04:00
|
|
|
end
|
2009-02-06 15:47:01 -05:00
|
|
|
end
|
|
|
|
EOE
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2008-05-25 07:29:00 -04:00
|
|
|
def initialize(site) #:nodoc:
|
2006-06-25 10:44:22 -04:00
|
|
|
@site = site
|
|
|
|
end
|
2010-08-24 22:05:23 -04:00
|
|
|
|
|
|
|
def inspect_responses #:nodoc:
|
|
|
|
self.class.responses.map { |r| r[0].to_s }.inspect
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Request
|
2006-09-29 12:25:49 -04:00
|
|
|
attr_accessor :path, :method, :body, :headers
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-10-02 10:20:18 -04:00
|
|
|
def initialize(method, path, body = nil, headers = {})
|
2010-06-22 18:50:15 -04:00
|
|
|
@method, @path, @body, @headers = method, path, body, headers
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
2009-02-06 15:47:01 -05:00
|
|
|
def ==(req)
|
2010-06-22 18:50:15 -04:00
|
|
|
path == req.path && method == req.method && headers_match?(req)
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
def to_s
|
2006-09-29 12:25:49 -04:00
|
|
|
"<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>"
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2010-06-22 18:50:15 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def headers_match?(req)
|
|
|
|
# Ignore format header on equality if it's not defined
|
|
|
|
format_header = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method]
|
|
|
|
if headers[format_header].present? || req.headers[format_header].blank?
|
|
|
|
headers == req.headers
|
|
|
|
else
|
|
|
|
headers.dup.merge(format_header => req.headers[format_header]) == req.headers
|
|
|
|
end
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
class Response
|
2006-12-05 14:12:51 -05:00
|
|
|
attr_accessor :body, :message, :code, :headers
|
|
|
|
|
|
|
|
def initialize(body, message = 200, headers = {})
|
|
|
|
@body, @message, @headers = body, message.to_s, headers
|
|
|
|
@code = @message[0,3].to_i
|
2007-12-10 00:53:56 -05:00
|
|
|
|
|
|
|
resp_cls = Net::HTTPResponse::CODE_TO_OBJ[@code.to_s]
|
|
|
|
if resp_cls && !resp_cls.body_permitted?
|
|
|
|
@body = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if @body.nil?
|
|
|
|
self['Content-Length'] = "0"
|
|
|
|
else
|
|
|
|
self['Content-Length'] = body.size.to_s
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2011-05-01 14:59:27 -04:00
|
|
|
# Returns true if code is 2xx,
|
|
|
|
# false otherwise.
|
2006-06-25 10:44:22 -04:00
|
|
|
def success?
|
2011-04-10 12:52:42 -04:00
|
|
|
code.in?(200..299)
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 21:15:10 -04:00
|
|
|
|
|
|
|
def [](key)
|
|
|
|
headers[key]
|
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-08-31 21:15:10 -04:00
|
|
|
def []=(key, value)
|
|
|
|
headers[key] = value
|
|
|
|
end
|
2007-12-10 00:53:56 -05:00
|
|
|
|
2011-05-01 14:59:27 -04:00
|
|
|
# Returns true if the other is a Response with an equal body, equal message
|
|
|
|
# and equal headers. Otherwise it returns false.
|
2007-04-25 21:53:01 -04:00
|
|
|
def ==(other)
|
|
|
|
if (other.is_a?(Response))
|
|
|
|
other.body == body && other.message == message && other.headers == headers
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Connection
|
|
|
|
private
|
2006-09-04 06:04:23 -04:00
|
|
|
silence_warnings do
|
|
|
|
def http
|
|
|
|
@http ||= HttpMock.new(@site)
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
end
|
2006-09-04 06:04:23 -04:00
|
|
|
end
|