examples_SUITE.erl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. %% Chunked Hello World.
  92. chunked_hello_world(Config) ->
  93. doc("Chunked Hello World example."),
  94. try
  95. do_compile_and_start(chunked_hello_world),
  96. do_chunked_hello_world(tcp, http, Config),
  97. do_chunked_hello_world(tcp, http2, Config)
  98. after
  99. do_stop(chunked_hello_world)
  100. end.
  101. do_chunked_hello_world(Transport, Protocol, Config) ->
  102. ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
  103. Ref = gun:get(ConnPid, "/"),
  104. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  105. %% We expect to receive a chunk every second, three total.
  106. {data, nofin, <<"Hello\r\n">>} = gun:await(ConnPid, Ref, 2000),
  107. {data, nofin, <<"World\r\n">>} = gun:await(ConnPid, Ref, 2000),
  108. {data, IsFin, <<"Chunked!\r\n">>} = gun:await(ConnPid, Ref, 2000),
  109. %% We may get an extra empty chunk (last chunk for HTTP/1.1,
  110. %% empty DATA frame with the FIN bit set for HTTP/2).
  111. case IsFin of
  112. fin -> ok;
  113. nofin ->
  114. {data, fin, <<>>} = gun:await(ConnPid, Ref, 500),
  115. ok
  116. end.
  117. %% Echo GET.
  118. echo_get(Config) ->
  119. doc("GET parameter echo example."),
  120. try
  121. do_compile_and_start(echo_get),
  122. do_echo_get(tcp, http, Config),
  123. do_echo_get(tcp, http2, Config)
  124. after
  125. do_stop(echo_get)
  126. end.
  127. do_echo_get(Transport, Protocol, Config) ->
  128. {200, _, <<"this is fun">>} = do_get(Transport, Protocol, "/?echo=this+is+fun", Config),
  129. ok.
  130. %% Echo POST.
  131. echo_post(Config) ->
  132. doc("POST parameter echo example."),
  133. try
  134. do_compile_and_start(echo_post),
  135. do_echo_post(tcp, http, Config),
  136. do_echo_post(tcp, http2, Config)
  137. after
  138. do_stop(echo_post)
  139. end.
  140. do_echo_post(Transport, Protocol, Config) ->
  141. ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
  142. Ref = gun:post(ConnPid, "/", [
  143. {<<"content-type">>, <<"application/octet-stream">>}
  144. ], <<"echo=this+is+fun">>),
  145. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  146. {ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
  147. ok.
  148. %% REST Hello World.
  149. rest_hello_world(Config) ->
  150. doc("REST Hello World example."),
  151. try
  152. do_compile_and_start(rest_hello_world),
  153. do_rest_hello_world(tcp, http, Config),
  154. do_rest_hello_world(tcp, http2, Config)
  155. after
  156. do_stop(rest_hello_world)
  157. end.
  158. do_rest_hello_world(Transport, Protocol, Config) ->
  159. << "<html>", _/bits >> = do_rest_get(Transport, Protocol,
  160. "/", undefined, undefined, Config),
  161. << "REST Hello World as text!" >> = do_rest_get(Transport, Protocol,
  162. "/", <<"text/plain">>, undefined, Config),
  163. << "{\"rest\": \"Hello World!\"}" >> = do_rest_get(Transport, Protocol,
  164. "/", <<"application/json">>, undefined, Config),
  165. not_acceptable = do_rest_get(Transport, Protocol,
  166. "/", <<"text/css">>, undefined, Config),
  167. ok.
  168. do_rest_get(Transport, Protocol, Path, Accept, Auth, Config) ->
  169. ReqHeaders0 = case Accept of
  170. undefined -> [];
  171. _ -> [{<<"accept">>, Accept}]
  172. end,
  173. ReqHeaders = case Auth of
  174. undefined -> ReqHeaders0;
  175. _ -> [{<<"authorization">>, [<<"Basic ">>, base64:encode(Auth)]}|ReqHeaders0]
  176. end,
  177. case do_get(Transport, Protocol, Path, ReqHeaders, Config) of
  178. {200, RespHeaders, Body} ->
  179. Accept = case Accept of
  180. undefined -> undefined;
  181. _ ->
  182. {_, ContentType} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
  183. ContentType
  184. end,
  185. Body;
  186. {401, _, _} ->
  187. unauthorized;
  188. {406, _, _} ->
  189. not_acceptable
  190. end.
  191. %% REST basic auth.
  192. rest_basic_auth(Config) ->
  193. doc("REST basic authorization example."),
  194. try
  195. do_compile_and_start(rest_basic_auth),
  196. do_rest_basic_auth(tcp, http, Config),
  197. do_rest_basic_auth(tcp, http2, Config)
  198. after
  199. do_stop(rest_basic_auth)
  200. end.
  201. do_rest_basic_auth(Transport, Protocol, Config) ->
  202. unauthorized = do_rest_get(Transport, Protocol, "/", undefined, undefined, Config),
  203. <<"Hello, Alladin!\n">> = do_rest_get(Transport, Protocol, "/", undefined, "Alladin:open sesame", Config),
  204. ok.
  205. %% File server.
  206. file_server(Config) ->
  207. doc("File server example with directory listing."),
  208. try
  209. do_compile_and_start(file_server),
  210. do_file_server(tcp, http, Config),
  211. do_file_server(tcp, http2, Config)
  212. after
  213. do_stop(file_server)
  214. end.
  215. do_file_server(Transport, Protocol, Config) ->
  216. %% Directory.
  217. {200, DirHeaders, <<"<!DOCTYPE html><html>", _/bits >>} = do_get(Transport, Protocol, "/", Config),
  218. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, DirHeaders),
  219. _ = do_rest_get(Transport, Protocol, "/", <<"application/json">>, undefined, Config),
  220. %% Files.
  221. {200, _, _} = do_get(Transport, Protocol, "/small.mp4", Config),
  222. {200, _, _} = do_get(Transport, Protocol, "/small.ogv", Config),
  223. {200, _, _} = do_get(Transport, Protocol, "/test.txt", Config),
  224. {200, _, _} = do_get(Transport, Protocol, "/video.html", Config),
  225. ok.
  226. %% Markdown middleware.
  227. markdown_middleware(Config) ->
  228. doc("Markdown middleware example."),
  229. try
  230. do_compile_and_start(markdown_middleware),
  231. do_markdown_middleware(tcp, http, Config),
  232. do_markdown_middleware(tcp, http2, Config)
  233. after
  234. do_stop(markdown_middleware)
  235. end.
  236. do_markdown_middleware(Transport, Protocol, Config) ->
  237. {200, Headers, <<"<h1>", _/bits >>} = do_get(Transport, Protocol, "/video.html", Config),
  238. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  239. ok.
  240. %% Websocket.
  241. websocket(_) ->
  242. doc("Websocket example."),
  243. try
  244. do_compile_and_start(websocket),
  245. %% We can only initiate a Websocket connection from HTTP/1.1.
  246. {ok, Pid} = gun:open("127.0.0.1", 8080, #{protocols => [http], retry => 0}),
  247. {ok, http} = gun:await_up(Pid),
  248. _ = monitor(process, Pid),
  249. gun:ws_upgrade(Pid, "/websocket", [], #{compress => true}),
  250. receive
  251. {gun_ws_upgrade, Pid, ok, _} ->
  252. ok;
  253. Msg1 ->
  254. exit({connection_failed, Msg1})
  255. end,
  256. gun:ws_send(Pid, {text, <<"hello">>}),
  257. receive
  258. {gun_ws, Pid, {text, <<"That's what she said! hello">>}} ->
  259. ok;
  260. Msg2 ->
  261. exit({receive_failed, Msg2})
  262. end,
  263. gun:ws_send(Pid, close)
  264. after
  265. do_stop(websocket)
  266. end.