emqx_plugin_libs_rule_SUITE.erl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_plugin_libs_rule_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -define(PORT, 9876).
  21. all() -> emqx_common_test_helpers:all(?MODULE).
  22. t_http_connectivity(_) ->
  23. {ok, Socket} = gen_tcp:listen(?PORT, []),
  24. ok = emqx_plugin_libs_rule:http_connectivity(
  25. "http://127.0.0.1:" ++ emqx_plugin_libs_rule:str(?PORT), 1000
  26. ),
  27. gen_tcp:close(Socket),
  28. {error, _} = emqx_plugin_libs_rule:http_connectivity(
  29. "http://127.0.0.1:" ++ emqx_plugin_libs_rule:str(?PORT), 1000
  30. ).
  31. t_tcp_connectivity(_) ->
  32. {ok, Socket} = gen_tcp:listen(?PORT, []),
  33. ok = emqx_plugin_libs_rule:tcp_connectivity("127.0.0.1", ?PORT, 1000),
  34. gen_tcp:close(Socket),
  35. {error, _} = emqx_plugin_libs_rule:tcp_connectivity("127.0.0.1", ?PORT, 1000).
  36. t_str(_) ->
  37. ?assertEqual("abc", emqx_plugin_libs_rule:str("abc")),
  38. ?assertEqual("abc", emqx_plugin_libs_rule:str(abc)),
  39. ?assertEqual("{\"a\":1}", emqx_plugin_libs_rule:str(#{a => 1})),
  40. ?assertEqual("1", emqx_plugin_libs_rule:str(1)),
  41. ?assertEqual("2.0", emqx_plugin_libs_rule:str(2.0)),
  42. ?assertEqual("true", emqx_plugin_libs_rule:str(true)),
  43. ?assertError(_, emqx_plugin_libs_rule:str({a, v})).
  44. t_bin(_) ->
  45. ?assertEqual(<<"abc">>, emqx_plugin_libs_rule:bin("abc")),
  46. ?assertEqual(<<"abc">>, emqx_plugin_libs_rule:bin(abc)),
  47. ?assertEqual(<<"{\"a\":1}">>, emqx_plugin_libs_rule:bin(#{a => 1})),
  48. ?assertEqual(<<"[{\"a\":1}]">>, emqx_plugin_libs_rule:bin([#{a => 1}])),
  49. ?assertEqual(<<"1">>, emqx_plugin_libs_rule:bin(1)),
  50. ?assertEqual(<<"2.0">>, emqx_plugin_libs_rule:bin(2.0)),
  51. ?assertEqual(<<"true">>, emqx_plugin_libs_rule:bin(true)),
  52. ?assertError(_, emqx_plugin_libs_rule:bin({a, v})).
  53. t_atom_key(_) ->
  54. _ = erlang,
  55. _ = port,
  56. ?assertEqual([erlang], emqx_plugin_libs_rule:atom_key([<<"erlang">>])),
  57. ?assertEqual([erlang, port], emqx_plugin_libs_rule:atom_key([<<"erlang">>, port])),
  58. ?assertEqual([erlang, port], emqx_plugin_libs_rule:atom_key([<<"erlang">>, <<"port">>])),
  59. ?assertEqual(erlang, emqx_plugin_libs_rule:atom_key(<<"erlang">>)),
  60. ?assertError({invalid_key, {a, v}}, emqx_plugin_libs_rule:atom_key({a, v})),
  61. _ = xyz876gv123,
  62. ?assertEqual([xyz876gv123, port], emqx_plugin_libs_rule:atom_key([<<"xyz876gv123">>, port])).
  63. t_unsafe_atom_key(_) ->
  64. ?assertEqual([xyz876gv], emqx_plugin_libs_rule:unsafe_atom_key([<<"xyz876gv">>])),
  65. ?assertEqual(
  66. [xyz876gv33, port],
  67. emqx_plugin_libs_rule:unsafe_atom_key([<<"xyz876gv33">>, port])
  68. ),
  69. ?assertEqual(
  70. [xyz876gv331, port1221],
  71. emqx_plugin_libs_rule:unsafe_atom_key([<<"xyz876gv331">>, <<"port1221">>])
  72. ),
  73. ?assertEqual(xyz876gv3312, emqx_plugin_libs_rule:unsafe_atom_key(<<"xyz876gv3312">>)).