diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-21 20:26:30 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-21 20:26:30 +0000 |
commit | 93c816f0c2ad45ef898de7c2dd2987f9ccb02678 (patch) | |
tree | 05b76bdaa6172648135c73eb4389ea2990ac82d3 /activeresource/lib | |
parent | 01452feff1ab4e06ed41cd093bb8a140cd6b2cb6 (diff) | |
download | rails-93c816f0c2ad45ef898de7c2dd2987f9ccb02678.tar.gz rails-93c816f0c2ad45ef898de7c2dd2987f9ccb02678.tar.bz2 rails-93c816f0c2ad45ef898de7c2dd2987f9ccb02678.zip |
move http_mock to lib so others can use it in their Ares tests
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5766 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/http_mock.rb | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb new file mode 100644 index 0000000000..e22a61e6a7 --- /dev/null +++ b/activeresource/lib/active_resource/http_mock.rb @@ -0,0 +1,123 @@ +require 'active_resource/connection' + +module ActiveResource + class InvalidRequestError < StandardError; end + + class HttpMock + class Responder + def initialize(responses) + @responses = responses + end + + for method in [ :post, :put, :get, :delete ] + module_eval <<-EOE + 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) + end + EOE + end + end + + class << self + def requests + @@requests ||= [] + end + + def responses + @@responses ||= {} + end + + def respond_to(pairs = {}) + reset! + pairs.each do |(path, response)| + responses[path] = response + end + yield Responder.new(responses) if block_given? + end + + def reset! + requests.clear + responses.clear + end + end + + 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 + self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for: \#{request}")) + end + EOE + end + + for method in [ :get, :delete ] + module_eval <<-EOE + 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 + + def initialize(site) + @site = site + end + end + + class Request + attr_accessor :path, :method, :body, :headers + + def initialize(method, path, body = nil, headers = {}) + @method, @path, @body, @headers = method, path, body, headers + @headers.update('Content-Type' => 'application/xml') + end + + def ==(other_request) + other_request.hash == hash + end + + def eql?(other_request) + self == other_request + end + + def to_s + "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>" + end + + def hash + "#{path}#{method}#{headers}".hash + end + end + + class Response + 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 + end + + def success? + (200..299).include?(code) + end + + def [](key) + headers[key] + end + + def []=(key, value) + headers[key] = value + end + end + + class Connection + private + silence_warnings do + def http + @http ||= HttpMock.new(@site) + end + end + end +end |