*_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,9 +40,10 @@ 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)
|
||||
@site = site.is_a?(URI) ? site : URI.parse(site)
|
||||
end
|
||||
|
@ -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.
|
||||
|
@ -48,41 +48,51 @@ class ConnectionTest < Test::Unit::TestCase
|
|||
assert_response_raises ActiveResource::ConnectionError, code
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
def test_get
|
||||
matz = @conn.get("/people/1.xml")
|
||||
assert_equal "Matz", matz["person"]["name"]
|
||||
end
|
||||
|
||||
|
||||
def test_post
|
||||
response = @conn.post("/people.xml")
|
||||
assert_equal "/people/5.xml", response["Location"]
|
||||
end
|
||||
|
||||
|
||||
def test_put
|
||||
response = @conn.put("/people/1.xml")
|
||||
assert_equal 204, response.code
|
||||
end
|
||||
|
||||
|
||||
def test_delete
|
||||
response = @conn.delete("/people/1.xml")
|
||||
assert_equal 200, response.code
|
||||
end
|
||||
|
||||
|
||||
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
|
||||
|
|
|
@ -2,13 +2,13 @@ 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 = {})
|
||||
|
@ -50,7 +50,7 @@ module ActiveResource
|
|||
end
|
||||
EOE
|
||||
end
|
||||
|
||||
|
||||
for method in [ :get, :delete ]
|
||||
module_eval <<-EOE
|
||||
def #{method}(path, headers)
|
||||
|
@ -60,7 +60,7 @@ module ActiveResource
|
|||
end
|
||||
EOE
|
||||
end
|
||||
|
||||
|
||||
def initialize(site)
|
||||
@site = site
|
||||
end
|
||||
|
@ -68,7 +68,7 @@ module ActiveResource
|
|||
|
||||
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')
|
||||
|
@ -77,27 +77,28 @@ module ActiveResource
|
|||
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, :code, :headers
|
||||
|
||||
def initialize(body, code = 200, headers = {})
|
||||
@body, @code, @headers = body, code, headers
|
||||
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
|
||||
|
@ -105,11 +106,10 @@ module ActiveResource
|
|||
def [](key)
|
||||
headers[key]
|
||||
end
|
||||
|
||||
|
||||
def []=(key, value)
|
||||
headers[key] = value
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Connection
|
||||
|
|
Loading…
Reference in New Issue