fix(test): update the testcases for emqx_ws_connection_SUITE
This commit is contained in:
parent
7d9321a141
commit
25d7616881
|
@ -48,6 +48,7 @@ init_per_testcase(TestCase, Config) when
|
||||||
TestCase =/= t_ws_pingreq_before_connected,
|
TestCase =/= t_ws_pingreq_before_connected,
|
||||||
TestCase =/= t_ws_non_check_origin
|
TestCase =/= t_ws_non_check_origin
|
||||||
->
|
->
|
||||||
|
emqx_channel_SUITE:set_default_zone_conf(),
|
||||||
%% Mock cowboy_req
|
%% Mock cowboy_req
|
||||||
ok = meck:new(cowboy_req, [passthrough, no_history, no_link]),
|
ok = meck:new(cowboy_req, [passthrough, no_history, no_link]),
|
||||||
ok = meck:expect(cowboy_req, header, fun(_, _, _) -> <<>> end),
|
ok = meck:expect(cowboy_req, header, fun(_, _, _) -> <<>> end),
|
||||||
|
@ -78,6 +79,7 @@ init_per_testcase(TestCase, Config) when
|
||||||
Config;
|
Config;
|
||||||
|
|
||||||
init_per_testcase(_, Config) ->
|
init_per_testcase(_, Config) ->
|
||||||
|
ok = emqx_ct_helpers:start_apps([]),
|
||||||
Config.
|
Config.
|
||||||
|
|
||||||
end_per_testcase(TestCase, _Config) when
|
end_per_testcase(TestCase, _Config) when
|
||||||
|
@ -96,6 +98,7 @@ end_per_testcase(TestCase, _Config) when
|
||||||
]);
|
]);
|
||||||
|
|
||||||
end_per_testcase(_, Config) ->
|
end_per_testcase(_, Config) ->
|
||||||
|
emqx_ct_helpers:stop_apps([]),
|
||||||
Config.
|
Config.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
@ -110,7 +113,6 @@ t_info(_) ->
|
||||||
end),
|
end),
|
||||||
#{sockinfo := SockInfo} = ?ws_conn:call(WsPid, info),
|
#{sockinfo := SockInfo} = ?ws_conn:call(WsPid, info),
|
||||||
#{socktype := ws,
|
#{socktype := ws,
|
||||||
active_n := 100,
|
|
||||||
peername := {{127,0,0,1}, 3456},
|
peername := {{127,0,0,1}, 3456},
|
||||||
sockname := {{127,0,0,1}, 18083},
|
sockname := {{127,0,0,1}, 18083},
|
||||||
sockstate := running
|
sockstate := running
|
||||||
|
@ -171,12 +173,10 @@ t_call(_) ->
|
||||||
?assertEqual(Info, ?ws_conn:call(WsPid, info)).
|
?assertEqual(Info, ?ws_conn:call(WsPid, info)).
|
||||||
|
|
||||||
t_ws_pingreq_before_connected(_) ->
|
t_ws_pingreq_before_connected(_) ->
|
||||||
ok = emqx_ct_helpers:start_apps([]),
|
|
||||||
{ok, _} = application:ensure_all_started(gun),
|
{ok, _} = application:ensure_all_started(gun),
|
||||||
{ok, WPID} = gun:open("127.0.0.1", 8083),
|
{ok, WPID} = gun:open("127.0.0.1", 8083),
|
||||||
ws_pingreq(#{}),
|
ws_pingreq(#{}),
|
||||||
gun:close(WPID),
|
gun:close(WPID).
|
||||||
emqx_ct_helpers:stop_apps([]).
|
|
||||||
|
|
||||||
ws_pingreq(State) ->
|
ws_pingreq(State) ->
|
||||||
receive
|
receive
|
||||||
|
@ -205,14 +205,11 @@ ws_pingreq(State) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
t_ws_sub_protocols_mqtt(_) ->
|
t_ws_sub_protocols_mqtt(_) ->
|
||||||
ok = emqx_ct_helpers:start_apps([]),
|
|
||||||
{ok, _} = application:ensure_all_started(gun),
|
{ok, _} = application:ensure_all_started(gun),
|
||||||
?assertMatch({gun_upgrade, _},
|
?assertMatch({gun_upgrade, _},
|
||||||
start_ws_client(#{protocols => [<<"mqtt">>]})),
|
start_ws_client(#{protocols => [<<"mqtt">>]})).
|
||||||
emqx_ct_helpers:stop_apps([]).
|
|
||||||
|
|
||||||
t_ws_sub_protocols_mqtt_equivalents(_) ->
|
t_ws_sub_protocols_mqtt_equivalents(_) ->
|
||||||
ok = emqx_ct_helpers:start_apps([]),
|
|
||||||
{ok, _} = application:ensure_all_started(gun),
|
{ok, _} = application:ensure_all_started(gun),
|
||||||
%% also support mqtt-v3, mqtt-v3.1.1, mqtt-v5
|
%% also support mqtt-v3, mqtt-v3.1.1, mqtt-v5
|
||||||
?assertMatch({gun_upgrade, _},
|
?assertMatch({gun_upgrade, _},
|
||||||
|
@ -222,58 +219,39 @@ t_ws_sub_protocols_mqtt_equivalents(_) ->
|
||||||
?assertMatch({gun_upgrade, _},
|
?assertMatch({gun_upgrade, _},
|
||||||
start_ws_client(#{protocols => [<<"mqtt-v5">>]})),
|
start_ws_client(#{protocols => [<<"mqtt-v5">>]})),
|
||||||
?assertMatch({gun_response, {_, 400, _}},
|
?assertMatch({gun_response, {_, 400, _}},
|
||||||
start_ws_client(#{protocols => [<<"not-mqtt">>]})),
|
start_ws_client(#{protocols => [<<"not-mqtt">>]})).
|
||||||
emqx_ct_helpers:stop_apps([]).
|
|
||||||
|
|
||||||
t_ws_check_origin(_) ->
|
t_ws_check_origin(_) ->
|
||||||
emqx_ct_helpers:start_apps([],
|
emqx_config:put_listener_conf(default, mqtt_ws, [websocket, check_origin_enable], true),
|
||||||
fun(emqx) ->
|
emqx_config:put_listener_conf(default, mqtt_ws, [websocket, check_origins],
|
||||||
{ok, Listeners} = application:get_env(emqx, listeners),
|
[<<"http://localhost:18083">>]),
|
||||||
NListeners = lists:map(fun(#{listen_on := 8083, opts := Opts} = Listener) ->
|
|
||||||
NOpts = proplists:delete(check_origin_enable, Opts),
|
|
||||||
Listener#{opts => [{check_origin_enable, true} | NOpts]};
|
|
||||||
(Listener) ->
|
|
||||||
Listener
|
|
||||||
end, Listeners),
|
|
||||||
application:set_env(emqx, listeners, NListeners),
|
|
||||||
ok;
|
|
||||||
(_) -> ok
|
|
||||||
end),
|
|
||||||
{ok, _} = application:ensure_all_started(gun),
|
{ok, _} = application:ensure_all_started(gun),
|
||||||
?assertMatch({gun_upgrade, _},
|
?assertMatch({gun_upgrade, _},
|
||||||
start_ws_client(#{protocols => [<<"mqtt">>],
|
start_ws_client(#{protocols => [<<"mqtt">>],
|
||||||
headers => [{<<"origin">>, <<"http://localhost:18083">>}]})),
|
headers => [{<<"origin">>, <<"http://localhost:18083">>}]})),
|
||||||
?assertMatch({gun_response, {_, 500, _}},
|
?assertMatch({gun_response, {_, 500, _}},
|
||||||
start_ws_client(#{protocols => [<<"mqtt">>],
|
start_ws_client(#{protocols => [<<"mqtt">>],
|
||||||
headers => [{<<"origin">>, <<"http://localhost:18080">>}]})),
|
headers => [{<<"origin">>, <<"http://localhost:18080">>}]})).
|
||||||
emqx_ct_helpers:stop_apps([]).
|
|
||||||
|
|
||||||
t_ws_non_check_origin(_) ->
|
t_ws_non_check_origin(_) ->
|
||||||
emqx_ct_helpers:start_apps([]),
|
emqx_config:put_listener_conf(default, mqtt_ws, [websocket, check_origin_enable], false),
|
||||||
|
emqx_config:put_listener_conf(default, mqtt_ws, [websocket, check_origins], []),
|
||||||
{ok, _} = application:ensure_all_started(gun),
|
{ok, _} = application:ensure_all_started(gun),
|
||||||
?assertMatch({gun_upgrade, _},
|
?assertMatch({gun_upgrade, _},
|
||||||
start_ws_client(#{protocols => [<<"mqtt">>],
|
start_ws_client(#{protocols => [<<"mqtt">>],
|
||||||
headers => [{<<"origin">>, <<"http://localhost:18083">>}]})),
|
headers => [{<<"origin">>, <<"http://localhost:18083">>}]})),
|
||||||
?assertMatch({gun_upgrade, _},
|
?assertMatch({gun_upgrade, _},
|
||||||
start_ws_client(#{protocols => [<<"mqtt">>],
|
start_ws_client(#{protocols => [<<"mqtt">>],
|
||||||
headers => [{<<"origin">>, <<"http://localhost:18080">>}]})),
|
headers => [{<<"origin">>, <<"http://localhost:18080">>}]})).
|
||||||
emqx_ct_helpers:stop_apps([]).
|
|
||||||
|
|
||||||
|
|
||||||
t_init(_) ->
|
t_init(_) ->
|
||||||
Opts = [{idle_timeout, 300000},
|
Opts = #{listener => mqtt_ws, zone => default},
|
||||||
{fail_if_no_subprotocol, false},
|
|
||||||
{supported_subprotocols, ["mqtt"]}],
|
|
||||||
WsOpts = #{compress => false,
|
|
||||||
deflate_opts => #{},
|
|
||||||
max_frame_size => infinity,
|
|
||||||
idle_timeout => 300000
|
|
||||||
},
|
|
||||||
ok = meck:expect(cowboy_req, parse_header, fun(_, req) -> undefined end),
|
ok = meck:expect(cowboy_req, parse_header, fun(_, req) -> undefined end),
|
||||||
{cowboy_websocket, req, [req, Opts], WsOpts} = ?ws_conn:init(req, Opts),
|
ok = meck:expect(cowboy_req, reply, fun(_, Req) -> Req end),
|
||||||
|
{ok, req, _} = ?ws_conn:init(req, Opts),
|
||||||
ok = meck:expect(cowboy_req, parse_header, fun(_, req) -> [<<"mqtt">>] end),
|
ok = meck:expect(cowboy_req, parse_header, fun(_, req) -> [<<"mqtt">>] end),
|
||||||
ok = meck:expect(cowboy_req, set_resp_header, fun(_, <<"mqtt">>, req) -> resp end),
|
ok = meck:expect(cowboy_req, set_resp_header, fun(_, <<"mqtt">>, req) -> resp end),
|
||||||
{cowboy_websocket, resp, [req, Opts], WsOpts} = ?ws_conn:init(req, Opts).
|
{cowboy_websocket, resp, [req, Opts], _} = ?ws_conn:init(req, Opts).
|
||||||
|
|
||||||
t_websocket_handle_binary(_) ->
|
t_websocket_handle_binary(_) ->
|
||||||
{ok, _} = websocket_handle({binary, <<>>}, st()),
|
{ok, _} = websocket_handle({binary, <<>>}, st()),
|
||||||
|
|
Loading…
Reference in New Issue