Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactoring, fixed sending cookies

  • Loading branch information...
commit c6f3c6e6c1fed6ea3cd21800de3699e7543d1ce0 1 parent c9737b2
@xianhuazhou authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+*.swp
+*.gem
View
4 Gemfile
@@ -0,0 +1,4 @@
+group :test do
+ gem 'rspec'
+ gem 'thin'
+end
View
25 Gemfile.lock
@@ -0,0 +1,25 @@
+GEM
+ specs:
+ daemons (1.1.8)
+ diff-lcs (1.1.3)
+ eventmachine (0.12.10)
+ rack (1.4.1)
+ rspec (2.9.0)
+ rspec-core (~> 2.9.0)
+ rspec-expectations (~> 2.9.0)
+ rspec-mocks (~> 2.9.0)
+ rspec-core (2.9.0)
+ rspec-expectations (2.9.1)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.9.0)
+ thin (1.4.1)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rspec
+ thin
View
6 Rakefile
@@ -0,0 +1,6 @@
+require 'rspec/core/rake_task'
+
+desc 'run spec'
+RSpec::Core::RakeTask.new do |t|
+ t.pattern = './spec/http_request_spec.rb'
+end
View
26 lib/http_request.rb
@@ -51,13 +51,13 @@ def update_cookies(response)
return unless response.header['set-cookie']
response.get_fields('set-cookie').each {|k|
k, v = k.split(';')[0].split('=')
- @@__cookies[k] = v
+ @@__cookies[@@__cookie_jar][k] = CGI.unescape(v)
}
end
# return cookies
def cookies
- @@__cookies
+ @@__cookies[@@__cookie_jar]
end
# check the http resource whether or not available
@@ -65,7 +65,7 @@ def available?(url, timeout = 5)
timeout(timeout) {
u = URI(url)
s = TCPSocket.new(u.host, u.port)
- s.close
+ s.cspecify lose
}
return true
rescue Exception => e
@@ -81,10 +81,21 @@ def call_request_method(method_name, *options, &block)
end
# we need to retrieve the cookies from last http response before reset cookies if it's a Net::HTTPResponse
- options[:cookies] = options[:cookies].cookies if options.is_a?(Hash) and options[:cookies].is_a?(Net::HTTPResponse)
+ # and make sure we have a cookie_jar to store the cookies
+ cookie_jar = 'default' # default name of cookie jar
+ if options.is_a?(Hash)
+ options[:cookies] = options[:cookies].cookies if options[:cookies].is_a?(Net::HTTPResponse)
+ if options[:cookie_jar].is_a? String
+ cookie_jar = options[:cookie_jar]
+ else
+ options[:cookie_jar] = cookie_jar
+ end
+ end
# reset
- @@__cookies = {}
+ @@__cookie_jar = cookie_jar
+ @@__cookies = {} unless defined? @@__cookies
+ @@__cookies[cookie_jar] = {} if @@__cookies[cookie_jar].nil?
@@redirect_times = 0
self.instance.request(method_name, options, &block)
end
@@ -262,7 +273,7 @@ def parse_options(method, options)
@uri = URI(@options[:url])
@uri.path = '/' if @uri.path.empty?
@headers = {
- 'Host' => @uri.host,
+ 'Host' => "#{@uri.host}:#{@uri.port}",
'Referer' => @options[:url],
'User-Agent' => 'HttpRequest.rb ' + self.class.version
}
@@ -293,6 +304,7 @@ def parse_options(method, options)
end
@headers['Cookie'] = cookies unless cookies.empty?
end
+ @headers['Connection'] = 'keep-alive'
end
# parse parameters for the options[:parameters] and @uri.query
@@ -392,7 +404,7 @@ def send_request(http)
else
@uri.path + @options[:parameters]
end
- h = http.method(@options[:method]).call(path, @headers)
+ h = http.method(@options[:method]).call(path, @headers)
else
h = http.method(@options[:method]).call("#{@uri.path}?#{@uri.query}", @options[:parameters], @headers)
end
View
291 spec/http_request_spec.rb
@@ -0,0 +1,291 @@
+require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'http_request.rb')
+
+URL = 'http://localhost:9527'
+hr = HttpRequest
+
+describe HttpRequest do
+
+ before :all do
+ Thread.new do |t|
+ web_server = File.join(File.expand_path(File.dirname(__FILE__)), 'web_server.rb')
+ @process_id = spawn "ruby #{web_server}", :in => "/dev/null", :out => "/dev/null"
+ sleep 2
+ end.join
+ end
+
+ after :all do
+ Process.kill 'KILL', @process_id
+ Process.wait
+ end
+
+ context "some basic http requests" do
+
+ it "can get the first page" do
+ hr.get(URL) do |http|
+ http.body.should == 'It Works!'
+ http['content-type'].should == 'text/html'
+ end
+ hr.get(URL + '/').body.should == 'It Works!'
+ end
+
+ it "has post or get method" do
+ hr.get(URL + "/method/post").body.should == 'No'
+ hr.post(URL + "/method/post").body.should == 'Yes'
+
+ hr.get(URL + "/method/get").body.should == 'Yes'
+ hr.post(URL + "/method/get").body.should == 'No'
+ end
+
+ it "can send AJAX requests" do
+ hr.get(:url => URL + "/ajax").body.should == 'N'
+ hr.get(:url => URL + "/ajax", :xhr => true).body.should == 'Y'
+ hr.get(:url => URL + "/ajax", :ajax => true).body.should == 'Y'
+
+ hr.get(URL + "/ajax", :xhr => true).body.should == 'Y'
+ hr.get(URL + "/ajax", :ajax => true).body.should == 'Y'
+ end
+
+ it "supports the following methods" do
+ url = URL + '/get-method-name'
+ hr.get(url).body.should == 'GET'
+ hr.post(url).body.should == 'POST'
+ hr.put(url).body.should == 'PUT'
+ hr.delete(url).body.should == 'DELETE'
+ hr.trace(url).body.should == 'TRACE'
+ hr.lock(url).body.should == 'LOCK'
+ hr.unlock(url).body.should == 'UNLOCK'
+ hr.move(url).body.should == 'MOVE'
+ hr.copy(url).body.should == 'COPY'
+ hr.propfind(url).body.should == 'PROPFIND'
+ hr.proppatch(url).body.should == 'PROPPATCH'
+ hr.mkcol(url).body.should == 'MKCOL'
+ hr.options(url).body.should == nil
+ hr.head(url).body.should == nil
+ end
+
+ end
+
+ context "some basic requests with parameter" do
+
+ it "should work with the get method" do
+ hr.get(URL + '/get').body.should ==({}.inspect)
+ hr.get(URL + '/get?&').body.should ==({}.inspect)
+ hr.get(URL + '/get?&#').body.should ==({}.inspect)
+ hr.get(URL + '/get?abc=').body.should ==({'abc' => ''}.inspect)
+
+ hr.get(URL + '/get?lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
+ hr.get(URL + '/get?lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
+
+ hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
+ hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
+
+ hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.get(URL + '/get?ids[]=1&ids[]=2').body.should ==({
+ 'ids' => ['1', '2']
+ }.inspect)
+
+ hr.get(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should ==({
+ 'ids' => ['1', '2']
+ }.inspect)
+
+ hr.get(URL + '/get?ids[a]=1&ids[b]=2').body.should ==({
+ 'ids' => {'a' => '1', 'b' => '2'}
+ }.inspect)
+
+ hr.get(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should ==({
+ 'ids' => {'a' => '1', 'b' => '2'}
+ }.inspect)
+
+ hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"ids"=>{')
+ hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"a"=>"1"')
+ hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"b"=>"2"')
+ end
+
+ it "should work with the post method" do
+ hr.post(URL + '/get').body.should ==({}.inspect)
+ hr.post(URL + '/get?&').body.should ==({}.inspect)
+ hr.post(URL + '/get?&#').body.should ==({}.inspect)
+ hr.post(URL + '/get?abc=').body.should ==({'abc' => ''}.inspect)
+
+ hr.post(URL + '/get?lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
+ hr.post(URL + '/get?lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
+
+ hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
+ hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
+
+ hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
+ hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
+
+ hr.post(URL + '/get?ids[]=1&ids[]=2').body.should ==({
+ 'ids' => ['1', '2']
+ }.inspect)
+
+ hr.post(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should ==({
+ 'ids' => ['1', '2']
+ }.inspect)
+
+ hr.post(URL + '/get?ids[a]=1&ids[b]=2').body.should ==({
+ 'ids' => {'a' => '1', 'b' => '2'}
+ }.inspect)
+
+ hr.post(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should ==({
+ 'ids' => {'a' => '1', 'b' => '2'}
+ }.inspect)
+
+ hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"ids"=>{')
+ hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"a"=>"1"')
+ hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"b"=>"2"')
+ end
+
+ end
+
+ context "http auth" do
+ it "supports the Basic Auth" do
+ hr.get("http://zhou:password@localhost:9527/auth/basic").body.should == "success!"
+ hr.get("http://localhost:9527/auth/basic").body.should == ""
+
+ hr.get(
+ :url => "http://zhou:password@localhost:9527/auth/basic",
+ :auth => :basic
+ ).body.should == "success!"
+
+ hr.get(
+ :url => "http://localhost:9527/auth/basic",
+ :auth_username => 'zhou',
+ :auth_password => 'password',
+ :auth => :basic
+ ).body.should == "success!"
+
+ hr.get(
+ :url => "http://localhost:9527/auth/basic",
+ :auth => {
+ :password => 'password',
+ :username => 'zhou',
+ :type => :basic
+ }
+ ).body.should == "success!"
+
+ hr.get(
+ :url => "http://localhost:9527/auth/basic",
+ :auth => {
+ :password => 'password',
+ :username => 'zhou'
+ }
+ ).body.should == "success!"
+ end
+
+ it "supports the Digest Auth" do
+ hr.get(
+ :url => "http://zhou:password@localhost:9527/auth/digest",
+ :auth => :digest
+ ).body.should == "success!"
+
+ hr.get(
+ :url => "http://localhost:9527/auth/digest",
+ :auth_username => 'zhou',
+ :auth_password => 'password',
+ :auth => :digest
+ ).body.should == "success!"
+
+ hr.get(
+ :url => "http://localhost:9527/auth/digest",
+ :auth => {
+ :password => 'password',
+ :username => 'zhou',
+ :type => :digest
+ }
+ ).body.should == "success!"
+ end
+
+ end
+
+ context 'Session and Cookie' do
+ it "can work with session" do
+ h = hr.get(URL + "/session")
+ h.body.should == "1"
+
+ h = hr.get(:url => URL + "/session", :cookies => h.cookies)
+ h.body.should == "2"
+
+ h = hr.get(:url => URL + "/session", :cookies => h.cookies)
+ h.body.should == "3"
+
+ h1 = hr.get(URL + "/session")
+ h1.body.should == "1"
+
+ h1 = hr.get(:url => URL + "/session", :cookies => h1.cookies)
+ h1.body.should == "2"
+
+ h2 = hr.get(URL + "/session")
+ h2.body.should == "1"
+
+ h2 = hr.get(:url => URL + "/session", :cookies => h2)
+ h2.body.should == "2"
+
+ h = hr.get(URL + "/session/1")
+ h.body.should == "/session/1:/session/2"
+
+ h = hr.get(:url => URL + "/session/1", :redirect => false)
+ h.code_3xx?.should == true
+ end
+
+ it "can work with cookies" do
+ h = hr.get(URL + "/cookie")
+ h.cookies['name'].should == 'zhou'
+ end
+ end
+
+ context 'upload file' do
+ it 'can upload 1 file' do
+ files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
+ h = hr.post(:url => URL + '/upload_file', :files => files)
+ h.body.should == 'hi.txt - hi'
+ end
+
+ it 'can upload 1 file with parameters' do
+ files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
+ h = hr.post(:url => URL + '/upload_file', :files => files, :parameters => {:name => 'Ruby'})
+ h.body.should == 'hi.txt - hi' + {'name' => 'Ruby'}.inspect
+ end
+
+ it 'can upload 1 file with parameters and query string' do
+ files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
+ h = hr.post(:url => URL + '/upload_file?version=1.9', :files => files, :parameters => {:name => 'Ruby'})
+ h.body.should include('1.9')
+ h.body.should include('version')
+ h.body.should include('name')
+ h.body.should include('Ruby')
+ h.body.should include('hi.txt - hi')
+ end
+
+ it 'can upload 2 files' do
+ files = [
+ {:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'},
+ {:file_name => 'ih.txt', :field_name => 'elif', :file_content => 'ih'}
+ ]
+ h = hr.post(:url => URL + '/upload_file2', :files => files)
+ h.body.should == 'hi.txt - hi, ih.txt - ih'
+ end
+ end
+
+end
View
5 test/web_server.rb → spec/web_server.rb
@@ -89,8 +89,10 @@
map '/session' do
app = lambda {|env|
+ env['rack.session'] ||= {}
env['rack.session']['counter'] ||= 0
env['rack.session']['counter'] += 1
+ File.open('/tmp/debug.txt', 'w') {|f| f.write env.inspect}
[200, {'Content-Type' => 'text/html'}, "#{env['rack.session']['counter']}"]
}
run Rack::Session::Cookie.new(app)
@@ -154,4 +156,5 @@
end
-Handler::Mongrel.run builder, :Port => 9527
+#Handler::Mongrel.run builder, :Port => 9527
+Handler::Thin.run builder, :Port => 9527
View
275 test/test_http_request.rb
@@ -1,275 +0,0 @@
-require 'test/spec'
-require File.join(File.dirname(__FILE__), '..', 'lib/http_request.rb')
-
-URL = 'http://localhost:9527'
-hr = HttpRequest
-
-context "some basic http requests" do
-
- specify "get the first page" do
- hr.get(URL) do |http|
- http.body.should.equal 'It Works!'
- http['content-type'].should.equal 'text/html'
- end
- hr.get(URL + '/').body.should.equal 'It Works!'
- end
-
- specify "post or get method" do
- hr.get(URL + "/method/post").body.should.equal 'No'
- hr.post(URL + "/method/post").body.should.equal 'Yes'
-
- hr.get(URL + "/method/get").body.should.equal 'Yes'
- hr.post(URL + "/method/get").body.should.equal 'No'
- end
-
- specify "xhr?" do
- hr.get(:url => URL + "/ajax").body.should.equal 'N'
- hr.get(:url => URL + "/ajax", :xhr => true).body.should.equal 'Y'
- hr.get(:url => URL + "/ajax", :ajax => true).body.should.equal 'Y'
-
- hr.get(URL + "/ajax", :xhr => true).body.should.equal 'Y'
- hr.get(URL + "/ajax", :ajax => true).body.should.equal 'Y'
- end
-
- specify "available http methods" do
- url = URL + '/get-method-name'
- hr.get(url).body.should.equal 'GET'
- hr.post(url).body.should.equal 'POST'
- hr.put(url).body.should.equal 'PUT'
- hr.delete(url).body.should.equal 'DELETE'
- hr.trace(url).body.should.equal 'TRACE'
- hr.lock(url).body.should.equal 'LOCK'
- hr.unlock(url).body.should.equal 'UNLOCK'
- hr.move(url).body.should.equal 'MOVE'
- hr.copy(url).body.should.equal 'COPY'
- hr.propfind(url).body.should.equal 'PROPFIND'
- hr.proppatch(url).body.should.equal 'PROPPATCH'
- hr.mkcol(url).body.should.equal 'MKCOL'
-
- hr.options(url).body.should.equal nil
- hr.options(url)['content-length'].should.equal 'options'.size.to_s
-
- hr.head(url).body.should.equal nil
- hr.head(url)['content-length'].should.equal 'head'.size.to_s
- end
-
-end
-
-context "some basic requests with parameter" do
-
- specify "get method" do
- hr.get(URL + '/get').body.should.equal({}.inspect)
- hr.get(URL + '/get?&').body.should.equal({}.inspect)
- hr.get(URL + '/get?&#').body.should.equal({}.inspect)
- hr.get(URL + '/get?abc=').body.should.equal({'abc' => ''}.inspect)
-
- hr.get(URL + '/get?lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
- hr.get(URL + '/get?lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
-
- hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
- hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
-
- hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.get(URL + '/get?ids[]=1&ids[]=2').body.should.equal({
- 'ids' => ['1', '2']
- }.inspect)
-
- hr.get(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should.equal({
- 'ids' => ['1', '2']
- }.inspect)
-
- hr.get(URL + '/get?ids[a]=1&ids[b]=2').body.should.equal({
- 'ids' => {'a' => '1', 'b' => '2'}
- }.inspect)
-
- hr.get(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should.equal({
- 'ids' => {'a' => '1', 'b' => '2'}
- }.inspect)
-
- hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"ids"=>{')
- hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"a"=>"1"')
- hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"b"=>"2"')
- end
-
- specify "post method" do
- hr.post(URL + '/get').body.should.equal({}.inspect)
- hr.post(URL + '/get?&').body.should.equal({}.inspect)
- hr.post(URL + '/get?&#').body.should.equal({}.inspect)
- hr.post(URL + '/get?abc=').body.should.equal({'abc' => ''}.inspect)
-
- hr.post(URL + '/get?lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
- hr.post(URL + '/get?lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
-
- hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
- hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
-
- hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
- hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
-
- hr.post(URL + '/get?ids[]=1&ids[]=2').body.should.equal({
- 'ids' => ['1', '2']
- }.inspect)
-
- hr.post(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should.equal({
- 'ids' => ['1', '2']
- }.inspect)
-
- hr.post(URL + '/get?ids[a]=1&ids[b]=2').body.should.equal({
- 'ids' => {'a' => '1', 'b' => '2'}
- }.inspect)
-
- hr.post(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should.equal({
- 'ids' => {'a' => '1', 'b' => '2'}
- }.inspect)
-
- hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"ids"=>{')
- hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"a"=>"1"')
- hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"b"=>"2"')
- end
-
-end
-
-context "http auth" do
- specify "Basic Auth" do
- hr.get("http://zhou:password@localhost:9527/auth/basic").body.should.equal "success!"
- hr.get("http://localhost:9527/auth/basic").body.should.equal ""
-
- hr.get(
- :url => "http://zhou:password@localhost:9527/auth/basic",
- :auth => :basic
- ).body.should.equal "success!"
-
- hr.get(
- :url => "http://localhost:9527/auth/basic",
- :auth_username => 'zhou',
- :auth_password => 'password',
- :auth => :basic
- ).body.should.equal "success!"
-
- hr.get(
- :url => "http://localhost:9527/auth/basic",
- :auth => {
- :password => 'password',
- :username => 'zhou',
- :type => :basic
- }
- ).body.should.equal "success!"
-
- hr.get(
- :url => "http://localhost:9527/auth/basic",
- :auth => {
- :password => 'password',
- :username => 'zhou'
- }
- ).body.should.equal "success!"
- end
-
- specify "Digest Auth" do
- hr.get(
- :url => "http://zhou:password@localhost:9527/auth/digest",
- :auth => :digest
- ).body.should.equal "success!"
-
- hr.get(
- :url => "http://localhost:9527/auth/digest",
- :auth_username => 'zhou',
- :auth_password => 'password',
- :auth => :digest
- ).body.should.equal "success!"
-
- hr.get(
- :url => "http://localhost:9527/auth/digest",
- :auth => {
- :password => 'password',
- :username => 'zhou',
- :type => :digest
- }
- ).body.should.equal "success!"
- end
-
-end
-
-context 'Session && Cookie' do
- specify "Session" do
- h = hr.get(URL + "/session")
- h.body.should.equal "1"
-
- h = hr.get(:url => URL + "/session", :cookies => h.cookies)
- h.body.should.equal "2"
-
- h = hr.get(:url => URL + "/session", :cookies => h.cookies)
- h.body.should.equal "3"
-
- h1 = hr.get(URL + "/session")
- h1.body.should.equal "1"
-
- h1 = hr.get(:url => URL + "/session", :cookies => h1.cookies)
- h1.body.should.equal "2"
-
- h2 = hr.get(URL + "/session")
- h2.body.should.equal "1"
-
- h2 = hr.get(:url => URL + "/session", :cookies => h2)
- h2.body.should.equal "2"
-
- h = hr.get(URL + "/session/1")
- h.body.should.equal "/session/1:/session/2"
-
- h = hr.get(:url => URL + "/session/1", :redirect => false)
- h.code_3xx?.should.equal true
- end
-
- specify "Cookie" do
- h = hr.get(URL + "/cookie")
- h.cookies['name'].should.equal 'zhou'
- end
-end
-
-context 'upload file' do
- specify 'upload file' do
- files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
- h = hr.post(:url => URL + '/upload_file', :files => files)
- h.body.should.equal 'hi.txt - hi'
- end
-
- specify 'upload file with parameters' do
- files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
- h = hr.post(:url => URL + '/upload_file', :files => files, :parameters => {:name => 'Ruby'})
- h.body.should.equal 'hi.txt - hi' + {'name' => 'Ruby'}.inspect
- end
-
- specify 'upload file with parameters and query string' do
- files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
- h = hr.post(:url => URL + '/upload_file?version=1.9', :files => files, :parameters => {:name => 'Ruby'})
- h.body.should.equal 'hi.txt - hi' + {'name' => 'Ruby', 'version' => '1.9'}.inspect
- end
-
- specify 'upload 2 files' do
- files = [
- {:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'},
- {:file_name => 'ih.txt', :field_name => 'elif', :file_content => 'ih'}
- ]
- h = hr.post(:url => URL + '/upload_file2', :files => files)
- h.body.should.equal 'hi.txt - hi, ih.txt - ih'
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.