http_request.rb 1.1.12 → 1.1.13

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,3 +1,12 @@
1
+ v1.1.13
2
+ * fixed cookies parser
3
+ * join utf-8 and ascii-8 bit form data
4
+ * testing with rspec now
5
+ * Gemfile added
6
+
7
+ v1.1.12
8
+ * post method supports query string
9
+
1
10
  v1.1.11
2
11
  * define http methods such as "get", "post" etc. dynamically instead of
3
12
  using "method_missing".
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ group :test do
2
+ gem 'rspec'
3
+ gem 'thin'
4
+ end
data/README.rdoc CHANGED
@@ -58,8 +58,8 @@ fetch headers
58
58
  }
59
59
 
60
60
  fetch cookies
61
- hp = HttpRequest.get('http://www.yahoo.com')
62
- hp.cookies.each {|k, v|
61
+ hr = HttpRequest.get('http://www.yahoo.com')
62
+ hr.cookies.each {|k, v|
63
63
  puts "#{k} => #{v}"
64
64
  }
65
65
 
@@ -67,6 +67,19 @@ add cookies into header
67
67
  HttpRequest.get(:url => 'http://www.example.com/', :cookies => {:login => 'Yes', :userid => 101})
68
68
  HttpRequest.get(:url => 'http://www.example.com/', :cookies => 'login=Yes; userId=101')
69
69
 
70
+ fetch cookies and add cookies
71
+ hr = HttpRequest.get('http://www.example.com/homepage')
72
+ hr = HttpRequest.post('http://www.example.com/login', :cookies => hr.cookies)
73
+ hr = HttpRequest.get('http://www.example.com/logout', :cookies => hr.cookies)
74
+
75
+ store cookies with cookie jar (since v1.1.13)
76
+ # default :cookie_jar is "default"
77
+ hr1 = HttpRequest.post('http://hostname/request-cookies', :cookie_jar => 'user1')
78
+ hr2 = HttpRequest.post('http://hostname/request-cookies', :cookie_jar => 'user2')
79
+
80
+ hr1 = HttpRequest.post('http://hostname/send-cookies', :cookie_jar => 'user1', :cookies => hr1.cookies)
81
+ hr2 = HttpRequest.post('http://hostname/send-cookies', :cookie_jar => 'user2', :cookies => hr2.cookies)
82
+
70
83
  upload file by post method
71
84
  HttpRequest.post(
72
85
  :url => 'http://localhost/upload.php',
@@ -228,4 +241,4 @@ download multiple files from a directory
228
241
  bug fixing, testing and testing...
229
242
 
230
243
  == LATEST VERSION
231
- 1.1.12
244
+ 1.1.13
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc 'run spec'
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = './spec/http_request_spec.rb'
6
+ end
data/lib/http_request.rb CHANGED
@@ -11,9 +11,9 @@
11
11
  #
12
12
  # == Version
13
13
  #
14
- # v1.1.12
14
+ # v1.1.13
15
15
  #
16
- # Last Change: 21 May, 2012
16
+ # Last Change: 29 July, 2012
17
17
  #
18
18
  # == Author
19
19
  #
@@ -32,7 +32,7 @@ class HttpRequest
32
32
  include Singleton
33
33
  class << self
34
34
  # version
35
- VERSION = '1.1.12'.freeze
35
+ VERSION = '1.1.13'.freeze
36
36
  def version;VERSION;end
37
37
 
38
38
  # available http methods
@@ -51,13 +51,13 @@ class HttpRequest
51
51
  return unless response.header['set-cookie']
52
52
  response.get_fields('set-cookie').each {|k|
53
53
  k, v = k.split(';')[0].split('=')
54
- @@__cookies[k] = v
54
+ @@__cookies[@@__cookie_jar][k] = CGI.unescape(v)
55
55
  }
56
56
  end
57
57
 
58
58
  # return cookies
59
59
  def cookies
60
- @@__cookies
60
+ @@__cookies[@@__cookie_jar]
61
61
  end
62
62
 
63
63
  # check the http resource whether or not available
@@ -65,7 +65,7 @@ class HttpRequest
65
65
  timeout(timeout) {
66
66
  u = URI(url)
67
67
  s = TCPSocket.new(u.host, u.port)
68
- s.close
68
+ s.cspecify lose
69
69
  }
70
70
  return true
71
71
  rescue Exception => e
@@ -81,10 +81,21 @@ class HttpRequest
81
81
  end
82
82
 
83
83
  # we need to retrieve the cookies from last http response before reset cookies if it's a Net::HTTPResponse
84
- options[:cookies] = options[:cookies].cookies if options.is_a?(Hash) and options[:cookies].is_a?(Net::HTTPResponse)
84
+ # and make sure we have a cookie_jar to store the cookies
85
+ cookie_jar = 'default' # default name of cookie jar
86
+ if options.is_a?(Hash)
87
+ options[:cookies] = options[:cookies].cookies if options[:cookies].is_a?(Net::HTTPResponse)
88
+ if options[:cookie_jar].is_a? String
89
+ cookie_jar = options[:cookie_jar]
90
+ else
91
+ options[:cookie_jar] = cookie_jar
92
+ end
93
+ end
85
94
 
86
95
  # reset
87
- @@__cookies = {}
96
+ @@__cookie_jar = cookie_jar
97
+ @@__cookies = {} unless defined? @@__cookies
98
+ @@__cookies[cookie_jar] = {} if @@__cookies[cookie_jar].nil?
88
99
  @@redirect_times = 0
89
100
  self.instance.request(method_name, options, &block)
90
101
  end
@@ -262,7 +273,7 @@ class HttpRequest
262
273
  @uri = URI(@options[:url])
263
274
  @uri.path = '/' if @uri.path.empty?
264
275
  @headers = {
265
- 'Host' => @uri.host,
276
+ 'Host' => "#{@uri.host}:#{@uri.port}",
266
277
  'Referer' => @options[:url],
267
278
  'User-Agent' => 'HttpRequest.rb ' + self.class.version
268
279
  }
@@ -293,6 +304,7 @@ class HttpRequest
293
304
  end
294
305
  @headers['Cookie'] = cookies unless cookies.empty?
295
306
  end
307
+ @headers['Connection'] = 'keep-alive'
296
308
  end
297
309
 
298
310
  # parse parameters for the options[:parameters] and @uri.query
@@ -339,6 +351,9 @@ class HttpRequest
339
351
  multipart << "\r\n#{f[:file_content]}"
340
352
  }
341
353
  multipart << "--#{boundary}--"
354
+ multipart.each_with_index do |val, key|
355
+ multipart[key] = val.force_encoding('UTF-8')
356
+ end
342
357
  multipart = multipart.join("\r\n")
343
358
  @headers['Content-length'] = "#{multipart.size}"
344
359
  @options[:parameters] = multipart
@@ -389,7 +404,7 @@ class HttpRequest
389
404
  else
390
405
  @uri.path + @options[:parameters]
391
406
  end
392
- h = http.method(@options[:method]).call(path, @headers)
407
+ h = http.method(@options[:method]).call(path, @headers)
393
408
  else
394
409
  h = http.method(@options[:method]).call("#{@uri.path}?#{@uri.query}", @options[:parameters], @headers)
395
410
  end
@@ -0,0 +1,291 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'http_request.rb')
2
+
3
+ URL = 'http://localhost:9527'
4
+ hr = HttpRequest
5
+
6
+ describe HttpRequest do
7
+
8
+ before :all do
9
+ Thread.new do |t|
10
+ web_server = File.join(File.expand_path(File.dirname(__FILE__)), 'web_server.rb')
11
+ @process_id = spawn "ruby #{web_server}", :in => "/dev/null", :out => "/dev/null"
12
+ sleep 2
13
+ end.join
14
+ end
15
+
16
+ after :all do
17
+ Process.kill 'KILL', @process_id
18
+ Process.wait
19
+ end
20
+
21
+ context "some basic http requests" do
22
+
23
+ it "can get the first page" do
24
+ hr.get(URL) do |http|
25
+ http.body.should == 'It Works!'
26
+ http['content-type'].should == 'text/html'
27
+ end
28
+ hr.get(URL + '/').body.should == 'It Works!'
29
+ end
30
+
31
+ it "has post or get method" do
32
+ hr.get(URL + "/method/post").body.should == 'No'
33
+ hr.post(URL + "/method/post").body.should == 'Yes'
34
+
35
+ hr.get(URL + "/method/get").body.should == 'Yes'
36
+ hr.post(URL + "/method/get").body.should == 'No'
37
+ end
38
+
39
+ it "can send AJAX requests" do
40
+ hr.get(:url => URL + "/ajax").body.should == 'N'
41
+ hr.get(:url => URL + "/ajax", :xhr => true).body.should == 'Y'
42
+ hr.get(:url => URL + "/ajax", :ajax => true).body.should == 'Y'
43
+
44
+ hr.get(URL + "/ajax", :xhr => true).body.should == 'Y'
45
+ hr.get(URL + "/ajax", :ajax => true).body.should == 'Y'
46
+ end
47
+
48
+ it "supports the following methods" do
49
+ url = URL + '/get-method-name'
50
+ hr.get(url).body.should == 'GET'
51
+ hr.post(url).body.should == 'POST'
52
+ hr.put(url).body.should == 'PUT'
53
+ hr.delete(url).body.should == 'DELETE'
54
+ hr.trace(url).body.should == 'TRACE'
55
+ hr.lock(url).body.should == 'LOCK'
56
+ hr.unlock(url).body.should == 'UNLOCK'
57
+ hr.move(url).body.should == 'MOVE'
58
+ hr.copy(url).body.should == 'COPY'
59
+ hr.propfind(url).body.should == 'PROPFIND'
60
+ hr.proppatch(url).body.should == 'PROPPATCH'
61
+ hr.mkcol(url).body.should == 'MKCOL'
62
+ hr.options(url).body.should == nil
63
+ hr.head(url).body.should == nil
64
+ end
65
+
66
+ end
67
+
68
+ context "some basic requests with parameter" do
69
+
70
+ it "should work with the get method" do
71
+ hr.get(URL + '/get').body.should ==({}.inspect)
72
+ hr.get(URL + '/get?&').body.should ==({}.inspect)
73
+ hr.get(URL + '/get?&#').body.should ==({}.inspect)
74
+ hr.get(URL + '/get?abc=').body.should ==({'abc' => ''}.inspect)
75
+
76
+ hr.get(URL + '/get?lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
77
+ hr.get(URL + '/get?lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
78
+
79
+ hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
80
+ hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
81
+
82
+ hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"lang"=>"Ruby"')
83
+ hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"version"=>"1.9"')
84
+
85
+ hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"lang"=>"Ruby"')
86
+ hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"version"=>"1.9"')
87
+
88
+ hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
89
+ hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
90
+
91
+ hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
92
+ hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
93
+
94
+ hr.get(URL + '/get?ids[]=1&ids[]=2').body.should ==({
95
+ 'ids' => ['1', '2']
96
+ }.inspect)
97
+
98
+ hr.get(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should ==({
99
+ 'ids' => ['1', '2']
100
+ }.inspect)
101
+
102
+ hr.get(URL + '/get?ids[a]=1&ids[b]=2').body.should ==({
103
+ 'ids' => {'a' => '1', 'b' => '2'}
104
+ }.inspect)
105
+
106
+ hr.get(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should ==({
107
+ 'ids' => {'a' => '1', 'b' => '2'}
108
+ }.inspect)
109
+
110
+ hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"ids"=>{')
111
+ hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"a"=>"1"')
112
+ hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"b"=>"2"')
113
+ end
114
+
115
+ it "should work with the post method" do
116
+ hr.post(URL + '/get').body.should ==({}.inspect)
117
+ hr.post(URL + '/get?&').body.should ==({}.inspect)
118
+ hr.post(URL + '/get?&#').body.should ==({}.inspect)
119
+ hr.post(URL + '/get?abc=').body.should ==({'abc' => ''}.inspect)
120
+
121
+ hr.post(URL + '/get?lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
122
+ hr.post(URL + '/get?lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
123
+
124
+ hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"lang"=>"Ruby"')
125
+ hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should include('"version"=>"1.9"')
126
+
127
+ hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"lang"=>"Ruby"')
128
+ hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should include('"version"=>"1.9"')
129
+
130
+ hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"lang"=>"Ruby"')
131
+ hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should include('"version"=>"1.9"')
132
+
133
+ hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
134
+ hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
135
+
136
+ hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"lang"=>"Ruby"')
137
+ hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should include('"version"=>"1.9"')
138
+
139
+ hr.post(URL + '/get?ids[]=1&ids[]=2').body.should ==({
140
+ 'ids' => ['1', '2']
141
+ }.inspect)
142
+
143
+ hr.post(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should ==({
144
+ 'ids' => ['1', '2']
145
+ }.inspect)
146
+
147
+ hr.post(URL + '/get?ids[a]=1&ids[b]=2').body.should ==({
148
+ 'ids' => {'a' => '1', 'b' => '2'}
149
+ }.inspect)
150
+
151
+ hr.post(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should ==({
152
+ 'ids' => {'a' => '1', 'b' => '2'}
153
+ }.inspect)
154
+
155
+ hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"ids"=>{')
156
+ hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"a"=>"1"')
157
+ hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should include('"b"=>"2"')
158
+ end
159
+
160
+ end
161
+
162
+ context "http auth" do
163
+ it "supports the Basic Auth" do
164
+ hr.get("http://zhou:password@localhost:9527/auth/basic").body.should == "success!"
165
+ hr.get("http://localhost:9527/auth/basic").body.should == ""
166
+
167
+ hr.get(
168
+ :url => "http://zhou:password@localhost:9527/auth/basic",
169
+ :auth => :basic
170
+ ).body.should == "success!"
171
+
172
+ hr.get(
173
+ :url => "http://localhost:9527/auth/basic",
174
+ :auth_username => 'zhou',
175
+ :auth_password => 'password',
176
+ :auth => :basic
177
+ ).body.should == "success!"
178
+
179
+ hr.get(
180
+ :url => "http://localhost:9527/auth/basic",
181
+ :auth => {
182
+ :password => 'password',
183
+ :username => 'zhou',
184
+ :type => :basic
185
+ }
186
+ ).body.should == "success!"
187
+
188
+ hr.get(
189
+ :url => "http://localhost:9527/auth/basic",
190
+ :auth => {
191
+ :password => 'password',
192
+ :username => 'zhou'
193
+ }
194
+ ).body.should == "success!"
195
+ end
196
+
197
+ it "supports the Digest Auth" do
198
+ hr.get(
199
+ :url => "http://zhou:password@localhost:9527/auth/digest",
200
+ :auth => :digest
201
+ ).body.should == "success!"
202
+
203
+ hr.get(
204
+ :url => "http://localhost:9527/auth/digest",
205
+ :auth_username => 'zhou',
206
+ :auth_password => 'password',
207
+ :auth => :digest
208
+ ).body.should == "success!"
209
+
210
+ hr.get(
211
+ :url => "http://localhost:9527/auth/digest",
212
+ :auth => {
213
+ :password => 'password',
214
+ :username => 'zhou',
215
+ :type => :digest
216
+ }
217
+ ).body.should == "success!"
218
+ end
219
+
220
+ end
221
+
222
+ context 'Session and Cookie' do
223
+ it "can work with session" do
224
+ h = hr.get(URL + "/session")
225
+ h.body.should == "1"
226
+
227
+ h = hr.get(:url => URL + "/session", :cookies => h.cookies)
228
+ h.body.should == "2"
229
+
230
+ h = hr.get(:url => URL + "/session", :cookies => h.cookies)
231
+ h.body.should == "3"
232
+
233
+ h1 = hr.get(URL + "/session")
234
+ h1.body.should == "1"
235
+
236
+ h1 = hr.get(:url => URL + "/session", :cookies => h1.cookies)
237
+ h1.body.should == "2"
238
+
239
+ h2 = hr.get(URL + "/session")
240
+ h2.body.should == "1"
241
+
242
+ h2 = hr.get(:url => URL + "/session", :cookies => h2)
243
+ h2.body.should == "2"
244
+
245
+ h = hr.get(URL + "/session/1")
246
+ h.body.should == "/session/1:/session/2"
247
+
248
+ h = hr.get(:url => URL + "/session/1", :redirect => false)
249
+ h.code_3xx?.should == true
250
+ end
251
+
252
+ it "can work with cookies" do
253
+ h = hr.get(URL + "/cookie")
254
+ h.cookies['name'].should == 'zhou'
255
+ end
256
+ end
257
+
258
+ context 'upload file' do
259
+ it 'can upload 1 file' do
260
+ files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
261
+ h = hr.post(:url => URL + '/upload_file', :files => files)
262
+ h.body.should == 'hi.txt - hi'
263
+ end
264
+
265
+ it 'can upload 1 file with parameters' do
266
+ files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
267
+ h = hr.post(:url => URL + '/upload_file', :files => files, :parameters => {:name => 'Ruby'})
268
+ h.body.should == 'hi.txt - hi' + {'name' => 'Ruby'}.inspect
269
+ end
270
+
271
+ it 'can upload 1 file with parameters and query string' do
272
+ files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
273
+ h = hr.post(:url => URL + '/upload_file?version=1.9', :files => files, :parameters => {:name => 'Ruby'})
274
+ h.body.should include('1.9')
275
+ h.body.should include('version')
276
+ h.body.should include('name')
277
+ h.body.should include('Ruby')
278
+ h.body.should include('hi.txt - hi')
279
+ end
280
+
281
+ it 'can upload 2 files' do
282
+ files = [
283
+ {:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'},
284
+ {:file_name => 'ih.txt', :field_name => 'elif', :file_content => 'ih'}
285
+ ]
286
+ h = hr.post(:url => URL + '/upload_file2', :files => files)
287
+ h.body.should == 'hi.txt - hi, ih.txt - ih'
288
+ end
289
+ end
290
+
291
+ end
@@ -89,8 +89,10 @@ builder = Builder.new do
89
89
 
90
90
  map '/session' do
91
91
  app = lambda {|env|
92
+ env['rack.session'] ||= {}
92
93
  env['rack.session']['counter'] ||= 0
93
94
  env['rack.session']['counter'] += 1
95
+ File.open('/tmp/debug.txt', 'w') {|f| f.write env.inspect}
94
96
  [200, {'Content-Type' => 'text/html'}, "#{env['rack.session']['counter']}"]
95
97
  }
96
98
  run Rack::Session::Cookie.new(app)
@@ -154,4 +156,5 @@ builder = Builder.new do
154
156
 
155
157
  end
156
158
 
157
- Handler::Mongrel.run builder, :Port => 9527
159
+ #Handler::Mongrel.run builder, :Port => 9527
160
+ Handler::Thin.run builder, :Port => 9527
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: http_request.rb
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.12
5
+ version: 1.1.13
6
6
  platform: ruby
7
7
  authors:
8
8
  - xianhua.zhou
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-05-21 00:00:00 Z
13
+ date: 2012-07-29 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description:
@@ -24,9 +24,11 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - Changelog
26
26
  - README.rdoc
27
+ - Gemfile
28
+ - Rakefile
27
29
  - lib/http_request.rb
28
- - test/test_http_request.rb
29
- - test/web_server.rb
30
+ - spec/http_request_spec.rb
31
+ - spec/web_server.rb
30
32
  homepage: http://github.com/xianhuazhou
31
33
  licenses: []
32
34
 
@@ -1,275 +0,0 @@
1
- require 'test/spec'
2
- require File.join(File.dirname(__FILE__), '..', 'lib/http_request.rb')
3
-
4
- URL = 'http://localhost:9527'
5
- hr = HttpRequest
6
-
7
- context "some basic http requests" do
8
-
9
- specify "get the first page" do
10
- hr.get(URL) do |http|
11
- http.body.should.equal 'It Works!'
12
- http['content-type'].should.equal 'text/html'
13
- end
14
- hr.get(URL + '/').body.should.equal 'It Works!'
15
- end
16
-
17
- specify "post or get method" do
18
- hr.get(URL + "/method/post").body.should.equal 'No'
19
- hr.post(URL + "/method/post").body.should.equal 'Yes'
20
-
21
- hr.get(URL + "/method/get").body.should.equal 'Yes'
22
- hr.post(URL + "/method/get").body.should.equal 'No'
23
- end
24
-
25
- specify "xhr?" do
26
- hr.get(:url => URL + "/ajax").body.should.equal 'N'
27
- hr.get(:url => URL + "/ajax", :xhr => true).body.should.equal 'Y'
28
- hr.get(:url => URL + "/ajax", :ajax => true).body.should.equal 'Y'
29
-
30
- hr.get(URL + "/ajax", :xhr => true).body.should.equal 'Y'
31
- hr.get(URL + "/ajax", :ajax => true).body.should.equal 'Y'
32
- end
33
-
34
- specify "available http methods" do
35
- url = URL + '/get-method-name'
36
- hr.get(url).body.should.equal 'GET'
37
- hr.post(url).body.should.equal 'POST'
38
- hr.put(url).body.should.equal 'PUT'
39
- hr.delete(url).body.should.equal 'DELETE'
40
- hr.trace(url).body.should.equal 'TRACE'
41
- hr.lock(url).body.should.equal 'LOCK'
42
- hr.unlock(url).body.should.equal 'UNLOCK'
43
- hr.move(url).body.should.equal 'MOVE'
44
- hr.copy(url).body.should.equal 'COPY'
45
- hr.propfind(url).body.should.equal 'PROPFIND'
46
- hr.proppatch(url).body.should.equal 'PROPPATCH'
47
- hr.mkcol(url).body.should.equal 'MKCOL'
48
-
49
- hr.options(url).body.should.equal nil
50
- hr.options(url)['content-length'].should.equal 'options'.size.to_s
51
-
52
- hr.head(url).body.should.equal nil
53
- hr.head(url)['content-length'].should.equal 'head'.size.to_s
54
- end
55
-
56
- end
57
-
58
- context "some basic requests with parameter" do
59
-
60
- specify "get method" do
61
- hr.get(URL + '/get').body.should.equal({}.inspect)
62
- hr.get(URL + '/get?&').body.should.equal({}.inspect)
63
- hr.get(URL + '/get?&#').body.should.equal({}.inspect)
64
- hr.get(URL + '/get?abc=').body.should.equal({'abc' => ''}.inspect)
65
-
66
- hr.get(URL + '/get?lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
67
- hr.get(URL + '/get?lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
68
-
69
- hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
70
- hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
71
-
72
- hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"lang"=>"Ruby"')
73
- hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"version"=>"1.9"')
74
-
75
- hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"lang"=>"Ruby"')
76
- hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"version"=>"1.9"')
77
-
78
- hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
79
- hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
80
-
81
- hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
82
- hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
83
-
84
- hr.get(URL + '/get?ids[]=1&ids[]=2').body.should.equal({
85
- 'ids' => ['1', '2']
86
- }.inspect)
87
-
88
- hr.get(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should.equal({
89
- 'ids' => ['1', '2']
90
- }.inspect)
91
-
92
- hr.get(URL + '/get?ids[a]=1&ids[b]=2').body.should.equal({
93
- 'ids' => {'a' => '1', 'b' => '2'}
94
- }.inspect)
95
-
96
- hr.get(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should.equal({
97
- 'ids' => {'a' => '1', 'b' => '2'}
98
- }.inspect)
99
-
100
- hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"ids"=>{')
101
- hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"a"=>"1"')
102
- hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"b"=>"2"')
103
- end
104
-
105
- specify "post method" do
106
- hr.post(URL + '/get').body.should.equal({}.inspect)
107
- hr.post(URL + '/get?&').body.should.equal({}.inspect)
108
- hr.post(URL + '/get?&#').body.should.equal({}.inspect)
109
- hr.post(URL + '/get?abc=').body.should.equal({'abc' => ''}.inspect)
110
-
111
- hr.post(URL + '/get?lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
112
- hr.post(URL + '/get?lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
113
-
114
- hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"lang"=>"Ruby"')
115
- hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.include('"version"=>"1.9"')
116
-
117
- hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"lang"=>"Ruby"')
118
- hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => '1.9'}).body.should.include('"version"=>"1.9"')
119
-
120
- hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"lang"=>"Ruby"')
121
- hr.post(:url => URL + '/get?lang=Ruby', :parameters => {:version => '1.9'}).body.should.include('"version"=>"1.9"')
122
-
123
- hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
124
- hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
125
-
126
- hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"lang"=>"Ruby"')
127
- hr.post(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.include('"version"=>"1.9"')
128
-
129
- hr.post(URL + '/get?ids[]=1&ids[]=2').body.should.equal({
130
- 'ids' => ['1', '2']
131
- }.inspect)
132
-
133
- hr.post(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should.equal({
134
- 'ids' => ['1', '2']
135
- }.inspect)
136
-
137
- hr.post(URL + '/get?ids[a]=1&ids[b]=2').body.should.equal({
138
- 'ids' => {'a' => '1', 'b' => '2'}
139
- }.inspect)
140
-
141
- hr.post(:url => URL + '/get', :parameters => 'ids[a]=1&ids[b]=2').body.should.equal({
142
- 'ids' => {'a' => '1', 'b' => '2'}
143
- }.inspect)
144
-
145
- hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"ids"=>{')
146
- hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"a"=>"1"')
147
- hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.include('"b"=>"2"')
148
- end
149
-
150
- end
151
-
152
- context "http auth" do
153
- specify "Basic Auth" do
154
- hr.get("http://zhou:password@localhost:9527/auth/basic").body.should.equal "success!"
155
- hr.get("http://localhost:9527/auth/basic").body.should.equal ""
156
-
157
- hr.get(
158
- :url => "http://zhou:password@localhost:9527/auth/basic",
159
- :auth => :basic
160
- ).body.should.equal "success!"
161
-
162
- hr.get(
163
- :url => "http://localhost:9527/auth/basic",
164
- :auth_username => 'zhou',
165
- :auth_password => 'password',
166
- :auth => :basic
167
- ).body.should.equal "success!"
168
-
169
- hr.get(
170
- :url => "http://localhost:9527/auth/basic",
171
- :auth => {
172
- :password => 'password',
173
- :username => 'zhou',
174
- :type => :basic
175
- }
176
- ).body.should.equal "success!"
177
-
178
- hr.get(
179
- :url => "http://localhost:9527/auth/basic",
180
- :auth => {
181
- :password => 'password',
182
- :username => 'zhou'
183
- }
184
- ).body.should.equal "success!"
185
- end
186
-
187
- specify "Digest Auth" do
188
- hr.get(
189
- :url => "http://zhou:password@localhost:9527/auth/digest",
190
- :auth => :digest
191
- ).body.should.equal "success!"
192
-
193
- hr.get(
194
- :url => "http://localhost:9527/auth/digest",
195
- :auth_username => 'zhou',
196
- :auth_password => 'password',
197
- :auth => :digest
198
- ).body.should.equal "success!"
199
-
200
- hr.get(
201
- :url => "http://localhost:9527/auth/digest",
202
- :auth => {
203
- :password => 'password',
204
- :username => 'zhou',
205
- :type => :digest
206
- }
207
- ).body.should.equal "success!"
208
- end
209
-
210
- end
211
-
212
- context 'Session && Cookie' do
213
- specify "Session" do
214
- h = hr.get(URL + "/session")
215
- h.body.should.equal "1"
216
-
217
- h = hr.get(:url => URL + "/session", :cookies => h.cookies)
218
- h.body.should.equal "2"
219
-
220
- h = hr.get(:url => URL + "/session", :cookies => h.cookies)
221
- h.body.should.equal "3"
222
-
223
- h1 = hr.get(URL + "/session")
224
- h1.body.should.equal "1"
225
-
226
- h1 = hr.get(:url => URL + "/session", :cookies => h1.cookies)
227
- h1.body.should.equal "2"
228
-
229
- h2 = hr.get(URL + "/session")
230
- h2.body.should.equal "1"
231
-
232
- h2 = hr.get(:url => URL + "/session", :cookies => h2)
233
- h2.body.should.equal "2"
234
-
235
- h = hr.get(URL + "/session/1")
236
- h.body.should.equal "/session/1:/session/2"
237
-
238
- h = hr.get(:url => URL + "/session/1", :redirect => false)
239
- h.code_3xx?.should.equal true
240
- end
241
-
242
- specify "Cookie" do
243
- h = hr.get(URL + "/cookie")
244
- h.cookies['name'].should.equal 'zhou'
245
- end
246
- end
247
-
248
- context 'upload file' do
249
- specify 'upload file' do
250
- files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
251
- h = hr.post(:url => URL + '/upload_file', :files => files)
252
- h.body.should.equal 'hi.txt - hi'
253
- end
254
-
255
- specify 'upload file with parameters' do
256
- files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
257
- h = hr.post(:url => URL + '/upload_file', :files => files, :parameters => {:name => 'Ruby'})
258
- h.body.should.equal 'hi.txt - hi' + {'name' => 'Ruby'}.inspect
259
- end
260
-
261
- specify 'upload file with parameters and query string' do
262
- files = [{:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'}]
263
- h = hr.post(:url => URL + '/upload_file?version=1.9', :files => files, :parameters => {:name => 'Ruby'})
264
- h.body.should.equal 'hi.txt - hi' + {'name' => 'Ruby', 'version' => '1.9'}.inspect
265
- end
266
-
267
- specify 'upload 2 files' do
268
- files = [
269
- {:file_name => 'hi.txt', :field_name => 'file', :file_content => 'hi'},
270
- {:file_name => 'ih.txt', :field_name => 'elif', :file_content => 'ih'}
271
- ]
272
- h = hr.post(:url => URL + '/upload_file2', :files => files)
273
- h.body.should.equal 'hi.txt - hi, ih.txt - ih'
274
- end
275
- end