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
|
|
|
|
2006-08-31 21:15:10 -04:00
|
|
|
for method in [ :post, :put, :get, :delete ]
|
|
|
|
module_eval <<-EOE
|
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?
|
|
|
|
yield Responder.new(responses)
|
|
|
|
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 ]
|
|
|
|
module_eval <<-EOE
|
|
|
|
def #{method}(path, body, headers)
|
|
|
|
request = ActiveResource::Request.new(:#{method}, path, body, headers)
|
|
|
|
self.class.requests << request
|
2006-12-21 17:18:58 -05:00
|
|
|
self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for: \#{request.inspect}"))
|
2006-09-29 12:25:49 -04:00
|
|
|
end
|
|
|
|
EOE
|
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2006-09-29 12:25:49 -04:00
|
|
|
for method in [ :get, :delete ]
|
2006-06-25 10:44:22 -04:00
|
|
|
module_eval <<-EOE
|
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
|
2006-12-21 17:18:58 -05:00
|
|
|
self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for: \#{request.inspect}"))
|
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-10-02 01:32:14 -04:00
|
|
|
@method, @path, @body, @headers = method, path, body, headers.dup
|
2006-09-29 12:25:49 -04:00
|
|
|
@headers.update('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
|
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-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
|