forked from wooga/etest_http
/
etest_http_test.erl
58 lines (42 loc) · 1.77 KB
/
etest_http_test.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
%% @author Johannes Huning <johannes.huning@wooga.com>
%% @doc Test of the assertions itself.
-module (etest_http_test).
-compile (export_all).
-include_lib ("etest/include/etest.hrl").
-include_lib ("etest_http/include/etest_http.hrl").
before_suite() ->
inets:start(),
{ok, Pid} = inets:start(httpd, [
{port, 59408},
{server_name, "etest_httpd"},
{server_root, "/tmp"},
{ipfamily, inet},
{document_root, "priv"} ]),
put(server_pid, Pid).
after_suite() ->
ok = inets:stop(httpd, get(server_pid)).
test_response_assertions() ->
Res = ?perform_get("http://localhost:59408/first.html"),
?assert_status(200, Res),
?assert_error({assert_status, _}, ?assert_status(400, Res)),
?assert_body_contains("Hello", Res),
?assert_error({assert_contains, _}, ?assert_body_contains("Olleh", Res)),
?assert_header("date", Res),
?assert_error({assert_header, _}, ?assert_header("X-Missing", Res)),
?assert_header_value("content-type", "text/html", Res),
?assert_error({assert_header_val, _},
?assert_header_value("content-type", "application/json", Res)).
test_json_assertions() ->
JsonStruct = etest_http_json:construct([
{foo, [bar, baz, 1, 2, 3]},
{bar, [{baz, bang}]}
]),
Res = ?perform_get("http://localhost:59408/second.json"),
?assert_json(JsonStruct, Res),
?assert_error({assert_equal, _}, ?assert_json([], Res)),
?assert_json_key(<<"foo">>, Res),
?assert_error({assert_json_key, _}, ?assert_json_key("baz", Res)),
?assert_json_value(<<"foo">>, [<<"bar">>, <<"baz">>, 1, 2, 3], Res),
?assert_json_value([<<"bar">>, <<"baz">>], <<"bang">>, Res),
?assert_error({assert_json_val, _},
?assert_json_value(<<"foo">>, undefined, Res)).