Skip to content

Commit

Permalink
[inaka#102] Allow proplist headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tothlac committed Dec 8, 2015
1 parent 13b6b44 commit 195060f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/shotgun.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
-type connection() :: pid().
-type http_verb() :: get | post | head | delete | patch | put | options.
-type uri() :: iodata().
-type headers() :: #{}.
-type headers() :: #{} | proplists:proplist().
-type body() :: iodata() | body_chunked.
-type options() ::
#{ async => boolean()
Expand Down Expand Up @@ -627,7 +627,7 @@ manage_chunk(IsFin, StreamRef, Data,
NewState.

%% @private
-spec process_options(map(), map(), http_verb()) -> map().
-spec process_options(map(), headers(), http_verb()) -> map().
process_options(Options, HeadersMap, HttpVerb) ->
Headers = basic_auth_header(HeadersMap),
HandleEvent = maps:get(handle_event, Options, undefined),
Expand All @@ -647,14 +647,15 @@ process_options(Options, HeadersMap, HttpVerb) ->
}.

%% @private
-spec basic_auth_header(map()) -> list().
-spec basic_auth_header(headers()) -> proplists:proplist().
basic_auth_header(Headers) when is_map(Headers) ->
basic_auth_header(maps:to_list(Headers));
basic_auth_header(Headers) ->
case maps:get(basic_auth, Headers, undefined) of
undefined ->
maps:to_list(Headers);
{User, Password} ->
HeadersClean = maps:remove(basic_auth, Headers),
HeadersList = maps:to_list(HeadersClean),
case lists:keyfind(basic_auth, 1, Headers) of
false ->
Headers;
{_, {User, Password}} = Res ->
HeadersList = lists:delete(Res, Headers),
Base64 = encode_basic_auth(User, Password),
BasicAuth = {<<"Authorization">>, <<"Basic ", Base64/binary>>},
[BasicAuth | HeadersList]
Expand Down
5 changes: 5 additions & 0 deletions test/shotgun_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ basic_auth(Config) ->
{ok, Response2} = shotgun:get(Conn, "/basic-auth", Headers),
#{status_code := 200} = Response2,

%% try the same request using proplists
{ok, Response3} = shotgun:get(Conn, "/basic-auth",
[{basic_auth, {"user", "pass"}}]),
#{status_code := 200} = Response3,

{comment, ""}.

-spec get(shotgun_test_utils:config()) -> {comment, string()}.
Expand Down

0 comments on commit 195060f

Please sign in to comment.