1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Make NullIO#read mimic IO#read

This commit is contained in:
Dean Strelau 2012-04-30 14:28:22 -04:00
parent f6b4363b39
commit 5fbd942f84
2 changed files with 35 additions and 4 deletions

View file

@ -1,10 +1,10 @@
require 'stringio'
module Puma module Puma
# Provides an IO-like object that always appears to contain no data. # 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. # Used as the value for rack.input when the request has no body.
# #
class NullIO class NullIO
# Always returns nil # Always returns nil
# #
def gets def gets
@ -16,10 +16,10 @@ module Puma
def each def each
end end
# Always returns nil # Mimics IO#read with no data
# #
def read(count) def read(*args)
nil StringIO.new('').read(*args)
end end
# Does nothing # Does nothing

31
test/test_null_io.rb Normal file
View file

@ -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