n2o_SUITE.erl 2.4 KB

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