io_splice 4.1.0 - copy_stream enhancement for 1.9
[ruby_io_splice.git] / test / test_rack_file_compat.rb
blob5505262547b59dcedf45bec01388e2a609aa2648
1 # -*- encoding: binary -*-
2 require "rack"
3 require "test/unit"
4 require "socket"
5 require "io/splice"
7 class TestRackFileCompat < Test::Unit::TestCase
8   def setup
9     @app = Rack::File.new(File.dirname(__FILE__))
10     @req = Rack::MockRequest.new(@app)
11     @base_file = File.basename(__FILE__)
12     @r, @w = UNIXSocket.pair
13   end
15   def teardown
16     [ @r, @w ].each { |io| io.closed? or io.close }
17   end
19   def test_get_rack_file
20     env = Rack::MockRequest.env_for "http://example.com/#@base_file"
21     status, headers, body = @app.call(env)
22     assert_equal 200, status.to_i
23     headers.each { |k,v|
24       assert_instance_of String, k.to_str
25       assert_instance_of String, v.to_str
26     }
27     thr = Thread.new { @r.read(File.size(__FILE__)) }
28     assert_equal File.size(__FILE__), IO::Splice.copy_stream(body, @w)
29     assert_equal File.read(__FILE__), thr.value
30   end
31 end if IO.respond_to?(:copy_stream)