Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/rspec_api_documentation/curl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_support/core_ext/object/to_query'
require 'base64'

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

def headers
filter_headers(super).map do |k, v|
"\\\n\t-H \"#{format_full_header(k, v)}\""
if k == "HTTP_AUTHORIZATION" && v =~ /^Basic/
"\\\n\t-u #{format_auth_header(v)}"
else
"\\\n\t-H \"#{format_full_header(k, v)}\""
end
end.join(" ")
end

Expand All @@ -55,6 +60,10 @@ def post_data

private

def format_auth_header(value)
::Base64.decode64(value.split(' ', 2).last || '')
end

def format_header(header)
header.gsub(/^HTTP_/, '').titleize.split.join("-")
end
Expand Down
14 changes: 14 additions & 0 deletions spec/curl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,18 @@
curl.output(host)
end
end

describe "Basic Authentication" do
let(:method) { "GET" }
let(:path) { "/" }
let(:data) { "" }
let(:headers) do
{
"HTTP_AUTHORIZATION" => %{Basic dXNlckBleGFtcGxlLm9yZzpwYXNzd29yZA==},
}
end

it { should_not =~ /-H "Authorization: Basic dXNlckBleGFtcGxlLm9yZzpwYXNzd29yZA=="/ }
it { should =~ /-u user@example\.org:password/ }
end
end