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/test | |
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/test')
-rw-r--r-- | activeresource/test/abstract_unit.rb | 9 | ||||
-rw-r--r-- | activeresource/test/http_mock.rb | 123 |
2 files changed, 4 insertions, 128 deletions
diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb index 56a78405e3..f19635826c 100644 --- a/activeresource/test/abstract_unit.rb +++ b/activeresource/test/abstract_unit.rb @@ -1,9 +1,8 @@ -$:.unshift(File.dirname(__FILE__) + '/../lib') -require 'active_resource' require 'test/unit' -require 'active_support/breakpoint' -$:.unshift(File.dirname(__FILE__) + '/.') -require 'http_mock' +$:.unshift "#{File.dirname(__FILE__)}/../lib" +require 'active_resource' +require 'active_resource/http_mock' +require 'active_support/breakpoint' ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log") diff --git a/activeresource/test/http_mock.rb b/activeresource/test/http_mock.rb deleted file mode 100644 index e22a61e6a7..0000000000 --- a/activeresource/test/http_mock.rb +++ /dev/null @@ -1,123 +0,0 @@ -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 |