diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-12-13 01:04:37 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-12-13 01:04:37 +0000 |
commit | 1c9f703a4705b75a31308682828a57330422c706 (patch) | |
tree | cd5aec0c856aa482d0dee0bc5979861e4e9013a3 /test | |
parent | 31c99ce745fabe5b996446af5d849e265cf2d2ca (diff) | |
download | ruby-1c9f703a4705b75a31308682828a57330422c706.tar.gz |
* 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
Diffstat (limited to 'test')
-rw-r--r-- | test/net/http/test_http.rb | 96 |
1 files changed, 96 insertions, 0 deletions
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") +--<boundary> +Content-Disposition: form-data; name="name" + +Gonbei Nanashi +--<boundary> +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 +--<boundary> +Content-Disposition: form-data; name="s\\"i\\\\o" + +\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B +--<boundary> +Content-Disposition: form-data; name="file"; filename="ruby-test" +Content-Type: application/octet-stream + +\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF +--<boundary>-- +__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--(?<boundary>\S+)/, body) + /\A--(?<boundary>\S+)/ =~ body + expected = expected.gsub(/<boundary>/, 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") +--<boundary> +Content-Disposition: form-data; name="file"; filename="<filename>" +Content-Type: application/octet-stream + +<data> +--<boundary>-- +__EOM__ + expected.sub!(/<filename>/, filename) + expected.sub!(/<data>/, $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--(?<boundary>\S+)/, body) + /\A--(?<boundary>\S+)/ =~ body + expected = expected.gsub(/<boundary>/, boundary) + assert_match(/^--(?<boundary>\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 |