emqx_rewrite_api_SUITE.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. %%--------------------------------------------------------------------
  15. -module(emqx_rewrite_api_SUITE).
  16. -compile(nowarn_export_all).
  17. -compile(export_all).
  18. -import(emqx_mgmt_api_test_util, [request/3, uri/1]).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. -define(BASE_CONF, #{}).
  22. all() ->
  23. emqx_common_test_helpers:all(?MODULE).
  24. init_per_testcase(_, Config) ->
  25. {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000),
  26. Config.
  27. init_per_suite(Config) ->
  28. Apps = emqx_cth_suite:start(
  29. [
  30. emqx_conf,
  31. {emqx_modules, #{config => ?BASE_CONF}},
  32. emqx_management,
  33. emqx_mgmt_api_test_util:emqx_dashboard()
  34. ],
  35. #{work_dir => emqx_cth_suite:work_dir(Config)}
  36. ),
  37. [{apps, Apps} | Config].
  38. end_per_suite(Config) ->
  39. Apps = ?config(apps, Config),
  40. emqx_cth_suite:stop(Apps),
  41. ok.
  42. %%------------------------------------------------------------------------------
  43. %% Tests
  44. %%------------------------------------------------------------------------------
  45. t_mqtt_topic_rewrite(_) ->
  46. Rules = [
  47. #{
  48. <<"source_topic">> => <<"test/#">>,
  49. <<"re">> => <<"test/*">>,
  50. <<"dest_topic">> => <<"test1/$2">>,
  51. <<"action">> => <<"publish">>
  52. }
  53. ],
  54. ?assertMatch(
  55. {ok, 200, _},
  56. request(
  57. put,
  58. uri(["mqtt", "topic_rewrite"]),
  59. Rules
  60. )
  61. ),
  62. {ok, 200, Result} =
  63. request(get, uri(["mqtt", "topic_rewrite"])),
  64. ?assertEqual(
  65. Rules,
  66. emqx_utils_json:decode(Result, [return_maps])
  67. ).
  68. t_mqtt_topic_rewrite_limit(_) ->
  69. Rule =
  70. #{
  71. <<"source_topic">> => <<"test/#">>,
  72. <<"re">> => <<"test/*">>,
  73. <<"dest_topic">> => <<"test1/$2">>,
  74. <<"action">> => <<"publish">>
  75. },
  76. Rules = [Rule || _ <- lists:seq(1, 21)],
  77. ?assertMatch(
  78. {ok, 413, _},
  79. request(
  80. put,
  81. uri(["mqtt", "topic_rewrite"]),
  82. Rules
  83. )
  84. ).
  85. t_mqtt_topic_rewrite_wildcard(_) ->
  86. BadRules = [
  87. #{
  88. <<"source_topic">> => <<"test/#">>,
  89. <<"re">> => <<"^test/(.+)$">>,
  90. <<"dest_topic">> => <<"bad/test/#">>
  91. },
  92. #{
  93. <<"source_topic">> => <<"test/#">>,
  94. <<"re">> => <<"^test/(.+)$">>,
  95. <<"dest_topic">> => <<"bad/#/test">>
  96. },
  97. #{
  98. <<"source_topic">> => <<"test/#">>,
  99. <<"re">> => <<"^test/(.+)$">>,
  100. <<"dest_topic">> => <<"bad/test/+">>
  101. },
  102. #{
  103. <<"source_topic">> => <<"test/#">>,
  104. <<"re">> => <<"^test/(.+)$">>,
  105. <<"dest_topic">> => <<"bad/+/test">>
  106. }
  107. ],
  108. Rules = lists:flatten(
  109. lists:map(
  110. fun(Rule) ->
  111. [Rule#{<<"action">> => <<"publish">>}, Rule#{<<"action">> => <<"all">>}]
  112. end,
  113. BadRules
  114. )
  115. ),
  116. lists:foreach(
  117. fun(Rule) ->
  118. ?assertMatch(
  119. {ok, 400, _},
  120. request(
  121. put,
  122. uri(["mqtt", "topic_rewrite"]),
  123. [Rule]
  124. )
  125. )
  126. end,
  127. Rules
  128. ).
  129. %%------------------------------------------------------------------------------
  130. %% Helpers
  131. %%------------------------------------------------------------------------------
  132. request(Method, Url) ->
  133. request(Method, Url, []).