n4u_SUITE.erl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. -module(n4u_SUITE).
  2. -behaviour(cowboy_http_handler).
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("n4u/include/n4u.hrl").
  5. -compile(export_all).
  6. suite() -> [{timetrap,{seconds,30}}].
  7. all() -> [{group, elements}].
  8. groups() -> [{elements, [], [elements]}].
  9. init_per_suite(Config) ->
  10. ok = application:start(crypto),
  11. ok = application:start(ranch),
  12. ok = application:start(cowboy),
  13. Config.
  14. end_per_suite(_Config) ->
  15. application:stop(cowboy),
  16. application:stop(ranch),
  17. application:stop(crypto),
  18. ok.
  19. init_per_group(elements, Config) ->
  20. Port = 33084,
  21. Transport = ranch_tcp,
  22. {ok, _} = cowboy:start_http(elements, 100, [{port, Port}], [
  23. {env, [{dispatch, init_dispatch()}]},
  24. {max_keepalive, 50},
  25. {timeout, 500} ]),
  26. {ok, Client} = cowboy_client:init([]),
  27. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  28. {transport, Transport}, {client, Client} | Config].
  29. end_per_group(Name, _) ->
  30. cowboy:stop_listener(Name),
  31. ok.
  32. init_dispatch() ->
  33. cowboy_router:compile([{"localhost", [{'_', n4u_SUITE, []}]}]).
  34. build_url(Path, Config) ->
  35. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  36. {port, Port} = lists:keyfind(port, 1, Config),
  37. PortBin = list_to_binary(integer_to_list(Port)),
  38. PathBin = list_to_binary(Path),
  39. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  40. elements(Config) ->
  41. Client = ?config(client, Config),
  42. URL = build_url("/elements", Config),
  43. ct:log("-> url ~p", [URL]),
  44. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Client),
  45. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  46. {ok, Body, _} = cowboy_client:response_body(Client3),
  47. ct:log("-> response Body: ~p", [Body]),
  48. {_, 10} = binary:match(Body, <<"test label">>),
  49. {_, 12} = binary:match(Body, <<"test textbox">>),
  50. ok.
  51. %% handle to process http request
  52. -record(state, {headers, body}).
  53. init({_Transport, http}, Req, _Opts) ->
  54. {ok, Req, #state{}}.
  55. handle(Req, State) ->
  56. RequestBridge = simple_bridge:make_request(cowboy_request_bridge, Req),
  57. ResponseBridge = simple_bridge:make_response(cowboy_response_bridge, RequestBridge),
  58. wf_context:init_context(RequestBridge, ResponseBridge),
  59. %% wf_handler:set_handler(http_basic_auth_security_handler, n2o_auth),
  60. {ok, NewReq} = wf_core:run(),
  61. {ok, NewReq, State}.
  62. terminate(_Reason, _Req, _State) ->
  63. ok.