Permalink
Browse files

rspec color added. added valid and errors methods and raise exception…

…s stuff other other branch
  • Loading branch information...
1 parent 16525eb commit 52600b4ead5e5d0a77f485a1ac33f4b8e94a3edd @kophyo kophyo committed Dec 27, 2012
View
4 .rspec
@@ -0,0 +1,4 @@
+--color
+--drb
+--debug
+--tag ~@slow
View
13 lib/shutl/resource/configuration.rb
@@ -1,6 +1,19 @@
require 'logger'
module Shutl::Resource
+
+ class << self
+ attr_writer :raise_exceptions_on_validation
+
+ def raise_exceptions_on_validation
+ if @raise_exceptions_on_validation.nil?
+ @raise_exceptions_on_validation = true
+ end
+
+ @raise_exceptions_on_validation
+ end
+ end
+
def configure(*args, &block)
Configuration.configure(*args, &block)
end
View
6 lib/shutl/resource/rest.rb
@@ -70,6 +70,12 @@ def status
response.code
end
+ attr_accessor :errors
+
+ def valid?
+ errors.blank?
+ end
+
def resource_id
instance_variable_get :"@#{self.class.resource_id_name}"
end
View
18 lib/shutl/resource/rest_class_methods.rb
@@ -150,7 +150,15 @@ def perform_action instance, verb, args, failure_message
end
def new_object(args={}, response=nil)
- new add_resource_id_to(args), response
+ instance = new add_resource_id_to(args)
+
+ instance.tap do |i|
+ parsed_response = response.parsed_response
+
+ if errors = (parsed_response and parsed_response["errors"])
+ i.errors = errors
+ end
+ end
end
def check_fail response, message
@@ -163,7 +171,13 @@ def check_fail response, message
when 404 then Shutl::ResourceNotFound
when 409 then Shutl::ResourceConflict
when 410 then Shutl::ResourceGone
- when 422 then Shutl::ResourceInvalid
+ when 422
+ if Shutl::Resource.raise_exceptions_on_validation
+ Shutl::ResourceInvalid
+ else
+ nil #handled as validation failure
+ end
+
when 411..499
Shutl::BadRequest
when 500 then Shutl::ServerError
View
29 spec/rest_resource_spec.rb
@@ -155,7 +155,32 @@
end
end
- describe '#create' do
+ describe '.create' do
+
+ context "With the setting to not raise exceptions" do
+ before do
+ Shutl::Resource.raise_exceptions_on_validation = false
+ end
+
+ after do
+ Shutl::Resource.raise_exceptions_on_validation = true
+ end
+ specify do
+ errors = {"base" => "invalid", "some_field" => "some field is invalid"}
+ body = {"errors" => errors}.to_json
+
+ @request = stub_request(:post, 'http://host/test_rests').
+ to_return(:status => 422, body: body, :headers => headers)
+
+ expect{@instance = TestRest.create}.to_not raise_error Shutl::ResourceInvalid
+
+ @request.should have_been_requested
+ @instance.should_not be_valid
+ @instance.errors.should == errors
+ end
+ end
+
+
it 'should send a post request to the endpoint' do
request = stub_post 200
@@ -179,7 +204,7 @@ def stub_post status
it 'should post the header content-type: json' do
request = stub_request(:post, 'http://host/test_rests').
- with( :headers => headers )
+ with(:body => "{\"test_rest\":{}}", :headers => headers )
TestRest.create

0 comments on commit 52600b4

Please sign in to comment.