diff --git a/test/test_auth.rb b/test/test_auth.rb index a82090e..82cec34 100644 --- a/test/test_auth.rb +++ b/test/test_auth.rb @@ -102,12 +102,15 @@ def setup_server end def do_basic_auth(req, res) + # Drain uploads before an authentication challenge can close the connection. + req.body @basic_auth.authenticate(req, res) res['content-type'] = 'text/plain' res.body = 'basic_auth OK' end def do_digest_auth(req, res) + req.body @digest_auth.authenticate(req, res) res['content-type'] = 'text/plain' res['x-query'] = req.body @@ -115,6 +118,7 @@ def do_digest_auth(req, res) end def do_digest_sess_auth(req, res) + req.body @digest_sess_auth.authenticate(req, res) res['content-type'] = 'text/plain' res['x-query'] = req.body diff --git a/test/test_auth_fixture.rb b/test/test_auth_fixture.rb new file mode 100644 index 0000000..e29fddf --- /dev/null +++ b/test/test_auth_fixture.rb @@ -0,0 +1,42 @@ +require File.expand_path('test_auth', File.dirname(__FILE__)) + +class TestAuthFixture < Test::Unit::TestCase + Request = Struct.new(:events) do + def body + events << :read_body + 'upload body' + end + end + + Authenticator = Struct.new(:events) do + def authenticate(_request, _response) + events << :authenticate + raise WEBrick::HTTPStatus::Unauthorized + end + end + + def test_basic_auth_drains_body_before_challenge + assert_body_drained_before_challenge(:basic_auth) + end + + def test_digest_auth_drains_body_before_challenge + assert_body_drained_before_challenge(:digest_auth) + end + + def test_digest_sess_auth_drains_body_before_challenge + assert_body_drained_before_challenge(:digest_sess_auth) + end + + private + + def assert_body_drained_before_challenge(kind) + events = [] + fixture = TestAuth.allocate + fixture.instance_variable_set("@#{kind}", Authenticator.new(events)) + + assert_raise(WEBrick::HTTPStatus::Unauthorized) do + fixture.send("do_#{kind}", Request.new(events), Object.new) + end + assert_equal([:read_body, :authenticate], events) + end +end