2006-06-25 10:44:22 -04:00
|
|
|
require 'active_resource/connection'
|
|
|
|
|
|
|
|
module ActiveResource
|
2007-01-16 01:34:10 -05:00
|
|
|
class InvalidRequestError < StandardError; end #:nodoc:
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
class HttpMock
|
2006-08-31 21:15:10 -04:00
|
|
|
class Responder
|
|
|
|
def initialize(responses)
|
|
|
|
@responses = responses
|
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2008-02-09 17:21:55 -05:00
|
|
|
for method in [ :post, :put, :get, :delete, :head ]
|
2007-12-21 06:22:03 -05:00
|
|
|
module_eval <<-EOE, __FILE__, __LINE__
|
2006-09-29 12:25:49 -04:00
|
|
|
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
|
2007-04-19 18:18:03 -04:00
|
|
|
@responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
|
2006-08-31 21:15:10 -04:00
|
|
|
end
|
|
|
|
EOE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
class << self
|
|
|
|
def requests
|
|
|
|
@@requests ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def responses
|
|
|
|
@@responses ||= {}
|
|
|
|
end
|
|
|
|
|
2006-08-31 21:15:10 -04:00
|
|
|
def respond_to(pairs = {})
|
2006-06-25 10:44:22 -04:00
|
|
|
reset!
|
|
|
|
pairs.each do |(path, response)|
|
|
|
|
responses[path] = response
|
|
|
|
end
|
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
|
|
|
|
Responder.new(responses)
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset!
|
|
|
|
requests.clear
|
|
|
|
responses.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-09-29 12:25:49 -04:00
|
|
|
for method in [ :post, :put ]
|
2007-12-21 06:22:03 -05:00
|
|
|
module_eval <<-EOE, __FILE__, __LINE__
|
2006-09-29 12:25:49 -04:00
|
|
|
def #{method}(path, body, headers)
|
|
|
|
request = ActiveResource::Request.new(:#{method}, path, body, headers)
|
|
|
|
self.class.requests << request
|
2007-12-21 06:22:03 -05:00
|
|
|
self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}"))
|
2006-09-29 12:25:49 -04:00
|
|
|
end
|
|
|
|
EOE
|
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2008-02-09 17:21:55 -05:00
|
|
|
for method in [ :get, :delete, :head ]
|
2007-12-21 06:22:03 -05:00
|
|
|
module_eval <<-EOE, __FILE__, __LINE__
|
2006-09-29 12:25:49 -04:00
|
|
|
def #{method}(path, headers)
|
|
|
|
request = ActiveResource::Request.new(:#{method}, path, nil, headers)
|
2006-06-25 10:44:22 -04:00
|
|
|
self.class.requests << request
|
2007-12-21 06:22:03 -05:00
|
|
|
self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}"))
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
EOE
|
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
def initialize(site)
|
|
|
|
@site = site
|
|
|
|
end
|
|
|
|
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 = {})
|
2007-12-21 06:22:03 -05:00
|
|
|
@method, @path, @body, @headers = method, path, body, headers.reverse_merge('Content-Type' => 'application/xml')
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other_request)
|
|
|
|
other_request.hash == hash
|
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
def eql?(other_request)
|
|
|
|
self == other_request
|
|
|
|
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
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
def hash
|
2006-09-29 12:25:49 -04:00
|
|
|
"#{path}#{method}#{headers}".hash
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
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
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
def success?
|
|
|
|
(200..299).include?(code)
|
|
|
|
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
|
|
|
|
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
|