Skip to content
This repository
Browse code

Improve exception handling when Location header is invalid. [#1192 st…

…ate:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 166c63818e901e64786a76029febf7c9cdb40f2d 1 parent 91eeb0f
Bob Aman authored January 28, 2009 lifo committed January 28, 2009
2  activeresource/lib/active_resource/base.rb
@@ -1006,7 +1006,7 @@ def load_attributes_from_response(response)
1006 1006
 
1007 1007
       # Takes a response from a typical create post and pulls the ID out
1008 1008
       def id_from_response(response)
1009  
-        response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
  1009
+        response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1] if response['Location']
1010 1010
       end
1011 1011
 
1012 1012
       def element_path(options = nil)
15  activeresource/test/base_test.rb
@@ -627,6 +627,12 @@ def test_id_from_response
627 627
     assert_equal '1', p.__send__(:id_from_response, resp)
628 628
   end
629 629
 
  630
+  def test_id_from_response_without_location
  631
+    p = Person.new
  632
+    resp = {}
  633
+    assert_equal nil, p.__send__(:id_from_response, resp)
  634
+  end
  635
+
630 636
   def test_create_with_custom_prefix
631 637
     matzs_house = StreetAddress.new(:person_id => 1)
632 638
     matzs_house.save
@@ -670,7 +676,6 @@ def test_reload_works_without_prefix_options
670 676
     assert_equal person, person.reload
671 677
   end
672 678
 
673  
-
674 679
   def test_create
675 680
     rick = Person.create(:name => 'Rick')
676 681
     assert rick.valid?
@@ -687,6 +692,14 @@ def test_create
687 692
     assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') }
688 693
   end
689 694
 
  695
+  def test_create_without_location
  696
+    ActiveResource::HttpMock.respond_to do |mock|
  697
+      mock.post   "/people.xml", {}, nil, 201
  698
+    end
  699
+    person = Person.create(:name => 'Rick')
  700
+    assert_equal nil, person.id
  701
+  end
  702
+
690 703
   def test_clone
691 704
     matz = Person.find(1)
692 705
     matz_c = matz.clone

0 notes on commit 166c638

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