1 require 'active_resource/connection'
4 class InvalidRequestError < StandardError; end #:nodoc:
8 def initialize(responses)
12 for method in [ :post, :put, :get, :delete ]
14 def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
15 @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
30 def respond_to(pairs = {})
32 pairs.each do |(path, response)|
33 responses[path] = response
37 yield Responder.new(responses)
39 Responder.new(responses)
49 for method in [ :post, :put ]
51 def #{method}(path, body, headers)
52 request = ActiveResource::Request.new(:#{method}, path, body, headers)
53 self.class.requests << request
54 self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for: \#{request.inspect}"))
59 for method in [ :get, :delete ]
61 def #{method}(path, headers)
62 request = ActiveResource::Request.new(:#{method}, path, nil, headers)
63 self.class.requests << request
64 self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for: \#{request.inspect}"))
75 attr_accessor :path, :method, :body, :headers
77 def initialize(method, path, body = nil, headers = {})
78 @method, @path, @body, @headers = method, path, body, headers.dup
79 @headers.update('Content-Type' => 'application/xml')
83 other_request.hash == hash
86 def eql?(other_request)
91 "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>"
95 "#{path}#{method}#{headers}".hash
100 attr_accessor :body, :message, :code, :headers
102 def initialize(body, message = 200, headers = {})
103 @body, @message, @headers = body, message.to_s, headers
104 @code = @message[0,3].to_i
108 (200..299).include?(code)
120 if (other.is_a?(Response))
121 other.body == body && other.message == message && other.headers == headers
132 @http ||= HttpMock.new(@site)