Skip to content

Commit

Permalink
fix: gracefully handle display of username that causes InvalidCompone…
Browse files Browse the repository at this point in the history
…ntError to be raised when composing a URI

For: pact-foundation/pact-net#289
  • Loading branch information
bethesque committed May 1, 2021
1 parent bb0ddca commit cecb98f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/pact/provider/pact_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def password

def to_s
if basic_auth? && http_or_https_uri?
URI(@uri).tap { |x| x.userinfo="#{username}:*****"}.to_s
begin
URI(@uri).tap { |x| x.userinfo="#{username}:*****"}.to_s
rescue URI::InvalidComponentError
URI(@uri).tap { |x| x.userinfo="*****:*****"}.to_s
end
elsif personal_access_token? && http_or_https_uri?
URI(@uri).tap { |x| x.userinfo="*****"}.to_s
else
Expand All @@ -45,7 +49,7 @@ def to_s
private def http_or_https_uri?
uri.start_with?('http://', 'https://')
end

end
end
end
8 changes: 8 additions & 0 deletions spec/lib/pact/provider/pact_uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
expect(pact_uri.to_s).to eq uri
end
end

context "with a username that has an @ symbol" do
let(:username) { "foo@bar" }

it 'does not blow up' do
expect(pact_uri.to_s).to eq "http://*****:*****@uri"
end
end
end

context 'with personal access token provided' do
Expand Down

0 comments on commit cecb98f

Please sign in to comment.