emqx_rewrite_api_SUITE.erl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. ok = emqx_common_test_helpers:load_config(emqx_modules_schema, ?BASE_CONF),
  29. ok = emqx_mgmt_api_test_util:init_suite(
  30. [emqx_conf, emqx_modules]
  31. ),
  32. Config.
  33. end_per_suite(_Config) ->
  34. emqx_mgmt_api_test_util:end_suite([emqx_conf, emqx_modules]),
  35. ok.
  36. %%------------------------------------------------------------------------------
  37. %% Tests
  38. %%------------------------------------------------------------------------------
  39. t_mqtt_topic_rewrite(_) ->
  40. Rules = [
  41. #{
  42. <<"source_topic">> => <<"test/#">>,
  43. <<"re">> => <<"test/*">>,
  44. <<"dest_topic">> => <<"test1/$2">>,
  45. <<"action">> => <<"publish">>
  46. }
  47. ],
  48. ?assertMatch(
  49. {ok, 200, _},
  50. request(
  51. put,
  52. uri(["mqtt", "topic_rewrite"]),
  53. Rules
  54. )
  55. ),
  56. {ok, 200, Result} =
  57. request(get, uri(["mqtt", "topic_rewrite"])),
  58. ?assertEqual(
  59. Rules,
  60. emqx_utils_json:decode(Result, [return_maps])
  61. ).
  62. t_mqtt_topic_rewrite_limit(_) ->
  63. Rule =
  64. #{
  65. <<"source_topic">> => <<"test/#">>,
  66. <<"re">> => <<"test/*">>,
  67. <<"dest_topic">> => <<"test1/$2">>,
  68. <<"action">> => <<"publish">>
  69. },
  70. Rules = [Rule || _ <- lists:seq(1, 21)],
  71. ?assertMatch(
  72. {ok, 413, _},
  73. request(
  74. put,
  75. uri(["mqtt", "topic_rewrite"]),
  76. Rules
  77. )
  78. ).
  79. t_mqtt_topic_rewrite_wildcard(_) ->
  80. BadRules = [
  81. #{
  82. <<"source_topic">> => <<"test/#">>,
  83. <<"re">> => <<"^test/(.+)$">>,
  84. <<"dest_topic">> => <<"bad/test/#">>
  85. },
  86. #{
  87. <<"source_topic">> => <<"test/#">>,
  88. <<"re">> => <<"^test/(.+)$">>,
  89. <<"dest_topic">> => <<"bad/#/test">>
  90. },
  91. #{
  92. <<"source_topic">> => <<"test/#">>,
  93. <<"re">> => <<"^test/(.+)$">>,
  94. <<"dest_topic">> => <<"bad/test/+">>
  95. },
  96. #{
  97. <<"source_topic">> => <<"test/#">>,
  98. <<"re">> => <<"^test/(.+)$">>,
  99. <<"dest_topic">> => <<"bad/+/test">>
  100. }
  101. ],
  102. Rules = lists:flatten(
  103. lists:map(
  104. fun(Rule) ->
  105. [Rule#{<<"action">> => <<"publish">>}, Rule#{<<"action">> => <<"all">>}]
  106. end,
  107. BadRules
  108. )
  109. ),
  110. lists:foreach(
  111. fun(Rule) ->
  112. ?assertMatch(
  113. {ok, 400, _},
  114. request(
  115. put,
  116. uri(["mqtt", "topic_rewrite"]),
  117. [Rule]
  118. )
  119. )
  120. end,
  121. Rules
  122. ).
  123. %%------------------------------------------------------------------------------
  124. %% Helpers
  125. %%------------------------------------------------------------------------------
  126. request(Method, Url) ->
  127. request(Method, Url, []).