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

Work around mock request hashing problem on 1.9 and jruby

This commit is contained in:
Jeremy Kemper 2009-02-06 12:47:01 -08:00
parent f7d509882e
commit 4b48f09a9a

View file

@ -59,7 +59,7 @@ module ActiveResource
# end # end
module_eval <<-EOE, __FILE__, __LINE__ module_eval <<-EOE, __FILE__, __LINE__
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}) def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
@responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers) @responses << [Request.new(:#{method}, path, nil, request_headers), Response.new(body || "", status, response_headers)]
end end
EOE EOE
end end
@ -91,21 +91,17 @@ module ActiveResource
@@requests ||= [] @@requests ||= []
end end
# Returns a hash of <tt>request => response</tt> pairs for all all responses this mock has delivered, where +request+ # Returns the list of requests and their mocked responses. Look up a
# is an instance of ActiveResource::Request and the response is, naturally, an instance of # response for a request using responses.assoc(request).
# ActiveResource::Response.
def responses def responses
@@responses ||= {} @@responses ||= []
end end
# Accepts a block which declares a set of requests and responses for the HttpMock to respond to. See the main # Accepts a block which declares a set of requests and responses for the HttpMock to respond to. See the main
# ActiveResource::HttpMock description for a more detailed explanation. # ActiveResource::HttpMock description for a more detailed explanation.
def respond_to(pairs = {}) #:yields: mock def respond_to(pairs = {}) #:yields: mock
reset! reset!
pairs.each do |(path, response)| responses.concat pairs.to_a
responses[path] = response
end
if block_given? if block_given?
yield Responder.new(responses) yield Responder.new(responses)
else else
@ -120,29 +116,23 @@ module ActiveResource
end end
end end
for method in [ :post, :put ] # body? methods
# def post(path, body, headers) { true => %w(post put),
# request = ActiveResource::Request.new(:post, path, body, headers) false => %w(get delete head) }.each do |has_body, methods|
# self.class.requests << request methods.each do |method|
# self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for #{request}")) # def post(path, body, headers)
# end # request = ActiveResource::Request.new(:post, path, body, headers)
module_eval <<-EOE, __FILE__, __LINE__ # self.class.requests << request
def #{method}(path, body, headers) # self.class.responses.assoc(request)[0] || raise(InvalidRequestError.new("No response recorded for #{request}"))
request = ActiveResource::Request.new(:#{method}, path, body, headers) # end
self.class.requests << request module_eval <<-EOE, __FILE__, __LINE__
self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}")) def #{method}(path, #{'body, ' if has_body}headers)
end request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers)
EOE self.class.requests << request
end self.class.responses.assoc(request).try(:second) || raise(InvalidRequestError.new("No response recorded for \#{request.inspect} in \#{self.class.responses.select { |req, res| req.path == request.path }.inspect}"))
end
for method in [ :get, :delete, :head ] EOE
module_eval <<-EOE, __FILE__, __LINE__ end
def #{method}(path, headers)
request = ActiveResource::Request.new(:#{method}, path, nil, headers)
self.class.requests << request
self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}"))
end
EOE
end end
def initialize(site) #:nodoc: def initialize(site) #:nodoc:
@ -157,21 +147,13 @@ module ActiveResource
@method, @path, @body, @headers = method, path, body, headers.merge(ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] => 'application/xml') @method, @path, @body, @headers = method, path, body, headers.merge(ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] => 'application/xml')
end end
def ==(other_request) def ==(req)
other_request.hash == hash path == req.path && method == req.method && headers == req.headers
end
def eql?(other_request)
self == other_request
end end
def to_s def to_s
"<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>" "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>"
end end
def hash
"#{path}#{method}#{headers}".hash
end
end end
class Response class Response