[前][次][番号順一覧][スレッド一覧]

ruby-changes:36266

From: akr <ko1@a...>
Date: Sun, 9 Nov 2014 23:01:49 +0900 (JST)
Subject: [ruby-changes:36266] akr:r48347 (trunk): * test/open-uri: Test server log in server thread.

akr	2014-11-09 23:01:20 +0900 (Sun, 09 Nov 2014)

  New Revision: 48347

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48347

  Log:
    * test/open-uri: Test server log in server thread.
    
    * test/webrick: Ditto.

  Modified files:
    trunk/ChangeLog
    trunk/test/open-uri/test_open-uri.rb
    trunk/test/open-uri/test_ssl.rb
    trunk/test/webrick/test_cgi.rb
    trunk/test/webrick/test_filehandler.rb
    trunk/test/webrick/test_httpauth.rb
    trunk/test/webrick/test_httpserver.rb
    trunk/test/webrick/utils.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48346)
+++ ChangeLog	(revision 48347)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Nov  9 22:46:13 2014  Tanaka Akira  <akr@f...>
+
+	* test/open-uri: Test server log in server thread.
+
+	* test/webrick: Ditto.
+
 Sun Nov  9 22:28:34 2014  Tanaka Akira  <akr@f...>
 
 	* lib/webrick/httpstatus.rb: require webrick/accesslog for AccessLog.
Index: test/webrick/test_httpauth.rb
===================================================================
--- test/webrick/test_httpauth.rb	(revision 48346)
+++ test/webrick/test_httpauth.rb	(revision 48347)
@@ -7,7 +7,11 @@ require_relative "utils" https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L7
 
 class TestWEBrickHTTPAuth < Test::Unit::TestCase
   def test_basic_auth
-    TestWEBrick.start_httpserver{|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      assert_equal(1, log.length)
+      assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, log[0])
+    }
+    TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
       realm = "WEBrick's realm"
       path = "/basic_auth"
 
@@ -27,7 +31,19 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L31
   end
 
   def test_basic_auth2
-    log = TestWEBrick.start_httpserver{|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      log.reject! {|line| /\A\s*\z/ =~ line }
+      pats = [
+        /ERROR Basic WEBrick's realm: webrick: password unmatch\./,
+        /ERROR WEBrick::HTTPStatus::Unauthorized/
+      ]
+      pats.each {|pat|
+        assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}")
+        log.reject! {|line| pat =~ line }
+      }
+      assert_equal([], log)
+    }
+    TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
       realm = "WEBrick's realm"
       path = "/basic_auth2"
 
@@ -61,16 +77,6 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L77
         http.request(g){|res| assert_not_equal("hoge", res.body, log.call)}
       }
     }
-    log.reject! {|line| /\A\s*\z/ =~ line }
-    pats = [
-      /ERROR Basic WEBrick's realm: webrick: password unmatch\./,
-      /ERROR WEBrick::HTTPStatus::Unauthorized/
-    ]
-    pats.each {|pat|
-      assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}")
-      log.reject! {|line| pat =~ line }
-    }
-    assert_equal([], log)
   end
 
   def test_basic_auth3
@@ -102,7 +108,20 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L108
       )/x
 
   def test_digest_auth
-    log = TestWEBrick.start_httpserver{|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      log.reject! {|line| /\A\s*\z/ =~ line }
+      pats = [
+        /ERROR Digest WEBrick's realm: no credentials in the request\./,
+        /ERROR WEBrick::HTTPStatus::Unauthorized/,
+        /ERROR Digest WEBrick's realm: webrick: digest unmatch\./
+      ]
+      pats.each {|pat|
+        assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}")
+        log.reject! {|line| pat =~ line }
+      }
+      assert_equal([], log)
+    }
+    TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
       realm = "WEBrick's realm"
       path = "/digest_auth"
 
@@ -153,17 +172,6 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L172
         end
       }
     }
-    log.reject! {|line| /\A\s*\z/ =~ line }
-    pats = [
-      /ERROR Digest WEBrick's realm: no credentials in the request\./,
-      /ERROR WEBrick::HTTPStatus::Unauthorized/,
-      /ERROR Digest WEBrick's realm: webrick: digest unmatch\./
-    ]
-    pats.each {|pat|
-      assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}")
-      log.reject! {|line| pat =~ line }
-    }
-    assert_equal([], log)
   end
 
   private
Index: test/webrick/utils.rb
===================================================================
--- test/webrick/utils.rb	(revision 48346)
+++ test/webrick/utils.rb	(revision 48347)
@@ -31,16 +31,25 @@ module TestWEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/utils.rb#L31
 
   module_function
 
