Browse files

Move the post body out even further

  • Loading branch information...
1 parent 2261559 commit a94f79fae4c513e213dcf35bd25e90735b9cf36b @qrush committed Dec 22, 2009
Showing with 44 additions and 48 deletions.
  1. +3 −3 lib/rest/request.rb
  2. +41 −45 test/rest_request_test.rb
View
6 lib/rest/request.rb
@@ -51,10 +51,8 @@ def perform
self.request = Net::HTTP::Delete.new(url.path, headers)
when :put
self.request = Net::HTTP::Put.new(url.path, headers)
- self.request.body = body
when :post
self.request = Net::HTTP::Post.new(url.path, headers)
- self.request.body = body
else
raise ArgumentError, "Unknown HTTP verb `#{verb}'"
end
@@ -84,7 +82,9 @@ def perform
http_request.verify_mode = verify_mode
end
- response = http_request.start {|http| http.request(request, request.body) }
+ response = http_request.start do |http|
+ http.request(request, body)
+ end
REST::Response.new(response.code, response.instance_variable_get('@header'), response.body)
end
View
86 test/rest_request_test.rb
@@ -9,93 +9,89 @@
http_response.add_field('Content-type', 'text/html')
Net::HTTP.any_instance.stubs(:start).returns(http_response)
end
-
+
it "should GET a resource" do
request = REST::Request.new(:get, URI.parse('http://example.com/resources/1'))
response = request.perform
-
+
response.status_code.should == 200
response.body.should == 'It works!'
end
-
+
it "should HEAD a resource" do
request = REST::Request.new(:head, URI.parse('http://example.com/resources/1'))
response = request.perform
-
+
response.status_code.should == 200
end
it "should DELETE a resource" do
request = REST::Request.new(:delete, URI.parse('http://example.com/resources/1'))
response = request.perform
-
- response.status_code.should == 200
- end
-
- it "should PUT a resource" do
- body = 'name=Manfred'
- request = REST::Request.new(:put, URI.parse('http://example.com/resources/1'), body)
-
- response = request.perform
- request.request.body.should == body
-
- response.status_code.should == 200
- response.body.should == 'It works!'
- end
-
- it "should POST a resource" do
- body = 'name=Manfred'
- request = REST::Request.new(:post, URI.parse('http://example.com/resources'), body)
-
- response = request.perform
- request.request.body.should == body
-
+
response.status_code.should == 200
- response.body.should == 'It works!'
- end
-
- it "should move body to the underlying request object" do
- body = 'It works!'
- request = REST::Request.new(:post, URI.parse('http://example.com/resources'), body)
-
- post = mock()
- post.expects(:body=).with(body)
- Net::HTTP::Post.stubs(:new).with(request.url.path, {}).returns(post)
-
- request.perform
end
-
+
it "should move headers to the underlying request object" do
headers = {'Accepts' => 'text/html', 'X-Helo' => 'Hi there!'}
request = REST::Request.new(:post, URI.parse('http://example.com/resources'), '', headers)
Net::HTTP::Post.expects(:new).with(request.url.path, headers).returns(stub(:body= => ''))
request.perform
end
-
+
it "should move the response headers to the REST::Response object" do
request = REST::Request.new(:get, URI.parse('http://example.com/resources/1'))
response = request.perform
response.headers['content-type'].should == ['text/html']
end
-
+
it "should move basic authentication credentials to the underlying request object" do
request = REST::Request.new(:post, URI.parse('http://example.com/resources'), '', {}, {:username => 'admin', :password => 'secret'})
Net::HTTP::Post.any_instance.expects(:basic_auth).with('admin', 'secret')
request.perform
end
-
+
it "should GET a resource from an HTTPS URL" do
request = REST::Request.new(:get, URI.parse('https://example.com/resources/1'))
response = request.perform
-
+
response.status_code.should == 200
response.body.should == 'It works!'
end
-
+
it "should raise an argumenterror for unknown verbs" do
request = REST::Request.new(:unknown, '')
lambda {
request.perform
}.should.raise(ArgumentError)
end
-end
+end
+
+describe "A REST request with a post body" do
+ before do
+ @body = 'name=Manfred'
+ starter = stub("http request starter")
+ starter.expects(:request).with(anything, @body)
+
+ http_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ http_response.stubs(:read_body).returns('It works!')
+ http_response.add_field('Content-type', 'text/html')
+ Net::HTTP.any_instance.stubs(:start).yields(starter).returns(http_response)
+ end
+
+ it "should PUT a resource" do
+ request = REST::Request.new(:put, URI.parse('http://example.com/resources/1'), @body)
+ response = request.perform
+
+ response.status_code.should == 200
+ response.body.should == 'It works!'
+ end
+
+ it "should POST a resource" do
+ request = REST::Request.new(:post, URI.parse('http://example.com/resources'), @body)
+ response = request.perform
+
+ response.status_code.should == 200
+ response.body.should == 'It works!'
+ end
+end

0 comments on commit a94f79f

Please sign in to comment.