Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/test_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,23 @@ 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
res.body = 'digest_auth OK' + req.query_string.to_s
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
Expand Down
42 changes: 42 additions & 0 deletions test/test_auth_fixture.rb
Original file line number Diff line number Diff line change
@@ -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
Loading