-  def start_server(klass, config={}, &block)
+  DefaultLogTester = lambda {|log, access_log| assert_equal([], log) }
+
+  def start_server(klass, config={}, log_tester=DefaultLogTester, &block)
     log_ary = []
-    log = proc { "webrick log start:\n" + log_ary.join.gsub(/^/, "  ").chomp + "\nwebrick log end" }
+    access_log_ary = []
+    log = proc { "webrick log start:\n" + (log_ary+access_log_ary).join.gsub(/^/, "  ").chomp + "\nwebrick log end" }
     server = klass.new({
       :BindAddress => "127.0.0.1", :Port => 0,
       :ServerType => Thread,
       :Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN),
-      :AccessLog => [[log_ary, ""]]
+      :AccessLog => [[access_log_ary, ""]]
     }.update(config))
     server_thread = server.start
+    server_thread2 = Thread.new {
+      server_thread.join
+      if log_tester
+        log_tester.call(log_ary, access_log_ary)
+      end
+    }
     addr = server.listeners[0].addr
     client_thread = Thread.new {
       begin
@@ -49,15 +58,14 @@ module TestWEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/utils.rb#L58
         server.shutdown
       end
     }
-    assert_join_threads([client_thread, server_thread])
-    log_ary
+    assert_join_threads([client_thread, server_thread2])
   end
 
-  def start_httpserver(config={}, &block)
-    start_server(WEBrick::HTTPServer, config, &block)
+  def start_httpserver(config={}, log_tester=DefaultLogTester, &block)
+    start_server(WEBrick::HTTPServer, config, log_tester, &block)
   end
 
-  def start_httpproxy(config={}, &block)
-    start_server(WEBrick::HTTPProxyServer, config, &block)
+  def start_httpproxy(config={}, log_tester=DefaultLogTester, &block)
+    start_server(WEBrick::HTTPProxyServer, config, log_tester, &block)
   end
 end
Index: test/webrick/test_cgi.rb
===================================================================
--- test/webrick/test_cgi.rb	(revision 48346)
+++ test/webrick/test_cgi.rb	(revision 48347)
@@ -6,7 +6,7 @@ require "test/unit" https://github.com/ruby/ruby/blob/trunk/test/webrick/test_cgi.rb#L6
 class TestWEBrickCGI < Test::Unit::TestCase
   CRLF = "\r\n"
 
