test: shared-sub topics/subscription api

This commit is contained in:
JimMoen 2023-10-30 14:41:57 +08:00
parent 814e22feb3
commit e9de7316b6
No known key found for this signature in database
GPG Key ID: 87A520B4F76BA86D
2 changed files with 82 additions and 0 deletions

View File

@ -155,6 +155,56 @@ t_list_with_internal_subscription(_Config) ->
),
ok.
t_list_with_shared_sub(_Config) ->
Client = proplists:get_value(client, _Config),
RealTopic = <<"t/+">>,
Topic = <<"$share/g1/", RealTopic/binary>>,
{ok, _, _} = emqtt:subscribe(Client, Topic),
{ok, _, _} = emqtt:subscribe(Client, RealTopic),
QS = [
{"clientid", ?CLIENTID},
{"match_topic", "t/#"}
],
Headers = emqx_mgmt_api_test_util:auth_header_(),
?assertMatch(
#{<<"data">> := [#{<<"clientid">> := ?CLIENTID}, #{<<"clientid">> := ?CLIENTID}]},
request_json(get, QS, Headers)
),
ok.
t_list_with_invalid_match_topic(_Config) ->
Client = proplists:get_value(client, _Config),
RealTopic = <<"t/+">>,
Topic = <<"$share/g1/", RealTopic/binary>>,
{ok, _, _} = emqtt:subscribe(Client, Topic),
{ok, _, _} = emqtt:subscribe(Client, RealTopic),
QS = [
{"clientid", ?CLIENTID},
{"match_topic", "$share/g1/t/1"}
],
Headers = emqx_mgmt_api_test_util:auth_header_(),
?assertMatch(
{error,
{{_, 400, _}, _, #{
<<"message">> := <<"match_topic_invalid">>,
<<"code">> := <<"INVALID_PARAMETER">>
}}},
begin
{error, {R, _H, Body}} = emqx_mgmt_api_test_util:request_api(
get, path(), uri_string:compose_query(QS), Headers, [], #{return_all => true}
),
{error, {R, _H, emqx_utils_json:decode(Body, [return_maps])}}
end
),
ok.
request_json(Method, Query, Headers) when is_list(Query) ->
Qs = uri_string:compose_query(Query),
{ok, MatchRes} = emqx_mgmt_api_test_util:request_api(Method, path(), Qs, Headers),

View File

@ -123,3 +123,35 @@ t_percent_topics(_Config) ->
),
ok = emqtt:stop(Client).
t_shared_topics(_Configs) ->
Node = atom_to_binary(node(), utf8),
RealTopic = <<"t/+">>,
Topic = <<"$share/g1/", RealTopic/binary>>,
{ok, Client} = emqtt:start_link(#{
username => <<"routes_username">>, clientid => <<"routes_cid">>
}),
{ok, _} = emqtt:connect(Client),
{ok, _, _} = emqtt:subscribe(Client, Topic),
{ok, _, _} = emqtt:subscribe(Client, RealTopic),
%% exact match with shared topic
Path = emqx_mgmt_api_test_util:api_path(["topics"]),
QS = uri_string:compose_query([
{"topic", Topic},
{"node", atom_to_list(node())}
]),
Headers = emqx_mgmt_api_test_util:auth_header_(),
{ok, MatchResponse1} = emqx_mgmt_api_test_util:request_api(get, Path, QS, Headers),
MatchData = emqx_utils_json:decode(MatchResponse1, [return_maps]),
?assertMatch(
#{<<"count">> := 1, <<"page">> := 1, <<"limit">> := 100},
maps:get(<<"meta">>, MatchData)
),
?assertMatch(
[#{<<"topic">> := Topic, <<"node">> := Node}],
maps:get(<<"data">>, MatchData)
),
ok = emqtt:stop(Client).