From 1c9f703a4705b75a31308682828a57330422c706 Mon Sep 17 00:00:00 2001 From: naruse Date: Mon, 13 Dec 2010 01:04:37 +0000 Subject: * lib/net/http.rb (Net::HTTPRequest#set_form): Added to support both application/x-www-form-urlencoded and multipart/form-data. There is a similar API, Net::HTTPRequest#set_form_data, but to keep its compatibility this is newly added. [ruby-dev:42729] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30188 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/net/http/test_http.rb | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'test/net/http') diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 586cce59bc..a2ce962573 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -293,6 +293,102 @@ module TestNetHTTP_version_1_2_methods assert_equal data.size, res.body.size assert_equal data, res.body end + + def test_set_form + require 'tempfile' + file = Tempfile.new('ruby-test') + file << "\u{30c7}\u{30fc}\u{30bf}" + data = [ + ['name', 'Gonbei Nanashi'], + ['name', "\u{540d}\u{7121}\u{3057}\u{306e}\u{6a29}\u{5175}\u{885b}"], + ['s"i\o', StringIO.new("\u{3042 3044 4e9c 925b}")], + ["file", file, filename: "ruby-test"] + ] + expected = <<"__EOM__".gsub(/\n/, "\r\n") +-- +Content-Disposition: form-data; name="name" + +Gonbei Nanashi +-- +Content-Disposition: form-data; name="name" + +\xE5\x90\x8D\xE7\x84\xA1\xE3\x81\x97\xE3\x81\xAE\xE6\xA8\xA9\xE5\x85\xB5\xE8\xA1\x9B +-- +Content-Disposition: form-data; name="s\\"i\\\\o" + +\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B +-- +Content-Disposition: form-data; name="file"; filename="ruby-test" +Content-Type: application/octet-stream + +\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF +---- +__EOM__ + start {|http| + _test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)}) + _test_set_form_multipart(http, false, data, expected) + _test_set_form_multipart(http, true, data, expected) + } + end + + def _test_set_form_urlencoded(http, data) + req = Net::HTTP::Post.new('/') + req.set_form(data) + res = http.request req + assert_equal "name=Gonbei+Nanashi&name=%E5%90%8D%E7%84%A1%E3%81%97%E3%81%AE%E6%A8%A9%E5%85%B5%E8%A1%9B", res.body + end + + def _test_set_form_multipart(http, chunked_p, data, expected) + data.each{|k,v|v.rewind rescue nil} + req = Net::HTTP::Post.new('/') + req.set_form(data, 'multipart/form-data') + req['Transfer-Encoding'] = 'chunked' if chunked_p + res = http.request req + body = res.body + assert_match(/\A--(?\S+)/, body) + /\A--(?\S+)/ =~ body + expected = expected.gsub(//, boundary) + assert_equal(expected, body) + end + + def test_set_form_with_file + require 'tempfile' + file = Tempfile.new('ruby-test') + file << $test_net_http_data + filename = File.basename(file.to_path) + data = [['file', file]] + expected = <<"__EOM__".gsub(/\n/, "\r\n") +-- +Content-Disposition: form-data; name="file"; filename="" +Content-Type: application/octet-stream + + +---- +__EOM__ + expected.sub!(//, filename) + expected.sub!(//, $test_net_http_data) + start {|http| + data.each{|k,v|v.rewind rescue nil} + req = Net::HTTP::Post.new('/') + req.set_form(data, 'multipart/form-data') + res = http.request req + body = res.body + header, _ = body.split(/\r\n\r\n/, 2) + assert_match(/\A--(?\S+)/, body) + /\A--(?\S+)/ =~ body + expected = expected.gsub(//, boundary) + assert_match(/^--(?\S+)\r\n/, header) + assert_match( + /^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/, + header) + assert_equal(expected, body) + + data.each{|k,v|v.rewind rescue nil} + req['Transfer-Encoding'] = 'chunked' + res = http.request req + #assert_equal(expected, res.body) + } + end end class TestNetHTTP_v1_2 < Test::Unit::TestCase -- cgit v1.2.3