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

ruby-changes:13104

From: akr <ko1@a...>
Date: Fri, 11 Sep 2009 21:51:13 +0900 (JST)
Subject: [ruby-changes:13104] Ruby:r24853 (trunk): more tests.

akr	2009-09-11 21:49:14 +0900 (Fri, 11 Sep 2009)

  New Revision: 24853

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

  Log:
    more tests.

  Modified files:
    trunk/test/test_open-uri.rb

Index: test/test_open-uri.rb
===================================================================
--- test/test_open-uri.rb	(revision 24852)
+++ test/test_open-uri.rb	(revision 24853)
@@ -2,6 +2,7 @@
 require 'open-uri'
 require 'webrick'
 require 'webrick/httpproxy'
+require 'zlib'
 
 class TestOpenURI < Test::Unit::TestCase
 
@@ -17,7 +18,7 @@
       _, port, _, host = srv.listeners[0].addr
       begin
         th = srv.start
-        yield dr, "http://#{host}:#{port}"
+        yield srv, dr, "http://#{host}:#{port}"
       ensure
         srv.shutdown
       end
@@ -25,7 +26,7 @@
   end
 
   def test_200
-    with_http {|dr, url|
+    with_http {|srv, dr, url|
       open("#{dr}/foo200", "w") {|f| f << "foo200" }
       open("#{url}/foo200") {|f|
         assert_equal("200", f.status[0])
@@ -34,15 +35,26 @@
     }
   end
 
+  def test_200big
+    with_http {|srv, dr, url|
+      content = "foo200big"*10240
+      open("#{dr}/foo200big", "w") {|f| f << content }
+      open("#{url}/foo200big") {|f|
+        assert_equal("200", f.status[0])
+        assert_equal(content, f.read)
+      }
+    }
+  end
+
   def test_404
-    with_http {|dr, url|
+    with_http {|srv, dr, url|
       exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
       assert_equal("404", exc.io.status[0])
     }
   end
 
   def test_open_uri
-    with_http {|dr, url|
+    with_http {|srv, dr, url|
       open("#{dr}/foo_ou", "w") {|f| f << "foo_ou" }
       u = URI("#{url}/foo_ou")
       open(u) {|f|
@@ -57,7 +69,7 @@
   end
 
   def test_mode
-    with_http {|dr, url|
+    with_http {|srv, dr, url|
       open("#{dr}/mode", "w") {|f| f << "mode" }
       open("#{url}/mode", "r") {|f|
         assert_equal("200", f.status[0])
@@ -72,7 +84,7 @@
   end
 
   def test_without_block
-    with_http {|dr, url|
+    with_http {|srv, dr, url|
       open("#{dr}/without_block", "w") {|g| g << "without_block" }
       begin
         f = open("#{url}/without_block")
@@ -91,7 +103,7 @@
   end
 
   def test_proxy
-    with_http {|dr, url|
+    with_http {|srv, dr, url|
       prxy = WEBrick::HTTPProxyServer.new({
                                      :ServerType => Thread,
                                      :Logger => WEBrick::Log.new(StringIO.new("")),
@@ -106,6 +118,17 @@
           assert_equal("200", f.status[0])
           assert_equal("proxy", f.read)
         }
+        open("#{url}/proxy", :proxy=>URI("http://#{p_host}:#{p_port}/")) {|f|
+          assert_equal("200", f.status[0])
+          assert_equal("proxy", f.read)
+        }
+        open("#{url}/proxy", :proxy=>nil) {|f|
+          assert_equal("200", f.status[0])
+          assert_equal("proxy", f.read)
+        }
+        assert_raise(ArgumentError) {
+          open("#{url}/proxy", :proxy=>:invalid) {}
+        }
       ensure
         prxy.shutdown
       end
@@ -113,7 +136,7 @@
   end
 
   def test_proxy_http_basic_authentication
-    with_http {|dr, url|
+    with_http {|srv, dr, url|
       prxy = WEBrick::HTTPProxyServer.new({
         :ServerType => Thread,
         :Logger => WEBrick::Log.new(StringIO.new("")),
@@ -147,5 +170,147 @@
     }
   end
 
+  def test_redirect
+    with_http {|srv, dr, url|
+      srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
+      srv.mount_proc("/r2/") {|req, res| res.body = "r2" }
+      srv.mount_proc("/to-file/") {|req, res| res.status = 301; res["location"] = "file:///foo" }
+      open("#{url}/r1/") {|f|
+        assert_equal("#{url}/r2", f.base_uri.to_s)
+        assert_equal("r2", f.read)
+      }
+      assert_raise(OpenURI::HTTPRedirect) { open("#{url}/r1/", :redirect=>false) {} }
+      assert_raise(RuntimeError) { open("#{url}/to-file/") {} }
+    }
+  end
+
+  def test_redirect_auth
+    with_http {|srv, dr, url|
+      srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2" }
+      srv.mount_proc("/r2/") {|req, res|
+        if req["Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
+          raise WEBrick::HTTPStatus::Unauthorized
+        end
+        res.body = "r2"
+      }
+      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
+      assert_equal("401", exc.io.status[0])
+      open("#{url}/r2/", :http_basic_authentication=>['user', 'pass']) {|f|
+        assert_equal("r2", f.read)
+      }
+      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
+      assert_equal("401", exc.io.status[0])
+    }
+  end
+
+  def test_userinfo
+    if "1.9.0" <= RUBY_VERSION
+      assert_raise(ArgumentError) { open("http://user:pass@1.../") {} }
+    end
+  end
+
+  def test_progress
+    with_http {|srv, dr, url|
+      content = "a" * 10000
+      srv.mount_proc("/data/") {|req, res| res.body = content }
+      length = []
+      progress = []
+      open("#{url}/data/",
+           :content_length_proc => lambda {|n| length << n },
+           :progress_proc => lambda {|n| progress << n },
+          ) {|f|
+        assert_equal(1, length.length)
+        assert_equal(content.length, length[0])
+        assert_equal(content.length, progress.inject(&:+))
+        assert_equal(content, f.read)
+      }
+    }
+  end
+
+  def test_progress_chunked
+    with_http {|srv, dr, url|
+      content = "a" * 10000
+      srv.mount_proc("/data/") {|req, res| res.body = content; res.chunked = true }
+      length = []
+      progress = []
+      open("#{url}/data/",
+           :content_length_proc => lambda {|n| length << n },
+           :progress_proc => lambda {|n| progress << n },
+          ) {|f|
+        assert_equal(1, length.length)
+        assert_equal(nil, length[0])
+        assert_equal(content.length, progress.inject(&:+))
+        assert_equal(content, f.read)
+      }
+    }
+  end
+
+  def test_uri_read
+    with_http {|srv, dr, url|
+      open("#{dr}/uriread", "w") {|f| f << "uriread" }
+      data = URI("#{url}/uriread").read
+      assert_equal("200", data.status[0])
+      assert_equal("uriread", data)
+    }
+  end
+
+  def test_encoding
+    with_http {|srv, dr, url|
+      content_u8 = "\u3042"
+      content_ej = "\xa2\xa4".force_encoding("euc-jp")
+      srv.mount_proc("/u8/") {|req, res| res.body = content_u8; res['content-type'] = 'text/plain; charset=utf-8' }
+      srv.mount_proc("/ej/") {|req, res| res.body = content_ej; res['content-type'] = 'TEXT/PLAIN; charset=EUC-JP' }
+      srv.mount_proc("/nc/") {|req, res| res.body = "aa"; res['content-type'] = 'Text/Plain' }
+      open("#{url}/u8/") {|f|
+        assert_equal(content_u8, f.read)
+        assert_equal("text/plain", f.content_type)
+        assert_equal("utf-8", f.charset)
+      }
+      open("#{url}/ej/") {|f|
+        assert_equal(content_ej, f.read)
+        assert_equal("text/plain", f.content_type)
+        assert_equal("euc-jp", f.charset)
+      }
+      open("#{url}/nc/") {|f|
+        assert_equal("aa", f.read)
+        assert_equal("text/plain", f.content_type)
+        assert_equal("iso-8859-1", f.charset)
+        assert_equal("unknown", f.charset { "unknown" })
+      }
+    }
+  end
+
+  def test_last_modified
+    with_http {|srv, dr, url|
+      srv.mount_proc("/data/") {|req, res| res.body = "foo"; res['last-modified'] = 'Fri, 07 Aug 2009 06:05:04 GMT' }
+      open("#{url}/data/") {|f|
+        assert_equal("foo", f.read)
+        assert_equal(Time.utc(2009,8,7,6,5,4), f.last_modified)
+      }
+    }
+  end
+
+  def test_content_encoding
+    with_http {|srv, dr, url|
+      content = "abc" * 10000
+      Zlib::GzipWriter.wrap(StringIO.new(content_gz="".force_encoding("ascii-8bit"))) {|z| z.write content }
+      srv.mount_proc("/data/") {|req, res| res.body = content_gz; res['content-encoding'] = 'gzip' }
+      srv.mount_proc("/data2/") {|req, res| res.body = content_gz; res['content-encoding'] = 'gzip'; res.chunked = true }
+      srv.mount_proc("/noce/") {|req, res| res.body = content_gz }
+      open("#{url}/data/") {|f|
+        assert_equal ['gzip'], f.content_encoding
+        assert_equal(content_gz, f.read.force_encoding("ascii-8bit"))
+      }
+      open("#{url}/data2/") {|f|
+        assert_equal ['gzip'], f.content_encoding
+        assert_equal(content_gz, f.read.force_encoding("ascii-8bit"))
+      }
+      open("#{url}/noce/") {|f|
+        assert_equal [], f.content_encoding
+        assert_equal(content_gz, f.read.force_encoding("ascii-8bit"))
+      }
+    }
+  end
+
 end
 

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

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