*_path instance methods. Check for missing/invalid site uri. http_mock response takes message arg, extracts numeric code. Tests log to test/debug.log
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5680 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
0ee0c1b2aa
commit
7370e54cc4
|
@ -106,7 +106,7 @@ module ActiveResource
|
|||
end
|
||||
|
||||
def destroy
|
||||
connection.delete(self.class.element_path(id, prefix_options))
|
||||
connection.delete(element_path)
|
||||
end
|
||||
|
||||
def to_xml(options={})
|
||||
|
@ -155,12 +155,12 @@ module ActiveResource
|
|||
end
|
||||
|
||||
def update
|
||||
connection.put(self.class.element_path(id, prefix_options), to_xml)
|
||||
connection.put(element_path, to_xml)
|
||||
true
|
||||
end
|
||||
|
||||
def create
|
||||
resp = connection.post(self.class.collection_path(prefix_options), to_xml)
|
||||
resp = connection.post(collection_path, to_xml)
|
||||
self.id = id_from_response(resp)
|
||||
true
|
||||
end
|
||||
|
@ -170,6 +170,14 @@ module ActiveResource
|
|||
response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
|
||||
end
|
||||
|
||||
def element_path(options = nil)
|
||||
self.class.element_path(id, options || prefix_options)
|
||||
end
|
||||
|
||||
def collection_path(options = nil)
|
||||
self.class.collection_path(options || prefix_options)
|
||||
end
|
||||
|
||||
private
|
||||
def find_or_create_resource_for_collection(name)
|
||||
find_or_create_resource_for(name.to_s.singularize)
|
||||
|
|
|
@ -40,7 +40,8 @@ module ActiveResource
|
|||
end
|
||||
|
||||
def initialize(site)
|
||||
self.site = site.is_a?(URI) ? site : URI.parse(site)
|
||||
raise ArgumentError, 'Missing site URI' unless site
|
||||
self.site = site
|
||||
end
|
||||
|
||||
def site=(site)
|
||||
|
@ -65,7 +66,7 @@ module ActiveResource
|
|||
|
||||
private
|
||||
def request(method, path, *arguments)
|
||||
logger.info "requesting #{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
|
||||
logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
|
||||
result = nil
|
||||
time = Benchmark.realtime { result = http.send(method, path, *arguments) }
|
||||
logger.info "--> #{result.code} #{result.message} (#{result.body.length}b %.2fs)" % time if logger
|
||||
|
|
|
@ -5,3 +5,5 @@ require 'active_support/breakpoint'
|
|||
|
||||
$:.unshift(File.dirname(__FILE__) + '/.')
|
||||
require 'http_mock'
|
||||
|
||||
ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")
|
||||
|
|
|
@ -2,7 +2,7 @@ require "#{File.dirname(__FILE__)}/abstract_unit"
|
|||
require 'base64'
|
||||
|
||||
class ConnectionTest < Test::Unit::TestCase
|
||||
Response = Struct.new(:code)
|
||||
ResponseCodeStub = Struct.new(:code)
|
||||
|
||||
def setup
|
||||
@conn = ActiveResource::Connection.new('http://localhost')
|
||||
|
@ -20,8 +20,8 @@ class ConnectionTest < Test::Unit::TestCase
|
|||
def test_handle_response
|
||||
# 2xx and 3xx are valid responses.
|
||||
[200, 299, 300, 399].each do |code|
|
||||
expected = Response.new(code)
|
||||
assert_equal expected, @conn.send(:handle_response, expected)
|
||||
expected = ResponseCodeStub.new(code)
|
||||
assert_equal expected, handle_response(expected)
|
||||
end
|
||||
|
||||
# 404 is a missing resource.
|
||||
|
@ -49,11 +49,17 @@ class ConnectionTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_initialize_raises_argument_error_on_missing_site
|
||||
assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) }
|
||||
end
|
||||
|
||||
def test_site_accessor_accepts_uri_or_string_argument
|
||||
site = URI.parse("http://localhost")
|
||||
|
||||
assert_raise(URI::InvalidURIError) { @conn.site = nil }
|
||||
|
||||
assert_nothing_raised { @conn.site = "http://localhost" }
|
||||
assert_equal site, @conn.site
|
||||
assert_equal site, @conn.site
|
||||
|
||||
assert_nothing_raised { @conn.site = site }
|
||||
assert_equal site, @conn.site
|
||||
|
@ -82,7 +88,11 @@ class ConnectionTest < Test::Unit::TestCase
|
|||
protected
|
||||
def assert_response_raises(klass, code)
|
||||
assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
|
||||
@conn.send(:handle_response, Response.new(code))
|
||||
handle_response ResponseCodeStub.new(code)
|
||||
end
|
||||
end
|
||||
|
||||
def handle_response(response)
|
||||
@conn.send(:handle_response, response)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -92,10 +92,11 @@ module ActiveResource
|
|||
end
|
||||
|
||||
class Response
|
||||
attr_accessor :body, :code, :headers
|
||||
attr_accessor :body, :message, :code, :headers
|
||||
|
||||
def initialize(body, code = 200, headers = {})
|
||||
@body, @code, @headers = body, 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?
|
||||
|
@ -109,7 +110,6 @@ module ActiveResource
|
|||
def []=(key, value)
|
||||
headers[key] = value
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Connection
|
||||
|
|
Loading…
Reference in New Issue