Skip to content

Commit

Permalink
Added tests for aws_request/6 and aws_request/8 (depends on meck). Ch…
Browse files Browse the repository at this point in the history
…ange Makefile
  • Loading branch information
couchemar committed Oct 10, 2012
1 parent d44cdee commit eb2463c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
_build
ebin/*.beam
ebin/*.app
.eunit/
deps/
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
all:
rebar compile
REBAR=$(shell which rebar || echo ./rebar)

get-deps:
@$(REBAR) get-deps

all: compile

clean:
rebar clean
@$(REBAR) clean

compile:
@$(REBAR) compile

eunit: compile
@$(REBAR) eunit skip_deps=true
7 changes: 6 additions & 1 deletion src/erlcloud_aws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ aws_request(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey

case Port of
undefined -> URL = [UProt, Host, Path];
_ -> URL = [UProt, Host, $:, Port, Path]
_ -> URL = [UProt, Host, $:, port_to_str(Port), Path]
end,

Response =
Expand Down Expand Up @@ -98,3 +98,8 @@ default_config() ->
Config ->
Config
end.

port_to_str(Port) when is_integer(Port) ->
integer_to_list(Port);
port_to_str(Port) when is_list(Port) ->
Port.
49 changes: 35 additions & 14 deletions test/erlcloud_aws_tests.erl
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
-module(erlcloud_aws_tests).
-include_lib("eunit/include/eunit.hrl").

request_test() ->
request_test_() ->
{foreach,
fun start/0,
fun stop/1,
[fun request_default_test/1,
fun request_prot_host_port_str_test/1,
fun request_prot_host_port_int_test/1]}.

start() ->
meck:new(httpc, [unstick]),
meck:expect(httpc, request, fun(_) -> {ok, {{0, 200, 0}, 0, ok}} end),
ok = erlcloud_aws:aws_request(get, "host", "/", [], "id", "key"),
[{_, {httpc, request, [Url]}, _}] = meck:history(httpc),
test_url(https, "host", 443, "/", Url),
ok.

stop(_) ->
meck:unload(httpc).

request_default_test(_) ->
ok = erlcloud_aws:aws_request(get, "host", "/", [], "id", "key"),
Url = get_url_from_history(meck:history(httpc)),
test_url(https, "host", 443, "/", Url).

request2_test() ->
meck:new(httpc, [unstick]),
meck:expect(httpc, request, fun(_) -> {ok, {{0, 200, 0}, 0, ok}} end),
request_prot_host_port_str_test(_) ->
ok = erlcloud_aws:aws_request(get, "http", "host1", "9999", "/path1", [], "id", "key"),
[{_, {httpc, request, [Url]}, _}] = meck:history(httpc),
test_url(http, "host1", 9999, "/path1", Url),
meck:unload(httpc).
Url = get_url_from_history(meck:history(httpc)),
test_url(http, "host1", 9999, "/path1", Url).

request_prot_host_port_int_test(_) ->
ok = erlcloud_aws:aws_request(get, "http", "host1", 9999, "/path1", [], "id", "key"),
Url = get_url_from_history(meck:history(httpc)),
test_url(http, "host1", 9999, "/path1", Url).

% ==================
% Internal functions
% ==================

get_url_from_history([{_, {httpc, request, [Url]}, _}]) ->
Url.

test_url(ExpScheme, ExpHost, ExpPort, ExpPath, Url) ->
{Scheme, _UserInfo, Host, Port, Path, _Query} = http_uri:parse(Url),
?assertEqual(ExpScheme, Scheme),
?assertEqual(ExpHost, Host),
?assertEqual(ExpPort, Port),
?assertEqual(ExpPath, Path).
[?_assertEqual(ExpScheme, Scheme),
?_assertEqual(ExpHost, Host),
?_assertEqual(ExpPort, Port),
?_assertEqual(ExpPath, Path)].

0 comments on commit eb2463c

Please sign in to comment.