emqx_common_test_http.erl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2023 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_SECRET, <<"default_app_secret">>).
  31. request_api(Method, Url, Auth) ->
  32. request_api(Method, Url, [], Auth, []).
  33. request_api(Method, Url, QueryParams, Auth) ->
  34. request_api(Method, Url, QueryParams, Auth, []).
  35. request_api(Method, Url, QueryParams, Auth, Body) ->
  36. request_api(Method, Url, QueryParams, Auth, Body, []).
  37. request_api(Method, Url, QueryParams, Auth, Body, HttpOpts) ->
  38. NewUrl =
  39. case QueryParams of
  40. [] ->
  41. Url;
  42. _ ->
  43. Url ++ "?" ++ QueryParams
  44. end,
  45. Request =
  46. case Body of
  47. [] ->
  48. {NewUrl, [Auth]};
  49. _ ->
  50. {NewUrl, [Auth], "application/json", emqx_utils_json:encode(Body)}
  51. end,
  52. do_request_api(Method, Request, HttpOpts).
  53. do_request_api(Method, Request, HttpOpts) ->
  54. ct:pal("Method: ~p, Request: ~p", [Method, Request]),
  55. case httpc:request(Method, Request, HttpOpts, [{body_format, binary}]) of
  56. {error, socket_closed_remotely} ->
  57. {error, socket_closed_remotely};
  58. {ok, {{"HTTP/1.1", Code, _}, _Headers, Return}} ->
  59. {ok, Code, Return};
  60. {ok, {Reason, _, _}} ->
  61. {error, Reason}
  62. end.
  63. get_http_data(ResponseBody) ->
  64. emqx_utils_json:decode(ResponseBody, [return_maps]).
  65. auth_header(#{api_key := ApiKey, api_secret := Secret}) ->
  66. auth_header(binary_to_list(ApiKey), binary_to_list(Secret)).
  67. auth_header(User, Pass) ->
  68. Encoded = base64:encode_to_string(lists:append([User, ":", Pass])),
  69. {"Authorization", "Basic " ++ Encoded}.
  70. default_auth_header() ->
  71. {ok, #{api_key := APIKey}} = emqx_mgmt_auth:read(?DEFAULT_APP_ID),
  72. auth_header(
  73. erlang:binary_to_list(APIKey), erlang:binary_to_list(?DEFAULT_APP_SECRET)
  74. ).
  75. create_default_app() ->
  76. Now = erlang:system_time(second),
  77. ExpiredAt = Now + timer:minutes(10),
  78. emqx_mgmt_auth:create(
  79. ?DEFAULT_APP_ID, ?DEFAULT_APP_SECRET, true, ExpiredAt, <<"default app key for test">>
  80. ).
  81. delete_default_app() ->
  82. emqx_mgmt_auth:delete(?DEFAULT_APP_ID).