123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- -module(n4u_SUITE).
- -behaviour(cowboy_http_handler).
- -include_lib("common_test/include/ct.hrl").
- -include_lib("n4u/include/n4u.hrl").
- -compile(export_all).
- suite() -> [{timetrap,{seconds,30}}].
- all() -> [{group, elements}].
- groups() -> [{elements, [], [elements]}].
- init_per_suite(Config) ->
- ok = application:start(crypto),
- ok = application:start(ranch),
- ok = application:start(cowboy),
- Config.
- end_per_suite(_Config) ->
- application:stop(cowboy),
- application:stop(ranch),
- application:stop(crypto),
- ok.
- init_per_group(elements, Config) ->
- Port = 33084,
- Transport = ranch_tcp,
- {ok, _} = cowboy:start_http(elements, 100, [{port, Port}], [
- {env, [{dispatch, init_dispatch()}]},
- {max_keepalive, 50},
- {timeout, 500} ]),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client} | Config].
- end_per_group(Name, _) ->
- cowboy:stop_listener(Name),
- ok.
- init_dispatch() ->
- cowboy_router:compile([{"localhost", [{'_', n4u_SUITE, []}]}]).
- build_url(Path, Config) ->
- {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
- {port, Port} = lists:keyfind(port, 1, Config),
- PortBin = list_to_binary(integer_to_list(Port)),
- PathBin = list_to_binary(Path),
- << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
- elements(Config) ->
- Client = ?config(client, Config),
- URL = build_url("/elements", Config),
- ct:log("-> url ~p", [URL]),
- {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3),
- ct:log("-> response Body: ~p", [Body]),
- {_, 10} = binary:match(Body, <<"test label">>),
- {_, 12} = binary:match(Body, <<"test textbox">>),
- ok.
- %% handle to process http request
- -record(state, {headers, body}).
- init({_Transport, http}, Req, _Opts) ->
- {ok, Req, #state{}}.
- handle(Req, State) ->
- RequestBridge = simple_bridge:make_request(cowboy_request_bridge, Req),
- ResponseBridge = simple_bridge:make_response(cowboy_response_bridge, RequestBridge),
- wf_context:init_context(RequestBridge, ResponseBridge),
- %% wf_handler:set_handler(http_basic_auth_security_handler, n2o_auth),
- {ok, NewReq} = wf_core:run(),
- {ok, NewReq, State}.
- terminate(_Reason, _Req, _State) ->
- ok.
|