Skip to content
This repository
Browse code

fixed failing tests now that non-GET requests are sent with .xml file…

… ext. Extracted #id_from_response as an entry point for customizing how a created resource gets its own ID.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5153 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 4d63e01fa052749f742e90bcb3889cbae6152c92 1 parent 5e5b87b
risk danger olson authored September 20, 2006
3  activeresource/CHANGELOG
... ...
@@ -1,5 +1,8 @@
1 1
 *SVN*
2 2
 
  3
+* Extracted #id_from_response as an entry point for customizing how a created resource gets its own ID.
  4
+  By default, it extracts from the Location response header.
  5
+
3 6
 * Optimistic locking: raise ActiveResource::ResourceConflict on 409 Conflict response. [Jeremy Kemper]
4 7
 
5 8
     # Example controller action
7  activeresource/lib/active_resource/base.rb
@@ -164,10 +164,15 @@ def update
164 164
 
165 165
       def create
166 166
         returning connection.post(self.class.collection_path(prefix_options), to_xml) do |resp|
167  
-          self.id = resp['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
  167
+          self.id = id_from_response(resp)
168 168
         end
169 169
       end
170 170
 
  171
+      # takes a response from a typical create post and pulls the ID out
  172
+      def id_from_response(response)
  173
+        response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
  174
+      end
  175
+
171 176
     private
172 177
       def find_or_create_resource_for_collection(name)
173 178
         find_or_create_resource_for(name.to_s.singularize)
2  activeresource/test/base_errors_test.rb
@@ -4,7 +4,7 @@
4 4
 class BaseErrorsTest < Test::Unit::TestCase
5 5
   def setup
6 6
     ActiveResource::HttpMock.respond_to do |mock|
7  
-      mock.post "/people", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 400
  7
+      mock.post "/people.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 400
8 8
     end
9 9
     @exception = nil
10 10
     @person    = Person.new(:name => '', :age => '')
31  activeresource/test/base_test.rb
@@ -10,22 +10,22 @@ def setup
10 10
     ActiveResource::HttpMock.respond_to do |mock|
11 11
       mock.get    "/people/1.xml",             @matz
12 12
       mock.get    "/people/2.xml",             @david
13  
-      mock.put    "/people/1",                 nil, 204
14  
-      mock.delete "/people/1",                 nil, 200
15  
-      mock.delete "/people/2",                 nil, 400
16  
-      mock.post   "/people",                   nil, 201, 'Location' => '/people/5.xml'
  13
+      mock.put    "/people/1.xml",             nil, 204
  14
+      mock.delete "/people/1.xml",             nil, 200
  15
+      mock.delete "/people/2.xml",             nil, 400
  16
+      mock.post   "/people.xml",               nil, 201, 'Location' => '/people/5.xml'
17 17
       mock.get    "/people/99.xml",            nil, 404
18 18
       mock.get    "/people.xml",               "<people>#{@matz}#{@david}</people>"
19 19
       mock.get    "/people/1/addresses.xml",   "<addresses>#{@addy}</addresses>"
20 20
       mock.get    "/people/1/addresses/1.xml", @addy
21  
-      mock.put    "/people/1/addresses/1",     nil, 204
22  
-      mock.delete "/people/1/addresses/1",     nil, 200
23  
-      mock.post   "/people/1/addresses",       nil, 201, 'Location' => '/people/1/addresses/5'
  21
+      mock.put    "/people/1/addresses/1.xml", nil, 204
  22
+      mock.delete "/people/1/addresses/1.xml", nil, 200
  23
+      mock.post   "/people/1/addresses.xml",   nil, 201, 'Location' => '/people/1/addresses/5'
24 24
       mock.get    "/people//addresses.xml",    nil, 404
25 25
       mock.get    "/people//addresses/1.xml",  nil, 404
26  
-      mock.put    "/people//addresses/1",      nil, 404
27  
-      mock.delete "/people//addresses/1",      nil, 404
28  
-      mock.post   "/people//addresses",        nil, 404
  26
+      mock.put    "/people//addresses/1.xml",  nil, 404
  27
+      mock.delete "/people//addresses/1.xml",  nil, 404
  28
+      mock.post   "/people//addresses.xml",    nil, 404
29 29
     end
30 30
   end
31 31
 
@@ -111,6 +111,15 @@ def test_create
111 111
     assert_equal '5', rick.id
112 112
   end
113 113
 
  114
+  def test_id_from_response
  115
+    p = Person.new
  116
+    resp = {'Location' => '/foo/bar/1'}
  117
+    assert_equal '1', p.send(:id_from_response, resp)
  118
+    
  119
+    resp['Location'] << '.xml'
  120
+    assert_equal '1', p.send(:id_from_response, resp)
  121
+  end
  122
+
114 123
   def test_create_with_custom_prefix
115 124
     matzs_house = StreetAddress.new({}, {:person_id => 1})
116 125
     matzs_house.save
@@ -136,7 +145,7 @@ def test_update_with_custom_prefix
136 145
   def test_update_conflict
137 146
     ActiveResource::HttpMock.respond_to do |mock|
138 147
       mock.get "/people/2.xml", @david
139  
-      mock.put "/people/2", nil, 409
  148
+      mock.put "/people/2.xml", nil, 409
140 149
     end
141 150
     assert_raises(ActiveResource::ResourceConflict) { Person.find(2).save }
142 151
   end

0 notes on commit 4d63e01

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