Skip to content

Commit

Permalink
Move tests from EUnit to Common Test
Browse files Browse the repository at this point in the history
Notice the use of README.md from the linked `priv` folder

Files were mostly renamed and then:
- added init/end_per_suite (from setup/teardown)
- added init/end_per_testcase (from elli_test_)
- removed _test suffix from exported function names
- moved rebar.config test options to test profile
- added a shareable (example + tests) symbolic link, used as
  filename:join(code:priv_dir(elli), "README.md") for
  single reference...
  • Loading branch information
paulo-ferraz-oliveira committed Jun 13, 2024
1 parent 1efab2e commit 198a6c9
Show file tree
Hide file tree
Showing 16 changed files with 473 additions and 488 deletions.
1 change: 1 addition & 0 deletions priv/README.md
24 changes: 14 additions & 10 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@
]}
]},
{test, [
{deps, [{hackney, "1.17.4"}]}
{deps, [{hackney, "1.17.4"}]},
{extra_src_dirs, [
{"test", [
{recursive, true}
]}
]},
{cover_enabled, true},
{cover_export_enabled, true},
{cover_excl_mods, [
elli_handler
]},
{covertool, [{coverdata_files, ["ct.coverdata"]}]},
{cover_opts, [verbose]},
{ct_opts, [{ct_hooks, [cth_surefire]}]}
]}
]}.

Expand All @@ -30,13 +43,4 @@
%{provider_hooks, [{pre, [{eunit, lint}]}]}.
{dialyzer, [{plt_extra_apps, [ssl]}]}.

{cover_enabled, true}.
{cover_export_enabled, true}.
{cover_excl_mods, [
elli_handler
]}.
{covertool, [{coverdata_files, ["ct.coverdata"]}]}.

{post_hooks, [{edoc, "doc/build.sh"}]}.

{ct_opts, [{ct_hooks, [cth_surefire]}]}.
6 changes: 3 additions & 3 deletions src/elli_example_callback.erl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ handle('GET', [<<"decoded-list">>], Req) ->
handle('GET', [<<"sendfile">>], _Req) ->
%% Returning {file, "/path/to/file"} instead of the body results
%% in Elli using sendfile.
F = "README.md",
F = filename:join(code:priv_dir(elli), "README.md"),
{ok, [], {file, F}};

handle('GET', [<<"send_no_file">>], _Req) ->
Expand All @@ -145,14 +145,14 @@ handle('GET', [<<"send_no_file">>], _Req) ->
{ok, [], {file, F}};

handle('GET', [<<"sendfile">>, <<"error">>], _Req) ->
F = "test",
F = code:priv_dir(elli),
{ok, [], {file, F}};

handle('GET', [<<"sendfile">>, <<"range">>], Req) ->
%% Read the Range header of the request and use the normalized
%% range with sendfile, otherwise send the entire file when
%% no range is present, or respond with a 416 if the range is invalid.
F = "README.md",
F = filename:join(code:priv_dir(elli), "README.md"),
{ok, [], {file, F, elli_request:get_range(Req)}};

handle('GET', [<<"compressed">>], _Req) ->
Expand Down
22 changes: 4 additions & 18 deletions src/elli_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

-export_type([version/0]).

-ifdef(TEST).
-export([get_body/5]).
-endif.

%% @type version(). HTTP version as a tuple, i.e. `{0, 9} | {1, 0} | {1, 1}'.
-type version() :: {0, 9} | {1, 0} | {1, 1}.

Expand Down Expand Up @@ -893,21 +897,3 @@ status(510) -> <<"510 Not Extended">>;
status(511) -> <<"511 Network Authentication Required">>;
status(I) when is_integer(I), I >= 100, I < 1000 -> list_to_binary(io_lib:format("~B Status", [I]));
status(B) when is_binary(B) -> B.


%%
%% UNIT TESTS
%%

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

get_body_test() ->
Socket = undefined,
Headers = [{<<"Content-Length">>, <<" 42 ">>}],
Buffer = binary:copy(<<".">>, 42),
Opts = [],
Callback = {no_mod, []},
?assertMatch({Buffer, <<>>},
get_body(Socket, Headers, Buffer, Opts, Callback)).
-endif.
20 changes: 0 additions & 20 deletions src/elli_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,3 @@ call(Method, Path, Headers, Body, Opts) ->
Body, {1, 1}, undefined, {Callback, CallbackArgs}),
ok = Callback:handle_event(elli_startup, [], CallbackArgs),
Callback:handle(Req, CallbackArgs).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

hello_world_test() ->
?assertMatch({ok, [], <<"Hello World!">>},
elli_test:call('GET', <<"/hello/world/">>, [], <<>>,
?EXAMPLE_CONF)),
?assertMatch({ok, [], <<"Hello Test1">>},
elli_test:call('GET', <<"/hello/?name=Test1">>, [], <<>>,
?EXAMPLE_CONF)),
?assertMatch({ok,
[{<<"content-type">>,
<<"application/json; charset=ISO-8859-1">>}],
<<"{\"name\" : \"Test2\"}">>},
elli_test:call('GET', <<"/type?name=Test2">>,
[{<<"accept">>, <<"application/json">>}], <<>>,
?EXAMPLE_CONF)).

-endif. %% TEST
Loading

0 comments on commit 198a6c9

Please sign in to comment.