-  def start_cgi_server(&block)
+  def start_cgi_server(log_tester=TestWEBrick::DefaultLogTester, &block)
     config = {
       :CGIInterpreter => TestWEBrick::RubyBin,
       :DocumentRoot => File.dirname(__FILE__),
@@ -23,7 +23,7 @@ class TestWEBrickCGI < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/webrick/test_cgi.rb#L23
     if RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin32/
       config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir.
     end
-    TestWEBrick.start_httpserver(config){|server, addr, port, log|
+    TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log|
       block.call(server, addr, port, log)
     }
   end
@@ -90,7 +90,10 @@ class TestWEBrickCGI < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/webrick/test_cgi.rb#L90
   end
 
   def test_bad_request
-    start_cgi_server{|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      assert_match(/BadRequest/, log.join)
+    }
+    start_cgi_server(log_tester) {|server, addr, port, log|
       sock = TCPSocket.new(addr, port)
       begin
         sock << "POST /webrick.cgi HTTP/1.0" << CRLF
@@ -111,7 +114,11 @@ class TestWEBrickCGI < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/webrick/test_cgi.rb#L114
   DumpPat = /#{Regexp.quote(CtrlSeq.dump[1...-1])}/o
 
   def test_bad_uri
-    start_cgi_server{|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      assert_equal(1, log.length)
+      assert_match(/ERROR bad URI/, log[0])
+    }
+    start_cgi_server(log_tester) {|server, addr, port, log|
       res = TCPSocket.open(addr, port) {|sock|
         sock << "GET /#{CtrlSeq}#{CRLF}#{CRLF}"
         sock.close_write
@@ -125,7 +132,11 @@ class TestWEBrickCGI < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/webrick/test_cgi.rb#L132
   end
 
   def test_bad_header
-    start_cgi_server{|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      assert_equal(1, log.length)
+      assert_match(/ERROR bad header/, log[0])
+    }
+    start_cgi_server(log_tester) {|server, addr, port, log|
       res = TCPSocket.open(addr, port) {|sock|
         sock << "GET / HTTP/1.0#{CRLF}#{CtrlSeq}#{CRLF}#{CRLF}"
         sock.close_write
Index: test/webrick/test_httpserver.rb
===================================================================
--- test/webrick/test_httpserver.rb	(revision 48346)
+++ test/webrick/test_httpserver.rb	(revision 48347)
@@ -230,7 +230,11 @@ class TestWEBrickHTTPServer < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpserver.rb#L230
       :StopCallback => Proc.new{ stopped += 1 },
       :RequestCallback => Proc.new{|req, res| requested0 += 1 },
     }
-    TestWEBrick.start_httpserver(config){|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      assert(log.find {|s| %r{ERROR `/' not found\.} =~ s })
+      assert_equal([], log.reject {|s| %r{ERROR `/' not found\.} =~ s })
+    }
+    TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log|
       vhost_config = {
         :ServerName => "myhostname",
         :BindAddress => addr,
@@ -333,7 +337,11 @@ class TestWEBrickHTTPServer < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpserver.rb#L337
     config = {
       :ServerName => "localhost"
     }
-    TestWEBrick.start_httpserver(config){|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      assert_equal(1, log.length)
+      assert_match(/WARN  Could not determine content-length of response body./, log[0])
+    }
+    TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log|
       server.mount_proc("/", lambda { |req, res|
         r,w = IO.pipe
         # Test for not setting chunked...
@@ -362,7 +370,12 @@ class TestWEBrickHTTPServer < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpserver.rb#L370
       :ServerName => "localhost",
       :RequestHandler => Proc.new{|req, res| requested += 1 },
     }
-    TestWEBrick.start_httpserver(config){|server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      assert_equal(2, log.length)
+      assert_match(/WARN  :RequestHandler is deprecated, please use :RequestCallback/, log[0])
+      assert_match(%r{ERROR `/' not found\.}, log[1])
+    }
+    TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log|
       Thread.pass while server.status != :Running
 
       http = Net::HTTP.new(addr, port)
Index: test/webrick/test_filehandler.rb
===================================================================
--- test/webrick/test_filehandler.rb	(revision 48346)
+++ test/webrick/test_filehandler.rb	(revision 48347)
@@ -165,8 +165,13 @@ class WEBrick::TestFileHandler < Test::U https://github.com/ruby/ruby/blob/trunk/test/webrick/test_filehandler.rb#L165
 
   def test_non_disclosure_name
     config = { :DocumentRoot => File.dirname(__FILE__), }
+    log_tester = lambda {|log, access_log|
+      log = log.reject {|s| /ERROR `.*' not found\./ =~ s }
+      log = log.reject {|s| /WARN  the request refers nondisclosure name/ =~ s }
+      assert_equal([], log)
+    }
     this_file = File.basename(__FILE__)
-    TestWEBrick.start_httpserver(config) do |server, addr, port, log|
+    TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log|
       http = Net::HTTP.new(addr, port)
       doc_root_opts = server[:DocumentRootOptions]
       doc_root_opts[:NondisclosureName] = %w(.ht* *~ test_*)
@@ -189,7 +194,12 @@ class WEBrick::TestFileHandler < Test::U https://github.com/ruby/ruby/blob/trunk/test/webrick/test_filehandler.rb#L194
 
   def test_directory_traversal
     config = { :DocumentRoot => File.dirname(__FILE__), }
-    TestWEBrick.start_httpserver(config) do |server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      log = log.reject {|s| /ERROR bad URI/ =~ s }
+      log = log.reject {|s| /ERROR `.*' not found\./ =~ s }
+      assert_equal([], log)
+    }
+    TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log|
       http = Net::HTTP.new(addr, port)
       req = Net::HTTP::Get.new("/../../")
       http.request(req){|res| assert_equal("400", res.code, log.call) }
@@ -217,7 +227,12 @@ class WEBrick::TestFileHandler < Test::U https://github.com/ruby/ruby/blob/trunk/test/webrick/test_filehandler.rb#L227
       :DocumentRoot => File.dirname(__FILE__),
       :CGIPathEnv => ENV['PATH'],
     }
-    TestWEBrick.start_httpserver(config) do |server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      log = log.reject {|s| /ERROR `.*' not found\./ =~ s }
+      log = log.reject {|s| /WARN  the request refers nondisclosure name/ =~ s }
+      assert_equal([], log)
+    }
+    TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log|
       http = Net::HTTP.new(addr, port)
       if windows?
         fname = nil
@@ -260,7 +275,11 @@ class WEBrick::TestFileHandler < Test::U https://github.com/ruby/ruby/blob/trunk/test/webrick/test_filehandler.rb#L275
         end
       },
     }
-    TestWEBrick.start_httpserver(config) do |server, addr, port, log|
+    log_tester = lambda {|log, access_log|
+      log = log.reject {|s| /ERROR `.*' not found\./ =~ s }
+      assert_equal([], log)
+    }
+    TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log|
       http = Net::HTTP.new(addr, port)
 
       req = Net::HTTP::Get.new("/webrick.cgi/test")
Index: test/open-uri/test_open-uri.rb
===================================================================
--- test/open-uri/test_open-uri.rb	(revision 48346)
+++ test/open-uri/test_open-uri.rb	(revision 48347)
@@ -13,7 +13,7 @@ class TestOpenURI < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_open-uri.rb#L13
   def NullLog.<<(arg)
   end
 
-  def with_http(log_is_empty=true)
+  def with_http(log_tester=lambda {|log| assert_equal([], log) })
     log = []
     logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
     Dir.mktmpdir {|dr|
@@ -25,17 +25,22 @@ class TestOpenURI < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_open-uri.rb#L25
         :BindAddress => '127.0.0.1',
         :Port => 0})
       _, port, _, host = srv.listeners[0].addr
-      begin
-        th = srv.start
-        yield srv, dr, "http://#{host}:#{port}", th, log
-      ensure
-        srv.shutdown
-        th.join
-      end
+      server_thread = srv.start
+      server_thread2 = Thread.new {
+        server_thread.join
+        if log_tester
+          log_tester.call(log)
+        end
+      }
+      client_thread = Thread.new {
+        begin
+          yield srv, dr, "http://#{host}:#{port}", server_thread, log
+        ensure
+          srv.shutdown
+        end
+      }
+      assert_join_threads([client_thread, server_thread2])
     }
-    if log_is_empty
-      assert_equal([], log)
-    end
   end
 
   def with_env(h)
@@ -81,21 +86,13 @@ class TestOpenURI < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_open-uri.rb#L86
   end
 
   def test_404
-    with_http(false) {|srv, dr, url, server_thread, server_log|
-      client_thread = Thread.new {
-        begin
-          exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
-          assert_equal("404", exc.io.status[0])
-        ensure
-          srv.shutdown
-        end
-      }
-      server_thread2 = Thread.new {
-        server_thread.join
-        assert_equal(1, server_log.length)
-        assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
-      }
-      assert_join_threads([client_thread, server_thread2])
+    log_tester = lambda {|server_log|
+      assert_equal(1, server_log.length)
+      assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
+    }
+    with_http(log_tester) {|srv, dr, url, server_thread, server_log|
+      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
+      assert_equal("404", exc.io.status[0])
     }
   end
 
@@ -475,42 +472,26 @@ class TestOpenURI < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_open-uri.rb#L472
   end
 
   def test_redirect_auth_failure_r2
-    with_http(false) {|srv, dr, url, server_thread, server_log|
+    log_tester = lambda {|server_log|
+      assert_equal(1, server_log.length)
+      assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
+    }
+    with_http(log_tester) {|srv, dr, url, server_thread, server_log|
       setup_redirect_auth(srv, url)
-      client_thread = Thread.new {
-        begin
-          exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
-          assert_equal("401", exc.io.status[0])
-        ensure
-          srv.shutdown
-        end
-      }
-      server_thread2 = Thread.new {
-        server_thread.join
-        assert_equal(1, server_log.length)
-        assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
-      }
-      assert_join_threads([client_thread, server_thread2])
+      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
+      assert_equal("401", exc.io.status[0])
     }
   end
 
   def test_redirect_auth_failure_r1
-    with_http(false) {|srv, dr, url, server_thread, server_log|
+    log_tester = lambda {|server_log|
+      assert_equal(1, server_log.length)
+      assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
+    }
+    with_http(log_tester) {|srv, dr, url, server_thread, server_log|
       setup_redirect_auth(srv, url)
-      client_thread = Thread.new {
-        begin
-          exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
-          assert_equal("401", exc.io.status[0])
-        ensure
-          srv.shutdown
-        end
-      }
-      server_thread2 = Thread.new {
-        server_thread.join
-        assert_equal(1, server_log.length)
-        assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
-      }
-      assert_join_threads([client_thread, server_thread2])
+      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
+      assert_equal("401", exc.io.status[0])
     }
   end
 
Index: test/open-uri/test_ssl.rb
===================================================================
--- test/open-uri/test_ssl.rb	(revision 48346)
+++ test/open-uri/test_ssl.rb	(revision 48347)
@@ -18,7 +18,7 @@ class TestOpenURISSL https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_ssl.rb#L18
   def NullLog.<<(arg)
   end
 
-  def with_https(log_is_empty=true)
+  def with_https(log_tester=lambda {|log| assert_equal([], log) })
     log = []
     logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
     Dir.mktmpdir {|dr|
@@ -34,16 +34,22 @@ class TestOpenURISSL https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_ssl.rb#L34
         :BindAddress => '127.0.0.1',
         :Port => 0})
       _, port, _, host = srv.listeners[0].addr
-      begin
-        th = srv.start
-        yield srv, dr, "https://#{host}:#{port}", th, log
-      ensure
-        srv.shutdown
-        th.join
-      end
-      if l (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]