Permalink
Browse files

Add support for POST using Patron.

  • Loading branch information...
1 parent 3df3029 commit a328b8b21471ee55d52908dc2d5fb1ef3c8288fe @mdub committed Oct 12, 2010
Showing with 29 additions and 5 deletions.
  1. +14 −2 lib/sham_rack/patron.rb
  2. +15 −3 spec/sham_rack_spec.rb
View
16 lib/sham_rack/patron.rb
@@ -27,7 +27,9 @@ def handle_request_with_rack(patron_request, rack_app)
end
def rack_env_for(patron_request)
- Rack::MockRequest.env_for(patron_request.url, :method => patron_request.action)
+ env = Rack::MockRequest.env_for(patron_request.url, :method => patron_request.action, :input => patron_request.upload_data)
+ env.merge!(header_env(patron_request))
+ env
end
def patron_response(rack_response)
@@ -37,10 +39,20 @@ def patron_response(rack_response)
res.instance_variable_set(:@status, status)
res.instance_variable_set(:@status_line, "HTTP/1.1 #{status} #{status_code}")
res.instance_variable_set(:@body, assemble_body(body))
- # res.instance_variable_set(:@headers, webmock_response.headers)
+ res.instance_variable_set(:@headers, headers)
res
end
+ def header_env(patron_request)
+ env = {}
+ patron_request.headers.each do |header, content|
+ key = header.upcase.gsub('-', '_')
+ key = "HTTP_" + key unless key =~ /^CONTENT_(TYPE|LENGTH)$/
+ env[key] = content
+ end
+ env
+ end
+
def assemble_body(body)
content = ""
body.each { |fragment| content << fragment }
View
18 spec/sham_rack_spec.rb
@@ -49,9 +49,8 @@
end
it "can be accessed using Patron" do
- session = Patron::Session.new
- session.base_url = "http://www.test.xyz"
- response = session.get("/foo/bar")
+ patron = Patron::Session.new
+ response = patron.get("http://www.test.xyz/foo/bar")
response.body.should == "Hello, world"
end
@@ -228,6 +227,19 @@ def env
end
+ it "supports POST using Patron" do
+
+ patron = Patron::Session.new
+ response = patron.post("http://env.xyz/resource", "<xml/>", "Content-Type" => "application/xml")
+
+ response.status.should == "200 OK"
+
+ env["REQUEST_METHOD"].should == "POST"
+ env["rack.input"].read.should == "<xml/>"
+ env["CONTENT_TYPE"].should == "application/xml"
+
+ end
+
it "supports PUT" do
RestClient.put("http://env.xyz/thing1", "stuff", :content_type => "text/plain")

0 comments on commit a328b8b

Please sign in to comment.