Skip to content

Commit

Permalink
Merge pull request #108 from jandillmann/master
Browse files Browse the repository at this point in the history
Decode HTTP basic auth header and generate cURL command with user and password
  • Loading branch information
oestrich committed Jan 21, 2014
2 parents fe8bf36 + 7f7ca1a commit 539ebe4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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

0 comments on commit 539ebe4

Please sign in to comment.