http_request.rb 1.1.7 → 1.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,4 +1,7 @@
1
- v1.1.6
1
+ v1.1.8
2
+ * fixed parameters when uploading files (it's double escaped)
3
+
4
+ v1.1.7
2
5
  * replaced X_REQUESTED_WITH with X-Requested-With for sending AJAX request
3
6
 
4
7
  v1.1.6
data/README.rdoc CHANGED
@@ -69,12 +69,12 @@ add cookies into header
69
69
  upload file by post method
70
70
  HttpRequest.post(
71
71
  :url => 'http://localhost/upload.php',
72
- :files => [{
72
+ :files => {
73
73
  :file_name => 'test.txt', # original file name, default is rand name such as 0cdex_0
74
74
  :field_name => 'user_file', # input field name, default is "files[]"
75
75
  :content_type => 'text/plain', # content type, default is application/octet-stream
76
76
  :file_content => 'Have a nice day!' # file content
77
- }]
77
+ }
78
78
  )
79
79
 
80
80
  upload more than 1 file
@@ -227,4 +227,4 @@ download multiple files from a directory
227
227
  bug fixing, testing and testing...
228
228
 
229
229
  == LATEST VERSION
230
- 1.1.7
230
+ 1.1.8
data/lib/http_request.rb CHANGED
@@ -286,7 +286,7 @@ class HttpRequest
286
286
 
287
287
  # parse parameters for the options[:parameters] and @uri.query
288
288
  def parse_parameters
289
- if @options[:parameters].is_a? Hash
289
+ if @options[:parameters].is_a?(Hash)
290
290
  @options[:parameters] = @options[:parameters].collect{|k, v|
291
291
  "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
292
292
  }.join('&')
@@ -297,7 +297,7 @@ class HttpRequest
297
297
  end
298
298
 
299
299
  # for uploading files
300
- build_multipart if @options[:files].is_a?(Array) and 'post'.eql?(@options[:method])
300
+ build_multipart if @options[:files] and 'post'.eql?(@options[:method])
301
301
  end
302
302
 
303
303
  # for uploading files
@@ -305,16 +305,17 @@ class HttpRequest
305
305
  boundary = md5(rand.to_s).to_s[0..5]
306
306
  @headers['Content-type'] = "multipart/form-data, boundary=#{boundary}"
307
307
  multipart = []
308
- if @options[:parameters]
309
- @options[:parameters] = CGI.parse(@options[:parameters]) if @options[:parameters].is_a? String
308
+ if @options[:parameters].is_a?(String)
309
+ @options[:parameters] = CGI.parse(@options[:parameters])
310
310
  if @options[:parameters].is_a? Hash
311
311
  @options[:parameters].each {|k, v|
312
312
  multipart << "--#{boundary}"
313
- multipart << "Content-disposition: form-data; name=\"#{CGI.escape(k.to_s)}\""
314
- multipart << "\r\n#{CGI.escape(v.to_s)}"
313
+ multipart << "Content-disposition: form-data; name=\"#{k}\""
314
+ multipart << "\r\n#{v.first}"
315
315
  }
316
316
  end
317
317
  end
318
+ @options[:files] = [@options[:files]] if @options[:files].is_a?(Hash)
318
319
  @options[:files].each_with_index {|f, index|
319
320
  f[:field_name] ||= "files[]"
320
321
  f[:file_name] ||= "#{boundary}_#{index}"
@@ -63,29 +63,23 @@ context "some basic requests with parameter" do
63
63
  hr.get(URL + '/get?&#').body.should.equal({}.inspect)
64
64
  hr.get(URL + '/get?abc=').body.should.equal({'abc' => ''}.inspect)
65
65
 
66
- hr.get(URL + '/get?lang=Ruby&version=1.9').body.should.equal({
67
- 'lang' => 'Ruby', 'version' => '1.9'
68
- }.inspect)
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"')
69
68
 
70
- hr.get(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.equal({
71
- 'lang' => 'Ruby', 'version' => '1.9'
72
- }.inspect)
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"')
73
71
 
74
- hr.get(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => 1.9}).body.should.equal({
75
- 'lang' => 'Ruby', 'version' => '1.9'
76
- }.inspect)
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"')
77
74
 
78
- hr.get(:url => URL + '/get?lang=Ruby', :parameters => {:version => 1.9}).body.should.equal({
79
- 'lang' => 'Ruby', 'version' => '1.9'
80
- }.inspect)
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"')
81
77
 
82
- hr.get(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.equal({
83
- 'lang' => 'Ruby', 'version' => '1.9'
84
- }.inspect)
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"')
85
80
 
86
- hr.get(URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.equal({
87
- 'lang' => 'Ruby', 'version' => '1.9'
88
- }.inspect)
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"')
89
83
 
90
84
  hr.get(URL + '/get?ids[]=1&ids[]=2').body.should.equal({
91
85
  'ids' => ['1', '2']
@@ -103,9 +97,9 @@ context "some basic requests with parameter" do
103
97
  'ids' => {'a' => '1', 'b' => '2'}
104
98
  }.inspect)
105
99
 
106
- hr.get(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.equal({
107
- 'ids' => {'a' => '1', 'b' => '2'}
108
- }.inspect)
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"')
109
103
  end
110
104
 
111
105
  specify "post method" do
@@ -114,21 +108,23 @@ context "some basic requests with parameter" do
114
108
  hr.post(URL + '/get?&#').body.should.equal({}.inspect)
115
109
  hr.post(URL + '/get?abc=').body.should.equal({'abc' => ''}.inspect)
116
110
 
117
- hr.post(URL + '/get?lang=Ruby&version=1.9').body.should.equal({
118
- 'lang' => 'Ruby', 'version' => '1.9'
119
- }.inspect)
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"')
120
113
 
121
- hr.post(:url => URL + '/get', :parameters => 'lang=Ruby&version=1.9').body.should.equal({
122
- 'lang' => 'Ruby', 'version' => '1.9'
123
- }.inspect)
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"')
124
116
 
125
- hr.post(:url => URL + '/get', :parameters => {:lang => 'Ruby', :version => 1.9}).body.should.equal({
126
- 'lang' => 'Ruby', 'version' => '1.9'
127
- }.inspect)
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"')
128
119
 
129
- hr.post(:url => URL + '/get', :parameters => {'lang' => 'Ruby', 'version' => '1.9'}).body.should.equal({
130
- 'lang' => 'Ruby', 'version' => '1.9'
131
- }.inspect)
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"')
132
128
 
133
129
  hr.post(URL + '/get?ids[]=1&ids[]=2').body.should.equal({
134
130
  'ids' => ['1', '2']
@@ -146,9 +142,9 @@ context "some basic requests with parameter" do
146
142
  'ids' => {'a' => '1', 'b' => '2'}
147
143
  }.inspect)
148
144
 
149
- hr.post(:url => URL + '/get', :parameters => {'ids[a]' => 1, 'ids[b]' => 2}).body.should.equal({
150
- 'ids' => {'a' => '1', 'b' => '2'}
151
- }.inspect)
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"')
152
148
  end
153
149
 
154
150
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_request.rb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
8
7
  - 1
9
- - 7
10
- version: 1.1.7
8
+ - 8
9
+ version: 1.1.8
11
10
  platform: ruby
12
11
  authors:
13
12
  - xianhua.zhou
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-08-18 00:00:00 +08:00
17
+ date: 2010-09-14 00:00:00 +08:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -47,7 +46,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
46
  requirements:
48
47
  - - ">="
49
48
  - !ruby/object:Gem::Version
50
- hash: 3
51
49
  segments:
52
50
  - 0
53
51
  version: "0"
@@ -56,7 +54,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
54
  requirements:
57
55
  - - ">="
58
56
  - !ruby/object:Gem::Version
59
- hash: 3
60
57
  segments:
61
58
  - 0
62
59
  version: "0"