Skip to content

Commit 539ebe4

Browse files
committed
Merge pull request #108 from jandillmann/master
Decode HTTP basic auth header and generate cURL command with user and password
2 parents fe8bf36 + 7f7ca1a commit 539ebe4

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/rspec_api_documentation/curl.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'active_support/core_ext/object/to_query'
2+
require 'base64'
23

34
module RspecApiDocumentation
45
class Curl < Struct.new(:method, :path, :data, :headers)
@@ -40,7 +41,11 @@ def url
4041

4142
def headers
4243
filter_headers(super).map do |k, v|
43-
"\\\n\t-H \"#{format_full_header(k, v)}\""
44+
if k == "HTTP_AUTHORIZATION" && v =~ /^Basic/
45+
"\\\n\t-u #{format_auth_header(v)}"
46+
else
47+
"\\\n\t-H \"#{format_full_header(k, v)}\""
48+
end
4449
end.join(" ")
4550
end
4651

@@ -55,6 +60,10 @@ def post_data
5560

5661
private
5762

63+
def format_auth_header(value)
64+
::Base64.decode64(value.split(' ', 2).last || '')
65+
end
66+
5867
def format_header(header)
5968
header.gsub(/^HTTP_/, '').titleize.split.join("-")
6069
end

spec/curl_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,18 @@
171171
curl.output(host)
172172
end
173173
end
174+
175+
describe "Basic Authentication" do
176+
let(:method) { "GET" }
177+
let(:path) { "/" }
178+
let(:data) { "" }
179+
let(:headers) do
180+
{
181+
"HTTP_AUTHORIZATION" => %{Basic dXNlckBleGFtcGxlLm9yZzpwYXNzd29yZA==},
182+
}
183+
end
184+
185+
it { should_not =~ /-H "Authorization: Basic dXNlckBleGFtcGxlLm9yZzpwYXNzd29yZA=="/ }
186+
it { should =~ /-u user@example\.org:password/ }
187+
end
174188
end

0 commit comments

Comments
 (0)