From 7f7ca1aa00343c1f2281e046987879dd8c5a8cb7 Mon Sep 17 00:00:00 2001
From: Jan Dillmann <j@n-dillmann.de>
Date: Mon, 20 Jan 2014 15:31:59 +0100
Subject: [PATCH] Decode HTTP basic auth header and generate cURL command with
 user and password

---
 lib/rspec_api_documentation/curl.rb | 11 ++++++++++-
 spec/curl_spec.rb                   | 14 ++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

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