Browse files

Merge branch 'fix_content_type' of git://github.com/dry/etest_http in…

…to dry-fix_content_type

Conflicts:
	src/etest_http.erl
  • Loading branch information...
2 parents 709f826 + 74b5293 commit 5cd35631e9051989abe0b9268ab46f5a69b4a8ed @johannesh johannesh committed Aug 9, 2013
Showing with 28 additions and 4 deletions.
  1. +28 −4 src/etest_http.erl
View
32 src/etest_http.erl
@@ -13,10 +13,11 @@ init() -> inets:start(), ok.
%% returning a `etest_http_res` record to assert upon.
perform_request(Method, Url, Headers, Queries, Body) ->
FullUrl = Url ++ query_string(Queries),
+ Hdrs = normalise_headers(Headers),
Request = case Method of
- get -> {FullUrl, Headers};
- delete -> {FullUrl, Headers};
- _ -> {FullUrl, Headers, "", Body}
+ get -> {FullUrl, Hdrs};
+ delete -> {FullUrl, Hdrs};
+ _ -> {FullUrl, Hdrs, content_type(Hdrs), Body}
end,
case httpc:request(Method, Request, [{autoredirect, false}], []) of
@@ -31,7 +32,30 @@ perform_request(Method, Url, Headers, Queries, Body) ->
end.
-%% @doc Generates a query string to be appended to a URL.
+%% @doc Normalises headers - binary/string/atom -> string.
+normalise_headers(Headers) ->
+ [{norm_k(Key), norm_v(Value)} || {Key, Value} <- Headers].
+
+
+%% @doc Normalises headers and extracts content-type.
+content_type(Headers) ->
+ proplists:get_value("content-type", Headers,
+ "application/x-www-form-urlencoded").
+
+
+%% @doc Reformat all header keys to lowercase strings.
+norm_k(K) when is_binary(K) -> norm_k(binary_to_list(K));
+norm_k(K) when is_list(K) -> string:to_lower(K);
+norm_k(K) when is_atom(K) -> atom_to_list(K).
+
+
+%% @doc Reformat all header values to strings.
+norm_v(V) when is_binary(V) -> norm_k(binary_to_list(V));
+norm_v(V) when is_atom(V) -> atom_to_list(V);
+norm_v(V) -> V.
+
+
+%% @doc Generates a query string to be appended to an URL.
query_string([Head|Tail]) ->
"?" ++ [make_query(Head) | [["&", make_query(Elem)] || Elem <- Tail]];

0 comments on commit 5cd3563

Please sign in to comment.