Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activeresource / lib / active_resource / http_mock.rb
blob37fc1f2bfefa0cf14cff212a70418e186feb3fb3
1 require 'active_resource/connection'
3 module ActiveResource
4   class InvalidRequestError < StandardError; end #:nodoc:
6   class HttpMock
7     class Responder
8       def initialize(responses)
9         @responses = responses
10       end
12       for method in [ :post, :put, :get, :delete ]
13         module_eval <<-EOE
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)
16           end
17         EOE
18       end
19     end
21     class << self
22       def requests
23         @@requests ||= []
24       end
26       def responses
27         @@responses ||= {}
28       end
30       def respond_to(pairs = {})
31         reset!
32         pairs.each do |(path, response)|
33           responses[path] = response
34         end
36         if block_given?
37           yield Responder.new(responses) 
38         else
39           Responder.new(responses)
40         end
41       end
43       def reset!
44         requests.clear
45         responses.clear
46       end
47     end
49     for method in [ :post, :put ]
50       module_eval <<-EOE
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}"))
55         end
56       EOE
57     end
59     for method in [ :get, :delete ]
60       module_eval <<-EOE
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}"))
65         end
66       EOE
67     end
69     def initialize(site)
70       @site = site
71     end
72   end
74   class Request
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')
80     end
82     def ==(other_request)
83       other_request.hash == hash
84     end
86     def eql?(other_request)
87       self == other_request
88     end
90     def to_s
91       "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>"
92     end
94     def hash
95       "#{path}#{method}#{headers}".hash
96     end
97   end
99   class Response
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
105     end
107     def success?
108       (200..299).include?(code)
109     end
111     def [](key)
112       headers[key]
113     end
115     def []=(key, value)
116       headers[key] = value
117     end
118     
119     def ==(other)
120       if (other.is_a?(Response))
121         other.body == body && other.message == message && other.headers == headers
122       else
123         false
124       end
125     end
126   end
128   class Connection
129     private
130       silence_warnings do
131         def http
132           @http ||= HttpMock.new(@site)
133         end
134       end
135   end