emqx_plugin_libs_rule_SUITE.erl 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. gen_tcp:close(Socket),
  27. {error, _} = emqx_plugin_libs_rule:http_connectivity(
  28. "http://127.0.0.1:"++emqx_plugin_libs_rule:str(?PORT), 1000).
  29. t_tcp_connectivity(_) ->
  30. {ok, Socket} = gen_tcp:listen(?PORT, []),
  31. ok = emqx_plugin_libs_rule:tcp_connectivity("127.0.0.1", ?PORT, 1000),
  32. gen_tcp:close(Socket),
  33. {error, _} = emqx_plugin_libs_rule:tcp_connectivity("127.0.0.1", ?PORT, 1000).
  34. t_str(_) ->
  35. ?assertEqual("abc", emqx_plugin_libs_rule:str("abc")),
  36. ?assertEqual("abc", emqx_plugin_libs_rule:str(abc)),
  37. ?assertEqual("{\"a\":1}", emqx_plugin_libs_rule:str(#{a => 1})),
  38. ?assertEqual("1", emqx_plugin_libs_rule:str(1)),
  39. ?assertEqual("2.0", emqx_plugin_libs_rule:str(2.0)),
  40. ?assertEqual("true", emqx_plugin_libs_rule:str(true)),
  41. ?assertError(_, emqx_plugin_libs_rule:str({a, v})).
  42. t_bin(_) ->
  43. ?assertEqual(<<"abc">>, emqx_plugin_libs_rule:bin("abc")),
  44. ?assertEqual(<<"abc">>, emqx_plugin_libs_rule:bin(abc)),
  45. ?assertEqual(<<"{\"a\":1}">>, emqx_plugin_libs_rule:bin(#{a => 1})),
  46. ?assertEqual(<<"[{\"a\":1}]">>, emqx_plugin_libs_rule:bin([#{a => 1}])),
  47. ?assertEqual(<<"1">>, emqx_plugin_libs_rule:bin(1)),
  48. ?assertEqual(<<"2.0">>, emqx_plugin_libs_rule:bin(2.0)),
  49. ?assertEqual(<<"true">>, emqx_plugin_libs_rule:bin(true)),
  50. ?assertError(_, emqx_plugin_libs_rule:bin({a, v})).
  51. t_atom_key(_) ->
  52. _ = erlang, _ = port,
  53. ?assertEqual([erlang], emqx_plugin_libs_rule:atom_key([<<"erlang">>])),
  54. ?assertEqual([erlang, port], emqx_plugin_libs_rule:atom_key([<<"erlang">>, port])),
  55. ?assertEqual([erlang, port], emqx_plugin_libs_rule:atom_key([<<"erlang">>, <<"port">>])),
  56. ?assertEqual(erlang, emqx_plugin_libs_rule:atom_key(<<"erlang">>)),
  57. ?assertError({invalid_key, {a, v}}, emqx_plugin_libs_rule:atom_key({a, v})),
  58. _ = xyz876gv123,
  59. ?assertEqual([xyz876gv123, port], emqx_plugin_libs_rule:atom_key([<<"xyz876gv123">>, port])).
  60. t_unsafe_atom_key(_) ->
  61. ?assertEqual([xyz876gv], emqx_plugin_libs_rule:unsafe_atom_key([<<"xyz876gv">>])),
  62. ?assertEqual([xyz876gv33, port],
  63. emqx_plugin_libs_rule:unsafe_atom_key([<<"xyz876gv33">>, port])),
  64. ?assertEqual([xyz876gv331, port1221],
  65. emqx_plugin_libs_rule:unsafe_atom_key([<<"xyz876gv331">>, <<"port1221">>])),
  66. ?assertEqual(xyz876gv3312, emqx_plugin_libs_rule:unsafe_atom_key(<<"xyz876gv3312">>)).