From eb2463ce85ec3af51f1375799079f52e7353c5ca Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Wed, 10 Oct 2012 17:46:19 +0400 Subject: [PATCH] Added tests for aws_request/6 and aws_request/8 (depends on meck). Change Makefile --- .gitignore | 2 ++ Makefile | 16 +++++++++--- src/erlcloud_aws.erl | 7 +++++- test/erlcloud_aws_tests.erl | 49 ++++++++++++++++++++++++++----------- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 86df3f28..a3db9623 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ _build ebin/*.beam ebin/*.app +.eunit/ +deps/ diff --git a/Makefile b/Makefile index cc3cb877..9d732e0c 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/src/erlcloud_aws.erl b/src/erlcloud_aws.erl index c5171e30..3f004980 100644 --- a/src/erlcloud_aws.erl +++ b/src/erlcloud_aws.erl @@ -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 = @@ -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. diff --git a/test/erlcloud_aws_tests.erl b/test/erlcloud_aws_tests.erl index ed00bc9d..257f9006 100644 --- a/test/erlcloud_aws_tests.erl +++ b/test/erlcloud_aws_tests.erl @@ -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)].