/
elli_middleware_tests.erl
104 lines (78 loc) · 3.04 KB
/
elli_middleware_tests.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-module(elli_middleware_tests).
-include_lib("eunit/include/eunit.hrl").
-include("elli.hrl").
elli_test_() ->
{setup,
fun setup/0, fun teardown/1,
[
?_test(hello_world()),
?_test(short_circuit()),
?_test(compress())
]}.
%%
%% TESTS
%%
short_circuit() ->
URL = "http://localhost:3002/middleware/short-circuit",
{ok, Response} = httpc:request(URL),
?assertEqual("short circuit!", body(Response)).
hello_world() ->
URL = "http://localhost:3002/hello/world",
{ok, Response} = httpc:request(URL),
?assertEqual("Hello World!", body(Response)).
compress() ->
{ok, Response} = httpc:request(get, {"http://localhost:3002/compressed",
[{"Accept-Encoding", "gzip"}]}, [], []),
?assertEqual(200, status(Response)),
?assertEqual([{"connection", "Keep-Alive"},
{"content-encoding", "gzip"},
{"content-length", "41"}], headers(Response)),
?assertEqual(binary:copy(<<"Hello World!">>, 86), zlib:gunzip(body(Response))),
{ok, Response1} = httpc:request("http://localhost:3002/compressed"),
?assertEqual(200, status(Response1)),
?assertEqual([{"connection", "Keep-Alive"},
{"content-length", "1032"}], headers(Response1)),
?assertEqual(lists:flatten(lists:duplicate(86, "Hello World!")), body(Response1)),
{ok, Response2} = httpc:request(get, {"http://localhost:3002/compressed-io_list",
[{"Accept-Encoding", "gzip"}]}, [], []),
?assertEqual(200, status(Response2)),
?assertEqual([{"connection", "Keep-Alive"},
{"content-encoding", "gzip"},
{"content-length", "41"}], headers(Response2)),
?assertEqual(binary:copy(<<"Hello World!">>, 86), zlib:gunzip(body(Response))),
{ok, Response3} = httpc:request("http://localhost:3002/compressed-io_list"),
?assertEqual(200, status(Response3)),
?assertEqual([{"connection", "Keep-Alive"},
{"content-length", "1032"}], headers(Response3)),
?assertEqual(lists:flatten(lists:duplicate(86, "Hello World!")), body(Response3)).
%%
%% HELPERS
%%
status({{_, Status, _}, _, _}) ->
Status.
body({_, _, Body}) ->
Body.
headers({_, Headers, _}) ->
lists:sort(Headers).
setup() ->
application:start(crypto),
application:start(public_key),
application:start(ssl),
inets:start(),
Config = [
{mods, [
{elli_access_log, [{name, elli_syslog},
{ip, "127.0.0.1"},
{port, 514}]},
{elli_example_middleware, []},
{elli_middleware_compress, []},
{elli_example_callback, []}
]}
],
{ok, P} = elli:start_link([{callback, elli_middleware},
{callback_args, Config},
{port, 3002}]),
unlink(P),
[P].
teardown(Pids) ->
[elli:stop(P) || P <- Pids].