You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.5 KiB

  1. -module(elli_middleware_tests).
  2. -include_lib("eunit/include/eunit.hrl").
  3. -include("elli_test.hrl").
  4. elli_test_() ->
  5. {setup,
  6. fun setup/0, fun teardown/1,
  7. [
  8. ?_test(hello_world()),
  9. ?_test(short_circuit()),
  10. ?_test(compress()),
  11. ?_test(no_callbacks())
  12. ]}.
  13. %%
  14. %% TESTS
  15. %%
  16. short_circuit() ->
  17. URL = "http://localhost:3002/middleware/short-circuit",
  18. Response = hackney:get(URL),
  19. ?assertMatch(<<"short circuit!">>, body(Response)).
  20. hello_world() ->
  21. URL = "http://localhost:3002/hello/world",
  22. Response = hackney:get(URL),
  23. ?assertMatch(<<"Hello World!">>, body(Response)).
  24. compress() ->
  25. Url = "http://localhost:3002/compressed",
  26. Headers = [{<<"Accept-Encoding">>, <<"gzip">>}],
  27. Response = hackney:get(Url, Headers),
  28. ?assertHeadersEqual([{<<"Connection">>, <<"Keep-Alive">>},
  29. {<<"Content-Encoding">>, <<"gzip">>},
  30. {<<"Content-Length">>, <<"41">>}],
  31. headers(Response)),
  32. ?assertEqual(binary:copy(<<"Hello World!">>, 86),
  33. zlib:gunzip(body(Response))),
  34. Response1 = hackney:get("http://localhost:3002/compressed"),
  35. ?assertHeadersEqual([{<<"Connection">>, <<"Keep-Alive">>},
  36. {<<"Content-Length">>, <<"1032">>}],
  37. headers(Response1)),
  38. ?assertEqual(iolist_to_binary(lists:duplicate(86, "Hello World!")),
  39. body(Response1)),
  40. Url2 = "http://localhost:3002/compressed-io_list",
  41. Headers2 = [{<<"Accept-Encoding">>, <<"gzip">>}],
  42. Response2 = hackney:get(Url2, Headers2),
  43. ?assertMatch(200, status(Response2)),
  44. ?assertHeadersEqual([{<<"Connection">>, <<"Keep-Alive">>},
  45. {<<"Content-Encoding">>, <<"gzip">>},
  46. {<<"Content-Length">>, <<"41">>}],
  47. headers(Response2)),
  48. ?assertEqual(binary:copy(<<"Hello World!">>, 86),
  49. zlib:gunzip(body(Response2))),
  50. Response3 = hackney:request("http://localhost:3002/compressed-io_list"),
  51. ?assertMatch(200, status(Response3)),
  52. ?assertHeadersEqual([{<<"Connection">>, <<"Keep-Alive">>},
  53. {<<"Content-Length">>, <<"1032">>}],
  54. headers(Response3)),
  55. ?assertEqual(iolist_to_binary(lists:duplicate(86, "Hello World!")),
  56. body(Response3)).
  57. no_callbacks() ->
  58. Response = hackney:get("http://localhost:3004/whatever"),
  59. ?assertMatch(404, status(Response)),
  60. ?assertMatch(<<"Not Found">>, body(Response)).
  61. %%
  62. %% HELPERS
  63. %%
  64. setup() ->
  65. application:start(crypto),
  66. application:start(public_key),
  67. application:start(ssl),
  68. {ok, _} = application:ensure_all_started(hackney),
  69. Config = [
  70. {mods, [
  71. {elli_access_log, [{name, elli_syslog},
  72. {ip, "127.0.0.1"},
  73. {port, 514}]},
  74. {elli_example_middleware, []},
  75. {elli_middleware_compress, []},
  76. {elli_example_callback, []}
  77. ]}
  78. ],
  79. {ok, P1} = elli:start_link([{callback, elli_middleware},
  80. {callback_args, Config},
  81. {port, 3002}]),
  82. unlink(P1),
  83. {ok, P2} = elli:start_link([{callback, elli_middleware},
  84. {callback_args, [{mods, []}]},
  85. {port, 3004}]),
  86. unlink(P2),
  87. [P1, P2].
  88. teardown(Pids) ->
  89. [elli:stop(P) || P <- Pids].