WebMock::RackResponse supports basic auth #170

Merged
merged 2 commits into from Mar 30, 2012
Jump to file or symbol
Failed to load files and symbols.
+23 −0
Split
@@ -38,6 +38,8 @@ def build_rack_env(request)
'SERVER_NAME' => uri.host
}
+ env['HTTP_AUTHORIZATION'] = 'Basic ' + [uri.userinfo].pack('m').delete("\r\n") if uri.userinfo
+
# Rack-specific variables
env['rack.input'] = StringIO.new(body)
env['rack.version'] = Rack::VERSION
@@ -4,6 +4,11 @@
before :each do
@rack_response = WebMock::RackResponse.new(MyRackApp)
@locked_rack_response = WebMock::RackResponse.new(MyLockedRackApp)
+ @rack_response_with_basic_auth = WebMock::RackResponse.new(
+ Rack::Auth::Basic.new(MyRackApp) do |username, password|
+ username == 'username' && password == 'password'
+ end
+ )
end
it "should hook up to a rack appliance" do
@@ -48,4 +53,20 @@
response = @rack_response.evaluate(request)
response.body.should include('Good to meet you, Jimmy!')
end
+
+ describe 'basic auth request' do
+ it 'should be failure when wrong credentials' do
+ request = WebMock::RequestSignature.new(:get, 'foo:bar@www.example.com')
+ response = @rack_response_with_basic_auth.evaluate(request)
+ response.status.first.should == 401
+ response.body.should_not include('This is my root!')
+ end
+
+ it 'should be success when valid credentials' do
+ request = WebMock::RequestSignature.new(:get, 'username:password@www.example.com')
+ response = @rack_response_with_basic_auth.evaluate(request)
+ response.status.first.should == 200
+ response.body.should include('This is my root!')
+ end
+ end
end