emqx_mgmt_api_test_util.erl 8.5 KB

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