Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added POST Method

  • Loading branch information...
commit d156fb2a9f10ace96a028458c0800741275a686c 1 parent 4485d1c
@kfoss authored
Showing with 51 additions and 1 deletion.
  1. +6 −0 README.rdoc
  2. +20 −1 lib/tor/http.rb
  3. +25 −0 spec/http_spec.rb
View
6 README.rdoc
@@ -12,9 +12,14 @@
p res.code
p res.body
+ res = Tor::HTTP.post('posttestserver.com', {"var" => "variable"}, '/post.php?dir=example', 80)
+ p res.code
+ p res.body
+
OR
res = Tor::HTTP.get(URI('http://google.com/'))
+ res = Tor::HTTP.post(URI('http://posttestserver.com/post.php?dir=example'), {"var" => "variable"})
The default port configuration is 9050 and ip configuration is 127.0.0.1. If you need to change configuration, you can use:
@@ -38,3 +43,4 @@ The default port configuration is 9050 and ip configuration is 127.0.0.1. If you
Copyright (c) 2012 Bruno Ghisi. See LICENSE.txt for
further details.
+
View
21 lib/tor/http.rb
@@ -20,7 +20,26 @@ def self.get(uri_or_host, path = nil, port = nil)
res
end
+ def self.post(uri_or_host, post_options = {}, path = nil, port = nil)
+ res = ""
+ host = nil
+ if path
+ host = uri_or_host
+ else
+ host = uri_or_host.host
+ port = uri_or_host.port
+ path = uri_or_host.request_uri
+ end
+
+ Net::HTTP.SOCKSProxy(Tor.configuration.ip, Tor.configuration.port).start(host, port) do |http|
+ request = Net::HTTP::Post.new(path)
+ request.set_form_data(post_options)
+ res = http.request(request)
+ end
+ res
+ end
+
end
end
-
+
View
25 spec/http_spec.rb
@@ -28,4 +28,29 @@
end
+ describe "post" do
+
+ context "with invalid parameters" do
+ it "does not work" do
+ expect { Tor::HTTP.post("google.com") }.to raise_error
+ end
+ end
+
+ context "with URI parameter" do
+ it "works" do
+ res = Tor::HTTP.post(URI('http://posttestserver.com/post.php?dir=example'), {"q" => "query", "var" => "variable"})
+ res.code.should eq("200")
+ end
+ end
+
+ context "with host, path and port parameters" do
+ it "works" do
+ res = Tor::HTTP.post('posttestserver.com', {"q" => "query", "var" => "variable"}, '/post.php?dir=example', 80)
+ res.code.should eq("200")
+ end
+
+ end
+
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.