From 5fbd942f84886d81c712677ee4538b0f7ad529e2 Mon Sep 17 00:00:00 2001 From: Dean Strelau Date: Mon, 30 Apr 2012 14:28:22 -0400 Subject: [PATCH] Make NullIO#read mimic IO#read --- lib/puma/null_io.rb | 8 ++++---- test/test_null_io.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 test/test_null_io.rb diff --git a/lib/puma/null_io.rb b/lib/puma/null_io.rb index d739de96..5a76cc8d 100644 --- a/lib/puma/null_io.rb +++ b/lib/puma/null_io.rb @@ -1,10 +1,10 @@ +require 'stringio' module Puma # Provides an IO-like object that always appears to contain no data. # Used as the value for rack.input when the request has no body. # class NullIO - # Always returns nil # def gets @@ -16,10 +16,10 @@ module Puma def each end - # Always returns nil + # Mimics IO#read with no data # - def read(count) - nil + def read(*args) + StringIO.new('').read(*args) end # Does nothing diff --git a/test/test_null_io.rb b/test/test_null_io.rb new file mode 100644 index 00000000..380cfe5a --- /dev/null +++ b/test/test_null_io.rb @@ -0,0 +1,31 @@ +require 'puma/null_io' +require 'test/unit' + +class TestNullIO < Test::Unit::TestCase + attr_accessor :nio + def setup + self.nio = Puma::NullIO.new + end + + def test_read_with_no_arguments + assert_equal "", nio.read + end + + def test_read_with_nil_length + assert_equal "", nio.read(nil) + end + + def test_read_with_zero_length + assert_equal "", nio.read(0) + end + + def test_read_with_positive_integer_length + assert_nil nio.read(1) + end + + def test_read_with_length_and_buffer + buf = "" + assert_nil nio.read(1,buf) + assert_equal "", buf + end +end