examples_SUITE.erl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. %% TCP and SSL Hello World.
  53. hello_world(Config) ->
  54. doc("Hello World example."),
  55. try
  56. do_compile_and_start(hello_world),
  57. do_hello_world(tcp, http, Config),
  58. do_hello_world(tcp, http2, Config)
  59. after
  60. do_stop(hello_world)
  61. end.
  62. ssl_hello_world(Config) ->
  63. doc("SSL Hello World example."),
  64. try
  65. do_compile_and_start(ssl_hello_world),
  66. do_hello_world(ssl, http, Config),
  67. do_hello_world(ssl, http2, Config)
  68. after
  69. do_stop(ssl_hello_world)
  70. end.
  71. do_hello_world(Transport, Protocol, Config) ->
  72. Port = case Transport of
  73. tcp -> 8080;
  74. ssl -> 8443
  75. end,
  76. ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
  77. Ref = gun:get(ConnPid, "/"),
  78. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  79. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  80. ok.
  81. %% Echo GET.
  82. echo_get(Config) ->
  83. doc("GET parameter echo example."),
  84. try
  85. do_compile_and_start(echo_get),
  86. do_echo_get(tcp, http, Config),
  87. do_echo_get(tcp, http2, Config)
  88. after
  89. do_stop(echo_get)
  90. end.
  91. do_echo_get(Transport, Protocol, Config) ->
  92. ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
  93. Ref = gun:get(ConnPid, "/?echo=this+is+fun"),
  94. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  95. {ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
  96. ok.
  97. %% Echo POST.
  98. echo_post(Config) ->
  99. doc("POST parameter echo example."),
  100. try
  101. do_compile_and_start(echo_post),
  102. do_echo_post(tcp, http, Config),
  103. do_echo_post(tcp, http2, Config)
  104. after
  105. do_stop(echo_post)
  106. end.
  107. do_echo_post(Transport, Protocol, Config) ->
  108. ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
  109. Ref = gun:post(ConnPid, "/", [
  110. {<<"content-type">>, <<"application/octet-stream">>}
  111. ], <<"echo=this+is+fun">>),
  112. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  113. {ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
  114. ok.
  115. %% REST Hello World.
  116. rest_hello_world(Config) ->
  117. doc("REST Hello World example."),
  118. try
  119. do_compile_and_start(rest_hello_world),
  120. do_rest_hello_world(tcp, http, Config),
  121. do_rest_hello_world(tcp, http2, Config)
  122. after
  123. do_stop(rest_hello_world)
  124. end.
  125. do_rest_hello_world(Transport, Protocol, Config) ->
  126. << "<html>", _/bits >> = do_rest_hello_world_get(Transport, Protocol, undefined, Config),
  127. << "REST Hello World as text!" >> = do_rest_hello_world_get(Transport, Protocol, <<"text/plain">>, Config),
  128. << "{\"rest\": \"Hello World!\"}" >> = do_rest_hello_world_get(Transport, Protocol, <<"application/json">>, Config),
  129. not_acceptable = do_rest_hello_world_get(Transport, Protocol, <<"text/css">>, Config),
  130. ok.
  131. do_rest_hello_world_get(Transport, Protocol, Accept, Config) ->
  132. Port = case Transport of
  133. tcp -> 8080;
  134. ssl -> 8443
  135. end,
  136. ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
  137. Headers = case Accept of
  138. undefined -> [];
  139. _ -> [{<<"accept">>, Accept}]
  140. end,
  141. Ref = gun:get(ConnPid, "/", Headers),
  142. case gun:await(ConnPid, Ref) of
  143. {response, nofin, 200, _} ->
  144. {ok, Body} = gun:await_body(ConnPid, Ref),
  145. Body;
  146. {response, _, 406, _} ->
  147. not_acceptable
  148. end.