emqx_common_test_http.erl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-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_common_test_http).
  17. -include_lib("common_test/include/ct.hrl").
  18. -export([ request_api/3
  19. , request_api/4
  20. , request_api/5
  21. , get_http_data/1
  22. , create_default_app/0
  23. , delete_default_app/0
  24. , default_auth_header/0
  25. , auth_header/2
  26. ]).
  27. request_api(Method, Url, Auth) ->
  28. request_api(Method, Url, [], Auth, []).
  29. request_api(Method, Url, QueryParams, Auth) ->
  30. request_api(Method, Url, QueryParams, Auth, []).
  31. request_api(Method, Url, QueryParams, Auth, Body) ->
  32. request_api(Method, Url, QueryParams, Auth, Body, []).
  33. request_api(Method, Url, QueryParams, Auth, Body, HttpOpts) ->
  34. NewUrl = case QueryParams of
  35. [] ->
  36. Url;
  37. _ ->
  38. Url ++ "?" ++ QueryParams
  39. end,
  40. Request = case Body of
  41. [] ->
  42. {NewUrl, [Auth]};
  43. _ ->
  44. {NewUrl, [Auth], "application/json", emqx_json:encode(Body)}
  45. end,
  46. do_request_api(Method, Request, HttpOpts).
  47. do_request_api(Method, Request, HttpOpts) ->
  48. ct:pal("Method: ~p, Request: ~p", [Method, Request]),
  49. case httpc:request(Method, Request, HttpOpts, [{body_format, binary}]) of
  50. {error, socket_closed_remotely} ->
  51. {error, socket_closed_remotely};
  52. {ok, {{"HTTP/1.1", Code, _}, _Headers, Return} } ->
  53. {ok, Code, Return};
  54. {ok, {Reason, _, _}} ->
  55. {error, Reason}
  56. end.
  57. get_http_data(ResponseBody) ->
  58. emqx_json:decode(ResponseBody, [return_maps]).
  59. auth_header(User, Pass) ->
  60. Encoded = base64:encode_to_string(lists:append([User,":",Pass])),
  61. {"Authorization","Basic " ++ Encoded}.
  62. default_auth_header() ->
  63. AppId = <<"myappid">>,
  64. AppSecret = emqx_mgmt_auth:get_appsecret(AppId),
  65. auth_header(erlang:binary_to_list(AppId), erlang:binary_to_list(AppSecret)).
  66. create_default_app() ->
  67. emqx_mgmt_auth:add_app(<<"myappid">>, <<"test">>).
  68. delete_default_app() ->
  69. emqx_mgmt_auth:del_app(<<"myappid">>).