Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding support for submitting data with a request

  • Loading branch information...
commit 705c0fbcaee04babe158ab4a7535276623dd7469 1 parent 6d175bf
@trydionel authored
View
2  README.md
@@ -26,6 +26,7 @@ Also comes with a command line tool `web_fixtures` which allow you to run simple
get "http://api.foursquare.com/v1/user"
get "http://www.foursquare.com", :authenticate => false
+ post "http://api.foursquare.com/v1/checkin", :data => { :vid => 24676 }
## File Storage
@@ -46,6 +47,7 @@ WebFixtures supports authentication through Basic HTTP Auth. After setting `aut
* Can enable/disable storing header data with `include_headers` [default: true]
* Specify storage path for fixtures with `storage_path` [default: './fixtures']
* Supports HTTP Basic Auth with the `authenticate` option [default: false]
+* Pass data to a URI via the `:data` inline option [default: nil]
* These options are available as both top-level methods and inline options for overriding defaults on a single request.
## Note on Patches/Pull Requests
View
9 lib/web_fixtures/request.rb
@@ -1,3 +1,5 @@
+require 'cgi'
+
module WebFixtures
class Request
@@ -27,6 +29,7 @@ def curl_command
command = "curl -s"
command << " -i" if options[:include_headers]
command << " -u #{collect_username}:#{collect_password}" if options[:authenticate]
+ command << " -d #{data_string}" if options[:data]
command << " -X #{method.to_s.upcase}" if method != :get
command << " -o \"#{output_file}\""
command << " \"#{uri}\""
@@ -53,6 +56,12 @@ def output_file
File.join(storage_path, filename)
end
+ def data_string
+ options[:data].map do |key, value|
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
+ end.join('&')
+ end
+
def collect_username
return username if username
return nil unless options[:authenticate]
View
10 spec/web_fixtures/dsl_spec.rb
@@ -53,6 +53,16 @@
subject.send method, "http://www.google.com", :authenticate => true
end
+ it "should allow for sending data" do
+ WebFixtures::Request.should_receive(:new).
+ with(method,
+ 'http://www.google.com/search',
+ hash_including(:data => { :q => 'foo' }))
+
+ # Poor example
+ subject.send method, "http://www.google.com/search", :data => { :q => "foo" }
+ end
+
end
end
View
24 spec/web_fixtures/request_spec.rb
@@ -56,6 +56,25 @@
end
end
+
+ describe "#data_string" do
+
+ it "should map the data elements into key=value format" do
+ subject.options = { :data => { :foo => "foo" } }
+ subject.data_string.should == "foo=foo"
+ end
+
+ it "should join the data elements" do
+ subject.options = { :data => { :foo => "foo", :bar => "bar" } }
+ subject.data_string.should match /(foo=foo|bar=bar)\&(foo=foo|bar=bar)/ # messy way to handle (lack-of) hash ordering
+ end
+
+ it "should url-encode the data elements" do
+ subject.options = { :data => { :foo => "f o o" } }
+ subject.data_string.should == "foo=f+o+o"
+ end
+
+ end
describe "#collect_password" do
@@ -129,6 +148,11 @@
subject.curl_command.should include('-u foo:bar')
end
+
+ it "should include -d key=val when passing data" do
+ subject.options = { :data => { :foo => "foo" } }
+ subject.curl_command.should include('-d foo=foo')
+ end
it "should not include -X for GET requests" do
subject.curl_command.should_not include('-X GET')
Please sign in to comment.
Something went wrong with that request. Please try again.