examples_SUITE.erl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. %% Copyright (c) 2016, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(examples_SUITE).
  15. -compile(export_all).
  16. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [gun_open/1]).
  19. %% ct.
  20. all() ->
  21. ct_helper:all(?MODULE).
  22. %% Remove environment variables inherited from Erlang.mk.
  23. init_per_suite(Config) ->
  24. os:unsetenv("ERLANG_MK_TMP"),
  25. os:unsetenv("APPS_DIR"),
  26. os:unsetenv("DEPS_DIR"),
  27. os:unsetenv("ERL_LIBS"),
  28. Config.
  29. end_per_suite(_) ->
  30. ok.
  31. %% Compile, start and stop releases.
  32. do_get_paths(Example0) ->
  33. Example = atom_to_list(Example0),
  34. {ok, CWD} = file:get_cwd(),
  35. Dir = CWD ++ "/../../examples/" ++ Example,
  36. Rel = Dir ++ "/_rel/" ++ Example ++ "_example/bin/" ++ Example ++ "_example",
  37. Log = Dir ++ "/_rel/" ++ Example ++ "_example/log/erlang.log.1",
  38. {Dir, Rel, Log}.
  39. do_compile_and_start(Example) ->
  40. {Dir, Rel, _} = do_get_paths(Example),
  41. %% TERM=dumb disables relx coloring.
  42. ct:log("~s~n", [os:cmd("cd " ++ Dir ++ " && make distclean && TERM=dumb make all")]),
  43. ct:log("~s~n", [os:cmd(Rel ++ " stop")]),
  44. ct:log("~s~n", [os:cmd(Rel ++ " start")]),
  45. timer:sleep(2000),
  46. ok.
  47. do_stop(Example) ->
  48. {_, Rel, Log} = do_get_paths(Example),
  49. ct:log("~s~n", [os:cmd(Rel ++ " stop")]),
  50. ct:log("~s~n", [element(2, file:read_file(Log))]),
  51. ok.
  52. %% Fetch a response.
  53. do_get(Transport, Protocol, Path, Config) ->
  54. do_get(Transport, Protocol, Path, [], Config).
  55. do_get(Transport, Protocol, Path, ReqHeaders, Config) ->
  56. Port = case Transport of
  57. tcp -> 8080;
  58. ssl -> 8443
  59. end,
  60. ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
  61. Ref = gun:get(ConnPid, Path, ReqHeaders),
  62. case gun:await(ConnPid, Ref) of
  63. {response, nofin, Status, RespHeaders} ->
  64. {ok, Body} = gun:await_body(ConnPid, Ref),
  65. {Status, RespHeaders, Body};
  66. {response, fin, Status, RespHeaders} ->
  67. {Status, RespHeaders, <<>>}
  68. end.
  69. %% TCP and SSL Hello World.
  70. hello_world(Config) ->
  71. doc("Hello World example."),
  72. try
  73. do_compile_and_start(hello_world),
  74. do_hello_world(tcp, http, Config),
  75. do_hello_world(tcp, http2, Config)
  76. after
  77. do_stop(hello_world)
  78. end.
  79. ssl_hello_world(Config) ->
  80. doc("SSL Hello World example."),
  81. try
  82. do_compile_and_start(ssl_hello_world),
  83. do_hello_world(ssl, http, Config),
  84. do_hello_world(ssl, http2, Config)
  85. after
  86. do_stop(ssl_hello_world)
  87. end.
  88. do_hello_world(Transport, Protocol, Config) ->
  89. {200, _, <<"Hello world!">>} = do_get(Transport, Protocol, "/", Config),
  90. ok.
  91. %% Echo GET.
  92. echo_get(Config) ->
  93. doc("GET parameter echo example."),
  94. try
  95. do_compile_and_start(echo_get),
  96. do_echo_get(tcp, http, Config),
  97. do_echo_get(tcp, http2, Config)
  98. after
  99. do_stop(echo_get)
  100. end.
  101. do_echo_get(Transport, Protocol, Config) ->
  102. {200, _, <<"this is fun">>} = do_get(Transport, Protocol, "/?echo=this+is+fun", Config),
  103. ok.
  104. %% Echo POST.
  105. echo_post(Config) ->
  106. doc("POST parameter echo example."),
  107. try
  108. do_compile_and_start(echo_post),
  109. do_echo_post(tcp, http, Config),
  110. do_echo_post(tcp, http2, Config)
  111. after
  112. do_stop(echo_post)
  113. end.
  114. do_echo_post(Transport, Protocol, Config) ->
  115. ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
  116. Ref = gun:post(ConnPid, "/", [
  117. {<<"content-type">>, <<"application/octet-stream">>}
  118. ], <<"echo=this+is+fun">>),
  119. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  120. {ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
  121. ok.
  122. %% REST Hello World.
  123. rest_hello_world(Config) ->
  124. doc("REST Hello World example."),
  125. try
  126. do_compile_and_start(rest_hello_world),
  127. do_rest_hello_world(tcp, http, Config),
  128. do_rest_hello_world(tcp, http2, Config)
  129. after
  130. do_stop(rest_hello_world)
  131. end.
  132. do_rest_hello_world(Transport, Protocol, Config) ->
  133. << "<html>", _/bits >> = do_rest_get(Transport, Protocol, "/", undefined, Config),
  134. << "REST Hello World as text!" >> = do_rest_get(Transport, Protocol, "/", <<"text/plain">>, Config),
  135. << "{\"rest\": \"Hello World!\"}" >> = do_rest_get(Transport, Protocol, "/", <<"application/json">>, Config),
  136. not_acceptable = do_rest_get(Transport, Protocol, "/", <<"text/css">>, Config),
  137. ok.
  138. do_rest_get(Transport, Protocol, Path, Accept, Config) ->
  139. ReqHeaders = case Accept of
  140. undefined -> [];
  141. _ -> [{<<"accept">>, Accept}]
  142. end,
  143. case do_get(Transport, Protocol, Path, ReqHeaders, Config) of
  144. {200, RespHeaders, Body} ->
  145. Accept = case Accept of
  146. undefined -> undefined;
  147. _ ->
  148. {_, ContentType} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
  149. ContentType
  150. end,
  151. Body;
  152. {406, _, _} ->
  153. not_acceptable
  154. end.
  155. %% File server.
  156. file_server(Config) ->
  157. doc("File server example with directory listing."),
  158. try
  159. do_compile_and_start(file_server),
  160. do_file_server(tcp, http, Config),
  161. do_file_server(tcp, http2, Config)
  162. after
  163. do_stop(file_server)
  164. end.
  165. do_file_server(Transport, Protocol, Config) ->
  166. %% Directory.
  167. {200, DirHeaders, <<"<!DOCTYPE html><html>", _/bits >>} = do_get(Transport, Protocol, "/", Config),
  168. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, DirHeaders),
  169. _ = do_rest_get(Transport, Protocol, "/", <<"application/json">>, Config),
  170. %% Files.
  171. {200, _, _} = do_get(Transport, Protocol, "/small.mp4", Config),
  172. {200, _, _} = do_get(Transport, Protocol, "/small.ogv", Config),
  173. {200, _, _} = do_get(Transport, Protocol, "/test.txt", Config),
  174. {200, _, _} = do_get(Transport, Protocol, "/video.html", Config),
  175. ok.