emqx_common_test_http.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-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. %%
  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_common_test_http).
  17. -include_lib("common_test/include/ct.hrl").
  18. -export([
  19. request_api/3,
  20. request_api/4,
  21. request_api/5,
  22. get_http_data/1,
  23. create_default_app/0,
  24. delete_default_app/0,
  25. default_auth_header/0,
  26. auth_header/1,
  27. auth_header/2
  28. ]).
  29. -define(DEFAULT_APP_ID, <<"default_appid">>).
  30. -define(DEFAULT_APP_KEY, <<"default_app_key">>).
  31. -define(DEFAULT_APP_SECRET, <<"default_app_secret">>).
  32. %% from emqx_dashboard/include/emqx_dashboard_rbac.hrl
  33. -define(ROLE_API_SUPERUSER, <<"administrator">>).
  34. request_api(Method, Url, Auth) ->
  35. request_api(Method, Url, [], Auth, []).
  36. request_api(Method, Url, QueryParams, Auth) ->
  37. request_api(Method, Url, QueryParams, Auth, []).
  38. request_api(Method, Url, QueryParams, Auth, Body) ->
  39. request_api(Method, Url, QueryParams, Auth, Body, []).
  40. request_api(Method, Url, QueryParams, Auth, Body, HttpOpts) ->
  41. NewUrl =
  42. case QueryParams of
  43. [] ->
  44. Url;
  45. _ ->
  46. Url ++ "?" ++ QueryParams
  47. end,
  48. Request =
  49. case Body of
  50. [] ->
  51. {NewUrl, [Auth]};
  52. _ ->
  53. {NewUrl, [Auth], "application/json", emqx_utils_json:encode(Body)}
  54. end,
  55. do_request_api(Method, Request, HttpOpts).
  56. do_request_api(Method, Request, HttpOpts) ->
  57. % ct:pal("Method: ~p, Request: ~p", [Method, Request]),
  58. case httpc:request(Method, Request, HttpOpts, [{body_format, binary}]) of
  59. {error, socket_closed_remotely} ->
  60. {error, socket_closed_remotely};
  61. {ok, {{"HTTP/1.1", Code, _}, _Headers, Return}} ->
  62. {ok, Code, Return};
  63. {ok, {Reason, _, _}} ->
  64. {error, Reason}
  65. end.
  66. get_http_data(ResponseBody) ->
  67. emqx_utils_json:decode(ResponseBody, [return_maps]).
  68. auth_header(#{api_key := ApiKey, api_secret := Secret}) ->
  69. auth_header(binary_to_list(ApiKey), binary_to_list(Secret)).
  70. auth_header(User, Pass) ->
  71. Encoded = base64:encode_to_string(lists:append([User, ":", Pass])),
  72. {"Authorization", "Basic " ++ Encoded}.
  73. default_auth_header() ->
  74. {ok, #{api_key := APIKey}} = emqx_mgmt_auth:read(?DEFAULT_APP_ID),
  75. auth_header(
  76. erlang:binary_to_list(APIKey), erlang:binary_to_list(?DEFAULT_APP_SECRET)
  77. ).
  78. create_default_app() ->
  79. Now = erlang:system_time(second),
  80. ExpiredAt = Now + timer:minutes(10),
  81. case
  82. emqx_mgmt_auth:create(
  83. ?DEFAULT_APP_ID,
  84. ?DEFAULT_APP_KEY,
  85. ?DEFAULT_APP_SECRET,
  86. true,
  87. ExpiredAt,
  88. <<"default app key for test">>,
  89. ?ROLE_API_SUPERUSER
  90. )
  91. of
  92. {ok, App} ->
  93. {ok, App};
  94. {error, name_already_existed} ->
  95. {ok, _} = emqx_mgmt_auth:read(?DEFAULT_APP_ID)
  96. end.
  97. delete_default_app() ->
  98. emqx_mgmt_auth:delete(?DEFAULT_APP_ID).