examples_SUITE.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 and POST.
  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(Config) ->
  98. doc("POST parameter echo example."),
  99. try
  100. do_compile_and_start(echo_post),
  101. do_echo_post(tcp, http, Config),
  102. do_echo_post(tcp, http2, Config)
  103. after
  104. do_stop(echo_post)
  105. end.
  106. do_echo_post(Transport, Protocol, Config) ->
  107. ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
  108. Ref = gun:post(ConnPid, "/", [
  109. {<<"content-type">>, <<"application/octet-stream">>}
  110. ], <<"echo=this+is+fun">>),
  111. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  112. {ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
  113. ok.