diff --git a/lib/rspec_api_documentation/curl.rb b/lib/rspec_api_documentation/curl.rb index 9c418a6f..d00060a7 100644 --- a/lib/rspec_api_documentation/curl.rb +++ b/lib/rspec_api_documentation/curl.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/object/to_query' +require 'base64' module RspecApiDocumentation class Curl < Struct.new(:method, :path, :data, :headers) @@ -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 @@ -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 diff --git a/spec/curl_spec.rb b/spec/curl_spec.rb index 9dd0d863..275a60c1 100644 --- a/spec/curl_spec.rb +++ b/spec/curl_spec.rb @@ -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