From 47c4c9f830082b8c8272832ee8568c1a3bd3a3c2 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Sun, 2 Jan 2005 22:50:01 +0000 Subject: This commit was manufactured by cvs2svn to create branch 'ruby_1_8'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@7710 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/net/http/test_httpheader.rb | 212 +++++++++++++++++++++++++++++++++++++ test/nkf/test_kconv.rb | 71 +++++++++++++ test/nkf/test_nkf.rb | 16 +++ test/webrick/test_httpauth.rb | 105 ++++++++++++++++++ test/xmlrpc/test_webrick_server.rb | 92 ++++++++++++++++ test/xmlrpc/webrick_testing.rb | 37 +++++++ 6 files changed, 533 insertions(+) create mode 100644 test/net/http/test_httpheader.rb create mode 100644 test/nkf/test_kconv.rb create mode 100644 test/nkf/test_nkf.rb create mode 100644 test/webrick/test_httpauth.rb create mode 100644 test/xmlrpc/test_webrick_server.rb create mode 100644 test/xmlrpc/webrick_testing.rb (limited to 'test') diff --git a/test/net/http/test_httpheader.rb b/test/net/http/test_httpheader.rb new file mode 100644 index 0000000000..1a8751e28c --- /dev/null +++ b/test/net/http/test_httpheader.rb @@ -0,0 +1,212 @@ +require 'net/http' +require 'test/unit' + +class HTTPHeaderTest < Test::Unit::TestCase + + class C + include Net::HTTPHeader + def initialize + @header = {} + end + end + + def setup + @c = C.new + end + + def test_size + assert_equal 0, @c.size + @c['a'] = 'a' + assert_equal 1, @c.size + @c['b'] = 'b' + assert_equal 2, @c.size + @c['b'] = 'b' + assert_equal 2, @c.size + @c['c'] = 'c' + assert_equal 3, @c.size + end + + def test_ASET + @c['My-Header'] = 'test string' + @c['my-Header'] = 'test string' + @c['My-header'] = 'test string' + @c['my-header'] = 'test string' + @c['MY-HEADER'] = 'test string' + assert_equal 1, @c.size + + @c['AaA'] = 'aaa' + @c['aaA'] = 'aaa' + @c['AAa'] = 'aaa' + assert_equal 2, @c.length + end + + def test_AREF + @c['My-Header'] = 'test string' + assert_equal 'test string', @c['my-header'] + assert_equal 'test string', @c['MY-header'] + assert_equal 'test string', @c['my-HEADER'] + + @c['Next-Header'] = 'next string' + assert_equal 'next string', @c['next-header'] + end + + def test_add_field + end + + def test_get_fields + end + + def test_delete + end + + def test_each + end + + def test_each_key + end + + def test_each_value + end + + def test_each_capitalized + @c['my-header'] = ['a', 'b'] + @c.each_capitalized do |k,v| + assert_equal 'My-Header', k + assert_equal 'a, b', v + end + end + + def test_key? + end + + def test_to_hash + end + + def test_range + try_range(1..5, '1-5') + try_range(234..567, '234-567') + try_range(-5..-1, '-5') + try_range(1..-1, '1-') + end + + def try_range(r, s) + @c['range'] = "bytes=#{s}" + assert_equal r, Array(@c.range)[0] + end + + def test_range= + @c.range = 0..499 + assert_equal 'bytes=0-499', @c['range'] + @c.range = 0...500 + assert_equal 'bytes=0-499', @c['range'] + @c.range = 300 + assert_equal 'bytes=0-299', @c['range'] + @c.range = -400 + assert_equal 'bytes=-400', @c['range'] + @c.set_range 0, 500 + assert_equal 'bytes=0-499', @c['range'] + end + + def test_content_range + end + + def test_range_length + @c['Content-Range'] = "bytes 0-499/1000" + assert_equal 500, @c.range_length + @c['Content-Range'] = "bytes 1-500/1000" + assert_equal 500, @c.range_length + @c['Content-Range'] = "bytes 1-1/1000" + assert_equal 1, @c.range_length + end + + def test_chunked? + try_chunked true, 'chunked' + try_chunked true, ' chunked ' + try_chunked true, '(OK)chunked' + + try_chunked false, 'not-chunked' + try_chunked false, 'chunked-but-not-chunked' + end + + def try_chunked(bool, str) + @c['transfer-encoding'] = str + assert_equal bool, @c.chunked? + end + + def test_content_length + @c.delete('content-length') + assert_nil @c['content-length'] + + try_content_length 500, '500' + try_content_length 10000_0000_0000, '1000000000000' + try_content_length 123, ' 123' + try_content_length 1, '1 23' + try_content_length 500, '(OK)500' + assert_raises(Net::HTTPHeaderSyntaxError, 'here is no digit, but') { + @c['content-length'] = 'no digit' + @c.content_length + } + end + + def try_content_length(len, str) + @c['content-length'] = str + assert_equal len, @c.content_length + end + + def test_content_length= + @c.content_length = 0 + assert_equal 0, @c.content_length + @c.content_length = 1 + assert_equal 1, @c.content_length + @c.content_length = 999 + assert_equal 999, @c.content_length + @c.content_length = 10000000000000 + assert_equal 10000000000000, @c.content_length + end + + def test_content_type + @c.content_type = 'text/html' + assert_equal 'text/html', @c.content_type + @c.content_type = 'application/pdf' + assert_equal 'application/pdf', @c.content_type + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal 'text/html', @c.content_type + end + + def test_main_type + @c.content_type = 'text/html' + assert_equal 'text', @c.main_type + @c.content_type = 'application/pdf' + assert_equal 'application', @c.main_type + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal 'text', @c.main_type + end + + def test_sub_type + @c.content_type = 'text/html' + assert_equal 'html', @c.sub_type + @c.content_type = 'application/pdf' + assert_equal 'pdf', @c.sub_type + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal 'html', @c.sub_type + end + + def test_type_params + @c.content_type = 'text/html' + assert_equal({}, @c.type_params) + @c.content_type = 'application/pdf' + assert_equal({}, @c.type_params) + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal({'charset' => 'iso-2022-jp'}, @c.type_params) + end + + def test_set_content_type + end + + def test_basic_auth + end + + def test_proxy_basic_auth + end + +end diff --git a/test/nkf/test_kconv.rb b/test/nkf/test_kconv.rb new file mode 100644 index 0000000000..2533424705 --- /dev/null +++ b/test/nkf/test_kconv.rb @@ -0,0 +1,71 @@ +require 'test/unit' +require 'kconv' + +class TestKconv < Test::Unit::TestCase + EUC_STR = "\ +\xa5\xaa\xa5\xd6\xa5\xb8\xa5\xa7\xa5\xaf\xa5\xc8\xbb\xd8\xb8\xfe\ +\xa5\xd7\xa5\xed\xa5\xb0\xa5\xe9\xa5\xdf\xa5\xf3\xa5\xb0\xb8\xc0\xb8\xec +\x52\x75\x62\x79" + UTF8_STR = "\ +\xe3\x82\xaa\xe3\x83\x96\xe3\x82\xb8\xe3\x82\xa7\ +\xe3\x82\xaf\xe3\x83\x88\xe6\x8c\x87\xe5\x90\x91\ +\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\x9f\ +\xe3\x83\xb3\xe3\x82\xb0\xe8\xa8\x80\xe8\xaa\x9e +\x52\x75\x62\x79" + SJIS_STR = "\ +\x83\x49\x83\x75\x83\x57\x83\x46\x83\x4e\x83\x67\x8e\x77\x8c\xfc\ +\x83\x76\x83\x8d\x83\x4f\x83\x89\x83\x7e\x83\x93\x83\x4f\x8c\xbe\x8c\xea +\x52\x75\x62\x79" + JIS_STR = "\ +\x1b\x24\x42\x25\x2a\x25\x56\x25\x38\x25\x27\x25\x2f\x25\x48\x3b\x58\x38\x7e\ +\x25\x57\x25\x6d\x25\x30\x25\x69\x25\x5f\x25\x73\x25\x30\x38\x40\x38\x6c\x1b\x28\x42 +\x52\x75\x62\x79" + + def test_eucjp + assert(EUC_STR.iseuc) + assert_equal(::Kconv::EUC, Kconv.guess(EUC_STR)) + assert_equal(EUC_STR, EUC_STR.toeuc) + assert_equal(EUC_STR, SJIS_STR.toeuc) + assert_equal(EUC_STR, UTF8_STR.toeuc) + assert_equal(EUC_STR, JIS_STR.toeuc) + assert_equal(EUC_STR, EUC_STR.kconv(::NKF::EUC)) + assert_equal(EUC_STR, SJIS_STR.kconv(::NKF::EUC)) + assert_equal(EUC_STR, UTF8_STR.kconv(::NKF::EUC)) + assert_equal(EUC_STR, JIS_STR.kconv(::NKF::EUC)) + end + def test_shiftjis + assert(SJIS_STR.issjis) + assert_equal(::Kconv::SJIS, Kconv.guess(SJIS_STR)) + assert_equal(SJIS_STR, EUC_STR.tosjis) + assert_equal(SJIS_STR, SJIS_STR.tosjis) + assert_equal(SJIS_STR, UTF8_STR.tosjis) + assert_equal(SJIS_STR, JIS_STR.tosjis) + assert_equal(SJIS_STR, EUC_STR.kconv(::NKF::SJIS)) + assert_equal(SJIS_STR, SJIS_STR.kconv(::NKF::SJIS)) + assert_equal(SJIS_STR, UTF8_STR.kconv(::NKF::SJIS)) + assert_equal(SJIS_STR, JIS_STR.kconv(::NKF::SJIS)) + end + def test_utf8 + assert(UTF8_STR.isutf8) + assert_equal(::Kconv::UTF8, Kconv.guess(UTF8_STR)) + assert_equal(UTF8_STR, EUC_STR.toutf8) + assert_equal(UTF8_STR, SJIS_STR.toutf8) + assert_equal(UTF8_STR, UTF8_STR.toutf8) + assert_equal(UTF8_STR, JIS_STR.toutf8) + assert_equal(UTF8_STR, EUC_STR.kconv(::NKF::UTF8)) + assert_equal(UTF8_STR, SJIS_STR.kconv(::NKF::UTF8)) + assert_equal(UTF8_STR, UTF8_STR.kconv(::NKF::UTF8)) + assert_equal(UTF8_STR, JIS_STR.kconv(::NKF::UTF8)) + end + def test_jis + assert_equal(::Kconv::JIS, Kconv.guess(JIS_STR)) + assert_equal(JIS_STR, EUC_STR.tojis) + assert_equal(JIS_STR, SJIS_STR.tojis) + assert_equal(JIS_STR, UTF8_STR.tojis) + assert_equal(JIS_STR, JIS_STR.tojis) + assert_equal(JIS_STR, EUC_STR.kconv(::NKF::JIS)) + assert_equal(JIS_STR, SJIS_STR.kconv(::NKF::JIS)) + assert_equal(JIS_STR, UTF8_STR.kconv(::NKF::JIS)) + assert_equal(JIS_STR, JIS_STR.kconv(::NKF::JIS)) + end +end diff --git a/test/nkf/test_nkf.rb b/test/nkf/test_nkf.rb new file mode 100644 index 0000000000..279ad59e1a --- /dev/null +++ b/test/nkf/test_nkf.rb @@ -0,0 +1,16 @@ +require 'test/unit' +require 'nkf' + +class TestNKF < Test::Unit::TestCase + EUC_STR = "\xa5\xaa\xa5\xd6\xa5\xb8\xa5\xa7\xa5\xaf\xa5\xc8\xbb\xd8\xb8\xfe\ +\xa5\xb9\xa5\xaf\xa5\xea\xa5\xd7\xa5\xc8\xb8\xc0\xb8\xec\ +Ruby" + + def test_guess + str_euc = EUC_STR + str_jis = NKF.nkf('-j', str_euc) + assert_equal(::NKF::JIS, NKF.guess(str_jis)) + assert_equal(::NKF::EUC, NKF.guess(str_euc)) + end + +end diff --git a/test/webrick/test_httpauth.rb b/test/webrick/test_httpauth.rb new file mode 100644 index 0000000000..7109ebc2cb --- /dev/null +++ b/test/webrick/test_httpauth.rb @@ -0,0 +1,105 @@ +require "test/unit" +require "net/http" +require "tempfile" +require "webrick" +require "webrick/httpauth/basicauth" + +class TestWEBrickHTTPAuth < Test::Unit::TestCase + class NullWriter + def NullWriter.<<(msg) + puts msg if $DEBUG + return self + end + end + + def start_httpserver + server = WEBrick::HTTPServer.new( + :BindAddress => "0.0.0.0", :Port => 0, + :Logger => WEBrick::Log.new(NullWriter), + :AccessLog => [[NullWriter, ""]] + ) + thread = nil + begin + thread = Thread.start{ server.start } + addr = server.listeners[0].addr + yield([server, addr[3], addr[1]]) + ensure + server.stop + thread.join + end + end + + def test_basic_auth + start_httpserver{|server, addr, port| + realm = "WEBrick's realm" + path = "/basic_auth" + + server.mount_proc(path){|req, res| + WEBrick::HTTPAuth.basic_auth(req, res, realm){|user, pass| + user == "webrick" && pass == "supersecretpassword" + } + res.body = "hoge" + } + http = Net::HTTP.new(addr, port) + g = Net::HTTP::Get.new(path) + g.basic_auth("webrick", "supersecretpassword") + http.request(g){|res| assert_equal("hoge", res.body)} + g.basic_auth("webrick", "not super") + http.request(g){|res| assert_not_equal("hoge", res.body)} + } + end + + def test_basic_auth2 + start_httpserver{|server, addr, port| + realm = "WEBrick's realm" + path = "/basic_auth2" + + tmpfile = Tempfile.new("test_webrick_auth") + tmpfile.close + tmp_pass = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path) + tmp_pass.set_passwd(realm, "webrick", "supersecretpassword") + tmp_pass.set_passwd(realm, "foo", "supersecretpassword") + tmp_pass.flush + + htpasswd = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path) + users = [] + htpasswd.each{|user, pass| users << user } + assert_equal(2, users.size) + assert(users.member?("webrick")) + assert(users.member?("foo")) + + server.mount_proc(path){|req, res| + auth = WEBrick::HTTPAuth::BasicAuth.new( + :Realm => realm, :UserDB => htpasswd, + :Logger => server.logger + ) + auth.authenticate(req, res) + res.body = "hoge" + } + http = Net::HTTP.new(addr, port) + g = Net::HTTP::Get.new(path) + g.basic_auth("webrick", "supersecretpassword") + http.request(g){|res| assert_equal("hoge", res.body)} + g.basic_auth("webrick", "not super") + http.request(g){|res| assert_not_equal("hoge", res.body)} + } + end + + def test_basic_auth3 + tmpfile = Tempfile.new("test_webrick_auth") + tmpfile.puts("webrick:{SHA}GJYFRpBbdchp595jlh3Bhfmgp8k=") + tmpfile.flush + assert_raises(NotImplementedError){ + WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path) + } + tmpfile.close(true) + + tmpfile = Tempfile.new("test_webrick_auth") + tmpfile.puts("webrick:$apr1$IOVMD/..$rmnOSPXr0.wwrLPZHBQZy0") + tmpfile.flush + assert_raises(NotImplementedError){ + WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path) + } + tmpfile.close(true) + end +end diff --git a/test/xmlrpc/test_webrick_server.rb b/test/xmlrpc/test_webrick_server.rb new file mode 100644 index 0000000000..e7fed016b0 --- /dev/null +++ b/test/xmlrpc/test_webrick_server.rb @@ -0,0 +1,92 @@ +require 'test/unit' +require 'webrick' +require File.join(File.dirname(__FILE__), 'webrick_testing') +require "xmlrpc/server" +require 'xmlrpc/client' + +class Test_Webrick < Test::Unit::TestCase + include WEBrick_Testing + + def create_servlet + s = XMLRPC::WEBrickServlet.new + + s.add_handler("test.add") do |a,b| + a + b + end + + s.add_handler("test.div") do |a,b| + if b == 0 + raise XMLRPC::FaultException.new(1, "division by zero") + else + a / b + end + end + + s.set_default_handler do |name, *args| + raise XMLRPC::FaultException.new(-99, "Method #{name} missing" + + " or wrong number of parameters!") + end + + s.add_introspection + + return s + end + + def setup_http_server(port, use_ssl) + require 'webrick/https' + start_server( + :Port => port, + :SSLEnable => use_ssl, + :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE, + :SSLCertName => [] + ) {|w| w.mount('/RPC2', create_servlet) } + + @s = XMLRPC::Client.new3(:port => port, :use_ssl => use_ssl) + end + + PORT = 8070 + def test_client_server + # NOTE: I don't enable SSL testing as this hangs + [false].each do |use_ssl| + begin + setup_http_server(PORT, use_ssl) + do_test + ensure + stop_server + end + end + end + + def do_test + # simple call + assert_equal 9, @s.call('test.add', 4, 5) + + # fault exception + assert_raises(XMLRPC::FaultException) { @s.call('test.div', 1, 0) } + + # fault exception via call2 + ok, param = @s.call2('test.div', 1, 0) + assert_equal false, ok + assert_instance_of XMLRPC::FaultException, param + assert_equal 1, param.faultCode + assert_equal 'division by zero', param.faultString + + # call2 without fault exception + ok, param = @s.call2('test.div', 10, 5) + assert_equal true, ok + assert_equal param, 2 + + # introspection + assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods") + + # default handler (missing handler) + ok, param = @s.call2('test.nonexisting') + assert_equal false, ok + assert_equal -99, param.faultCode + + # default handler (wrong number of arguments) + ok, param = @s.call2('test.add', 1, 2, 3) + assert_equal false, ok + assert_equal -99, param.faultCode + end +end diff --git a/test/xmlrpc/webrick_testing.rb b/test/xmlrpc/webrick_testing.rb new file mode 100644 index 0000000000..a6830cece1 --- /dev/null +++ b/test/xmlrpc/webrick_testing.rb @@ -0,0 +1,37 @@ +require 'timeout' + +module WEBrick_Testing + class DummyLog < WEBrick::BasicLog + def initialize() super(self) end + def <<(*args) end + end + + def start_server(config={}) + raise "already started" if @__server + @__started = false + + Thread.new { + @__server = WEBrick::HTTPServer.new( + { + :Logger => DummyLog.new, + :AccessLog => [], + :StartCallback => proc { @__started = true } + }.update(config)) + yield @__server + @__server.start + @__started = false + } + + Timeout.timeout(5) { + nil until @__started # wait until the server is ready + } + end + + def stop_server + Timeout.timeout(5) { + @__server.shutdown + nil while @__started # wait until the server is down + } + @__server = nil + end +end -- cgit v1.2.3