Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

*_path instance methods. Check for missing/invalid site uri. http_moc…

…k 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
  • Loading branch information...
commit 7370e54cc4d6b298a29383bf943b277bcfb730aa 1 parent 0ee0c1b
@jeremy jeremy authored
View
14 activeresource/lib/active_resource/base.rb
@@ -106,7 +106,7 @@ def save
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 @@ def connection(refresh = false)
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 @@ def id_from_response(response)
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)
View
7 activeresource/lib/active_resource/connection.rb
@@ -40,9 +40,10 @@ class << self ; attr_reader :default_header end
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 @@ def post(path, body = '')
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
View
2  activeresource/test/abstract_unit.rb
@@ -5,3 +5,5 @@
$:.unshift(File.dirname(__FILE__) + '/.')
require 'http_mock'
+
+ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")
View
32 activeresource/test/connection_test.rb
@@ -2,7 +2,7 @@
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 @@ def setup
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 @@ def test_handle_response
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
View
32 activeresource/test/http_mock.rb
@@ -2,13 +2,13 @@
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 @@ def #{method}(path, body, headers)
end
EOE
end
-
+
for method in [ :get, :delete ]
module_eval <<-EOE
def #{method}(path, headers)
@@ -60,7 +60,7 @@ def #{method}(path, headers)
end
EOE
end
-
+
def initialize(site)
@site = site
end
@@ -68,7 +68,7 @@ def initialize(site)
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 @@ def initialize(method, path, body = nil, headers = {})
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 @@ def success?
def [](key)
headers[key]
end
-
+
def []=(key, value)
headers[key] = value
end
-
end
class Connection

0 comments on commit 7370e54

Please sign in to comment.
Something went wrong with that request. Please try again.