Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

WebMock::RackResponse supports basic auth #170

Merged
merged 2 commits into from

2 participants

@jugyo

It sets the header 'HTTP_AUTHORIZATION' to rack env when basic auth request.

@bblimke
Owner

Looks good. Thanks for adding this support.
It only requires a spec now to be merged into master.

@jugyo

I wrote the spec :thumbsup:

@bblimke
Owner

Awesome.

@bblimke bblimke merged commit 352d653 into bblimke:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.