Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #170 from jugyo/patch-1

WebMock::RackResponse supports basic auth
  • Loading branch information...
commit 352d653e5f5c61a9a16fe54c6561c49bd7f6a7c7 2 parents 4a0382d + 48ae511
@bblimke authored
Showing with 23 additions and 0 deletions.
  1. +2 −0  lib/webmock/rack_response.rb
  2. +21 −0 spec/unit/rack_response_spec.rb
View
2  lib/webmock/rack_response.rb
@@ -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
View
21 spec/unit/rack_response_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.