diff options
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 |