Permalink
Browse files

don't escape the parameters(name & value) when uploading files.

  • Loading branch information...
Zhou Xianhua
Zhou Xianhua committed Sep 14, 2010
1 parent 7a75100 commit a58751028fd5eab5be77f8dc90ea972872f1bf14
Showing with 39 additions and 42 deletions.
  1. +7 −6 lib/http_request.rb
  2. +32 −36 test/test_http_request.rb
View
@@ -286,7 +286,7 @@ def parse_options(method, options)
# parse parameters for the options[:parameters] and @uri.query
def parse_parameters
- if @options[:parameters].is_a? Hash
+ if @options[:parameters].is_a?(Hash)
@options[:parameters] = @options[:parameters].collect{|k, v|
"#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
}.join('&')
@@ -297,24 +297,25 @@ def parse_parameters
end
# for uploading files
- build_multipart if @options[:files].is_a?(Array) and 'post'.eql?(@options[:method])
+ build_multipart if @options[:files] and 'post'.eql?(@options[:method])
end
# for uploading files
def build_multipart
boundary = md5(rand.to_s).to_s[0..5]
@headers['Content-type'] = "multipart/form-data, boundary=#{boundary}"
multipart = []
- if @options[:parameters]
- @options[:parameters] = CGI.parse(@options[:parameters]) if @options[:parameters].is_a? String
+ if @options[:parameters].is_a?(String)
+ @options[:parameters] = CGI.parse(@options[:parameters])
if @options[:parameters].is_a? Hash
@options[:parameters].each {|k, v|
multipart << "--#{boundary}"
- multipart << "Content-disposition: form-data; name=\"#{CGI.escape(k.to_s)}\""
- multipart << "\r\n#{CGI.escape(v.to_s)}"
+ multipart << "Content-disposition: form-data; name=\"#{k}\""
+ multipart << "\r\n#{v.first}"
}
end
end
+ @options[:files] = [@options[:files]] if @options[:files].is_a?(Hash)
@options[:files].each_with_index {|f, index|
f[:field_name] ||= "files[]"
f[:file_name] ||= "#{boundary}_#{index}"
View
@@ -63,29 +63,23 @@
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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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']
@@ -103,9 +97,9 @@
'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
@@ -114,21 +108,23 @@
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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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.equal({
- 'lang' => 'Ruby', 'version' => '1.9'
- }.inspect)
+ 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']
@@ -146,9 +142,9 @@
'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

0 comments on commit a587510

Please sign in to comment.