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

ruby-changes:30358

From: drbrain <ko1@a...>
Date: Thu, 8 Aug 2013 03:38:55 +0900 (JST)
Subject: [ruby-changes:30358] drbrain:r42427 (trunk): * lib/webrick/httpresponse.rb: Allow #body to be an IO-like object

drbrain	2013-08-08 03:38:39 +0900 (Thu, 08 Aug 2013)

  New Revision: 42427

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

  Log:
    * lib/webrick/httpresponse.rb:  Allow #body to be an IO-like object
      that responds to #readpartial and #read.
      [ruby-trunk - Feature #8155]
    * NEWS:  NEWS for above
    * test/webrick/test_httpresponse.rb:  Tests for above.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/lib/webrick/httpresponse.rb
    trunk/test/webrick/test_httpresponse.rb
_______________________________________________
ruby-cvs mailing list
ruby-cvs@r...
http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-cvs
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42426)
+++ ChangeLog	(revision 42427)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Aug  8 03:37:38 2013  Eric Hodel  <drbrain@s...>
+
+	* lib/webrick/httpresponse.rb:  Allow #body to be an IO-like object
+	  that responds to #readpartial and #read.
+	  [ruby-trunk - Feature #8155]
+	* NEWS:  NEWS for above
+	* test/webrick/test_httpresponse.rb:  Tests for above.
+
 Wed Aug  7 23:06:26 2013  Akinori MUSHA  <knu@i...>
 
 	* ruby.c (Process.argv0): New method to return the original value
Index: lib/webrick/httpresponse.rb
===================================================================
--- lib/webrick/httpresponse.rb	(revision 42426)
+++ lib/webrick/httpresponse.rb	(revision 42427)
@@ -47,7 +47,8 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpresponse.rb#L47
     attr_accessor :reason_phrase
 
     ##
-    # Body may be a String or IO subclass.
+    # Body may be a String or IO-like object that responds to #read and
+    # #readpartial.
 
     attr_accessor :body
 
@@ -299,9 +300,10 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpresponse.rb#L300
     # Sends the body on +socket+
 
     def send_body(socket) # :nodoc:
-      case @body
-      when IO then send_body_io(socket)
-      else send_body_string(socket)
+      if @body.respond_to? :readpartial then
+        send_body_io(socket)
+      else
+        send_body_string(socket)
       end
     end
 
Index: NEWS
===================================================================
--- NEWS	(revision 42426)
+++ NEWS	(revision 42427)
@@ -145,6 +145,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L145
 * CGI::Util
   * All class methods modulized.
 
+* WEBrick
+  * The body of a response may now be a StringIO or other IO-like that responds
+    to #readpartial and #read.
+
 * XMLRPC::Client
   * New methods:
     * XMLRPC::Client#http. It returns Net::HTTP for the client. Normally,
Index: test/webrick/test_httpresponse.rb
===================================================================
--- test/webrick/test_httpresponse.rb	(revision 42426)
+++ test/webrick/test_httpresponse.rb	(revision 42427)
@@ -1,5 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L1
 require "webrick"
 require "minitest/autorun"
+require "stringio"
 
 module WEBrick
   class TestHTTPResponse < MiniTest::Unit::TestCase
@@ -45,5 +46,93 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L46
 
       assert_equal 0, logger.messages.length
     end
+
+    def test_send_body_io
+      body_r, body_w = IO.pipe
+
+      body_w.write 'hello'
+      body_w.close
+
+      @res.body = body_r
+
+      r, w = IO.pipe
+
+      @res.send_body w
+
+      w.close
+
+      assert_equal 'hello', r.read
+    end
+
+    def test_send_body_string
+      @res.body = 'hello'
+
+      r, w = IO.pipe
+
+      @res.send_body w
+
+      w.close
+
+      assert_equal 'hello', r.read
+    end
+
+    def test_send_body_string_io
+      @res.body = StringIO.new 'hello'
+
+      r, w = IO.pipe
+
+      @res.send_body w
+
+      w.close
+
+      assert_equal 'hello', r.read
+    end
+
+    def test_send_body_io_chunked
+      @res.chunked = true
+
+      body_r, body_w = IO.pipe
+
+      body_w.write 'hello'
+      body_w.close
+
+      @res.body = body_r
+
+      r, w = IO.pipe
+
+      @res.send_body w
+
+      w.close
+
+      assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
+    end
+
+    def test_send_body_string_chunked
+      @res.chunked = true
+
+      @res.body = 'hello'
+
+      r, w = IO.pipe
+
+      @res.send_body w
+
+      w.close
+
+      assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
+    end
+
+    def test_send_body_string_io_chunked
+      @res.chunked = true
+
+      @res.body = StringIO.new 'hello'
+
+      r, w = IO.pipe
+
+      @res.send_body w
+
+      w.close
+
+      assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
+    end
   end
 end

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

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