emqx_mgmt_api_test_util.erl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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_mgmt_api_test_util).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -define(SERVER, "http://127.0.0.1:18083").
  20. -define(BASE_PATH, "/api/v5").
  21. init_suite() ->
  22. init_suite([]).
  23. init_suite(Apps) ->
  24. init_suite(Apps, fun set_special_configs/1, #{}).
  25. init_suite(Apps, SetConfigs) when is_function(SetConfigs) ->
  26. init_suite(Apps, SetConfigs, #{}).
  27. init_suite(Apps, SetConfigs, Opts) ->
  28. emqx_common_test_helpers:start_apps(
  29. Apps ++ [emqx_management, emqx_dashboard], SetConfigs, Opts
  30. ),
  31. _ = emqx_common_test_http:create_default_app(),
  32. ok.
  33. end_suite() ->
  34. end_suite([]).
  35. end_suite(Apps) ->
  36. emqx_common_test_http:delete_default_app(),
  37. emqx_common_test_helpers:stop_apps(Apps ++ [emqx_management, emqx_dashboard]),
  38. ok.
  39. set_special_configs(emqx_dashboard) ->
  40. emqx_dashboard_api_test_helpers:set_default_config(),
  41. ok;
  42. set_special_configs(_App) ->
  43. ok.
  44. -spec emqx_dashboard() -> emqx_cth_suite:appspec().
  45. emqx_dashboard() ->
  46. emqx_dashboard("dashboard.listeners.http { enable = true, bind = 18083 }").
  47. emqx_dashboard(Config) ->
  48. {emqx_dashboard, #{
  49. config => Config,
  50. before_start => fun() ->
  51. {ok, _} = emqx_common_test_http:create_default_app()
  52. end,
  53. after_start => fun() ->
  54. true = emqx_dashboard_listener:is_ready(infinity)
  55. end
  56. }}.
  57. %% there is no difference between the 'request' and 'request_api'
  58. %% the 'request' is only to be compatible with the 'emqx_dashboard_api_test_helpers:request'
  59. request(Method, Url) ->
  60. request(Method, Url, []).
  61. request(Method, Url, Body) ->
  62. request_api_with_body(Method, Url, Body).
  63. uri(Parts) ->
  64. emqx_dashboard_api_test_helpers:uri(Parts).
  65. uri(Host, Parts) ->
  66. emqx_dashboard_api_test_helpers:uri(Host, Parts).
  67. %% compatible_mode will return as same as 'emqx_dashboard_api_test_helpers:request'
  68. request_api_with_body(Method, Url, Body) ->
  69. Opts = #{compatible_mode => true, httpc_req_opts => [{body_format, binary}]},
  70. request_api(Method, Url, [], auth_header_(), Body, Opts).
  71. request_api(Method, Url) ->
  72. request_api(Method, Url, auth_header_()).
  73. request_api(Method, Url, AuthOrHeaders) ->
  74. request_api(Method, Url, [], AuthOrHeaders, [], #{}).
  75. request_api(Method, Url, QueryParams, AuthOrHeaders) ->
  76. request_api(Method, Url, QueryParams, AuthOrHeaders, [], #{}).
  77. request_api(Method, Url, QueryParams, AuthOrHeaders, Body) ->
  78. request_api(Method, Url, QueryParams, AuthOrHeaders, Body, #{}).
  79. request_api(Method, Url, QueryParams, [], Body, Opts) ->
  80. request_api(Method, Url, QueryParams, auth_header_(), Body, Opts);
  81. request_api(Method, Url, QueryParams, AuthOrHeaders, [], Opts) when
  82. (Method =:= options) orelse
  83. (Method =:= get) orelse
  84. (Method =:= put) orelse
  85. (Method =:= head) orelse
  86. (Method =:= delete) orelse
  87. (Method =:= trace)
  88. ->
  89. NewUrl =
  90. case QueryParams of
  91. "" -> Url;
  92. _ -> Url ++ "?" ++ QueryParams
  93. end,
  94. do_request_api(Method, {NewUrl, build_http_header(AuthOrHeaders)}, Opts);
  95. request_api(Method, Url, QueryParams, AuthOrHeaders, Body, Opts) when
  96. (Method =:= post) orelse
  97. (Method =:= patch) orelse
  98. (Method =:= put) orelse
  99. (Method =:= delete)
  100. ->
  101. NewUrl =
  102. case QueryParams of
  103. "" -> Url;
  104. _ -> Url ++ "?" ++ QueryParams
  105. end,
  106. do_request_api(
  107. Method,
  108. {NewUrl, build_http_header(AuthOrHeaders), "application/json",
  109. emqx_utils_json:encode(Body)},
  110. Opts
  111. ).
  112. do_request_api(Method, Request, Opts) ->
  113. ReturnAll = maps:get(return_all, Opts, false),
  114. CompatibleMode = maps:get(compatible_mode, Opts, false),
  115. HttpcReqOpts = maps:get(httpc_req_opts, Opts, []),
  116. ct:pal("~p: ~p~nOpts: ~p", [Method, Request, Opts]),
  117. case httpc:request(Method, Request, [], HttpcReqOpts) of
  118. {error, socket_closed_remotely} ->
  119. {error, socket_closed_remotely};
  120. {ok, {{_, Code, _}, _Headers, Body}} when CompatibleMode ->
  121. {ok, Code, Body};
  122. {ok, {{"HTTP/1.1", Code, _} = Reason, Headers, Body}} when
  123. Code >= 200 andalso Code =< 299 andalso ReturnAll
  124. ->
  125. {ok, {Reason, Headers, Body}};
  126. {ok, {{"HTTP/1.1", Code, _}, _, Body}} when
  127. Code >= 200 andalso Code =< 299
  128. ->
  129. {ok, Body};
  130. {ok, {Reason, Headers, Body}} when ReturnAll ->
  131. {error, {Reason, Headers, Body}};
  132. {ok, {Reason, _Headers, _Body}} ->
  133. {error, Reason}
  134. end.
  135. auth_header_() ->
  136. emqx_common_test_http:default_auth_header().
  137. build_http_header(X) when is_list(X) ->
  138. X;
  139. build_http_header(X) ->
  140. [X].
  141. default_server() ->
  142. ?SERVER.
  143. api_path(Parts) ->
  144. join_http_path([?SERVER, ?BASE_PATH | Parts]).
  145. api_path(Host, Parts) ->
  146. join_http_path([Host, ?BASE_PATH | Parts]).
  147. api_path_without_base_path(Parts) ->
  148. join_http_path([?SERVER | Parts]).
  149. join_http_path([]) ->
  150. [];
  151. join_http_path([Part | Rest]) ->
  152. lists:foldl(fun(P, Acc) -> emqx_bridge_http_connector:join_paths(Acc, P) end, Part, Rest).
  153. %% Usage:
  154. %% upload_request(<<"site.com/api/upload">>, <<"path/to/file.png">>,
  155. %% <<"upload">>, <<"image/png">>, [], <<"some-token">>)
  156. %%
  157. %% Usage with RequestData:
  158. %% Payload = [{upload_type, <<"user_picture">>}],
  159. %% PayloadContent = emqx_utils_json:encode(Payload),
  160. %% RequestData = [
  161. %% {<<"payload">>, PayloadContent}
  162. %% ]
  163. %% upload_request(<<"site.com/api/upload">>, <<"path/to/file.png">>,
  164. %% <<"upload">>, <<"image/png">>, RequestData, <<"some-token">>)
  165. -spec upload_request(URL, FilePath, Name, MimeType, RequestData, AuthorizationToken) ->
  166. {ok, binary()} | {error, list()}
  167. when
  168. URL :: binary(),
  169. FilePath :: binary(),
  170. Name :: binary(),
  171. MimeType :: binary(),
  172. RequestData :: list(),
  173. AuthorizationToken :: binary().
  174. upload_request(URL, FilePath, Name, MimeType, RequestData, AuthorizationToken) ->
  175. Method = post,
  176. Filename = filename:basename(FilePath),
  177. {ok, Data} = file:read_file(FilePath),
  178. Boundary = emqx_guid:to_base62(emqx_guid:gen()),
  179. RequestBody = format_multipart_formdata(
  180. Data,
  181. RequestData,
  182. Name,
  183. [Filename],
  184. MimeType,
  185. Boundary
  186. ),
  187. ContentType = "multipart/form-data; boundary=" ++ binary_to_list(Boundary),
  188. ContentLength = integer_to_list(length(binary_to_list(RequestBody))),
  189. Headers = [
  190. {"Content-Length", ContentLength},
  191. case AuthorizationToken of
  192. _ when is_tuple(AuthorizationToken) ->
  193. AuthorizationToken;
  194. _ when is_binary(AuthorizationToken) ->
  195. {"Authorization", "Bearer " ++ binary_to_list(AuthorizationToken)};
  196. _ ->
  197. {}
  198. end
  199. ],
  200. HTTPOptions = [],
  201. Options = [{body_format, binary}],
  202. inets:start(),
  203. httpc:request(Method, {URL, Headers, ContentType, RequestBody}, HTTPOptions, Options).
  204. -spec format_multipart_formdata(Data, Params, Name, FileNames, MimeType, Boundary) ->
  205. binary()
  206. when
  207. Data :: binary(),
  208. Params :: list(),
  209. Name :: binary(),
  210. FileNames :: list(),
  211. MimeType :: binary(),
  212. Boundary :: binary().
  213. format_multipart_formdata(Data, Params, Name, FileNames, MimeType, Boundary) ->
  214. StartBoundary = erlang:iolist_to_binary([<<"--">>, Boundary]),
  215. LineSeparator = <<"\r\n">>,
  216. WithParams = lists:foldl(
  217. fun({Key, Value}, Acc) ->
  218. erlang:iolist_to_binary([
  219. Acc,
  220. StartBoundary,
  221. LineSeparator,
  222. <<"Content-Disposition: form-data; name=\"">>,
  223. Key,
  224. <<"\"">>,
  225. LineSeparator,
  226. LineSeparator,
  227. Value,
  228. LineSeparator
  229. ])
  230. end,
  231. <<"">>,
  232. Params
  233. ),
  234. WithPaths = lists:foldl(
  235. fun(FileName, Acc) ->
  236. erlang:iolist_to_binary([
  237. Acc,
  238. StartBoundary,
  239. LineSeparator,
  240. <<"Content-Disposition: form-data; name=\"">>,
  241. Name,
  242. <<"\"; filename=\"">>,
  243. FileName,
  244. <<"\"">>,
  245. LineSeparator,
  246. <<"Content-Type: ">>,
  247. MimeType,
  248. LineSeparator,
  249. LineSeparator,
  250. Data,
  251. LineSeparator
  252. ])
  253. end,
  254. WithParams,
  255. FileNames
  256. ),
  257. erlang:iolist_to_binary([WithPaths, StartBoundary, <<"--">>, LineSeparator]).