diff --git a/apps/emqx/test/emqx_common_test_http.erl b/apps/emqx/test/emqx_common_test_http.erl new file mode 100644 index 000000000..27fcdc268 --- /dev/null +++ b/apps/emqx/test/emqx_common_test_http.erl @@ -0,0 +1,83 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2019 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +-module(emqx_common_test_http). + +-include_lib("common_test/include/ct.hrl"). + +-export([ request_api/3 + , request_api/4 + , request_api/5 + , get_http_data/1 + , create_default_app/0 + , delete_default_app/0 + , default_auth_header/0 + , auth_header/2 +]). + +request_api(Method, Url, Auth) -> + request_api(Method, Url, [], Auth, []). + +request_api(Method, Url, QueryParams, Auth) -> + request_api(Method, Url, QueryParams, Auth, []). + +request_api(Method, Url, QueryParams, Auth, Body) -> + request_api(Method, Url, QueryParams, Auth, Body, []). + +request_api(Method, Url, QueryParams, Auth, Body, HttpOpts) -> + NewUrl = case QueryParams of + [] -> + Url; + _ -> + Url ++ "?" ++ QueryParams + end, + Request = case Body of + [] -> + {NewUrl, [Auth]}; + _ -> + {NewUrl, [Auth], "application/json", emqx_json:encode(Body)} + end, + do_request_api(Method, Request, HttpOpts). + +do_request_api(Method, Request, HttpOpts) -> + ct:pal("Method: ~p, Request: ~p", [Method, Request]), + case httpc:request(Method, Request, HttpOpts, [{body_format, binary}]) of + {error, socket_closed_remotely} -> + {error, socket_closed_remotely}; + {ok, {{"HTTP/1.1", Code, _}, _Headers, Return} } + when Code =:= 200 orelse Code =:= 201 -> + {ok, Return}; + {ok, {Reason, _, _}} -> + {error, Reason} + end. + +get_http_data(ResponseBody) -> + maps:get(<<"data">>, emqx_json:decode(ResponseBody, [return_maps])). + +auth_header(User, Pass) -> + Encoded = base64:encode_to_string(lists:append([User,":",Pass])), + {"Authorization","Basic " ++ Encoded}. + +default_auth_header() -> + AppId = <<"myappid">>, + AppSecret = emqx_mgmt_auth:get_appsecret(AppId), + auth_header(erlang:binary_to_list(AppId), erlang:binary_to_list(AppSecret)). + +create_default_app() -> + emqx_mgmt_auth:add_app(<<"myappid">>, <<"test">>). + +delete_default_app() -> + emqx_mgmt_auth:del_app(<<"myappid">>). diff --git a/apps/emqx/test/emqx_plugins_SUITE_data/emqx_hocon_plugin/rebar.config b/apps/emqx/test/emqx_plugins_SUITE_data/emqx_hocon_plugin/rebar.config index 57bf1245c..0e9fe1067 100644 --- a/apps/emqx/test/emqx_plugins_SUITE_data/emqx_hocon_plugin/rebar.config +++ b/apps/emqx/test/emqx_plugins_SUITE_data/emqx_hocon_plugin/rebar.config @@ -17,7 +17,7 @@ {profiles, [{test, [ - {deps, [{emqx_ct_helpers, {git,"https://github.com/emqx/emqx-ct-helpers.git", {branch,"hocon"}}} + {deps, [ ]} ]} ]}. diff --git a/apps/emqx/test/emqx_plugins_SUITE_data/emqx_mini_plugin/rebar.config b/apps/emqx/test/emqx_plugins_SUITE_data/emqx_mini_plugin/rebar.config index b2bf39c55..239726dc0 100644 --- a/apps/emqx/test/emqx_plugins_SUITE_data/emqx_mini_plugin/rebar.config +++ b/apps/emqx/test/emqx_plugins_SUITE_data/emqx_mini_plugin/rebar.config @@ -17,7 +17,7 @@ {profiles, [{test, [ - {deps, [{emqx_ct_helpers, {git,"https://github.com/emqx/emqx-ct-helpers.git", {branch,"hocon"}}} + {deps, [ ]} ]} ]}. diff --git a/apps/emqx_authz/test/emqx_authz_api_mnesia_SUITE.erl b/apps/emqx_authz/test/emqx_authz_api_mnesia_SUITE.erl index 4f0881bfb..153516f7c 100644 --- a/apps/emqx_authz/test/emqx_authz_api_mnesia_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_api_mnesia_SUITE.erl @@ -24,15 +24,6 @@ -define(CONF_DEFAULT, <<"authorization: {sources: []}">>). --import(emqx_ct_http, [ request_api/3 - , request_api/5 - , get_http_data/1 - , create_default_app/0 - , delete_default_app/0 - , default_auth_header/0 - , auth_header/2 - ]). - -define(HOST, "http://127.0.0.1:18083/"). -define(API_VERSION, "v5"). -define(BASE_PATH, "api"). @@ -123,7 +114,7 @@ set_special_configs(emqx_dashboard) -> emqx_config:put([emqx_dashboard], Config), ok; set_special_configs(emqx_authz) -> - emqx_config:put([authorization], #{sources => [#{type => 'built-in-database', + emqx_config:put([authorization], #{sources => [#{type => 'built-in-database', enable => true} ]}), ok; diff --git a/apps/emqx_authz/test/emqx_authz_api_settings_SUITE.erl b/apps/emqx_authz/test/emqx_authz_api_settings_SUITE.erl index 573b98f18..6e6207bbc 100644 --- a/apps/emqx_authz/test/emqx_authz_api_settings_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_api_settings_SUITE.erl @@ -24,15 +24,6 @@ -define(CONF_DEFAULT, <<"authorization: {sources: []}">>). --import(emqx_ct_http, [ request_api/3 - , request_api/5 - , get_http_data/1 - , create_default_app/0 - , delete_default_app/0 - , default_auth_header/0 - , auth_header/2 - ]). - -define(HOST, "http://127.0.0.1:18083/"). -define(API_VERSION, "v5"). -define(BASE_PATH, "api"). diff --git a/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl b/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl index 72e16a0ec..d6116c01e 100644 --- a/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl @@ -24,15 +24,6 @@ -define(CONF_DEFAULT, <<"authorization: {sources: []}">>). --import(emqx_ct_http, [ request_api/3 - , request_api/5 - , get_http_data/1 - , create_default_app/0 - , delete_default_app/0 - , default_auth_header/0 - , auth_header/2 - ]). - -define(HOST, "http://127.0.0.1:18083/"). -define(API_VERSION, "v5"). -define(BASE_PATH, "api"). diff --git a/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl b/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl index b59a5f0b3..6d245c9bc 100644 --- a/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl +++ b/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl @@ -19,7 +19,7 @@ -compile(nowarn_export_all). -compile(export_all). --import(emqx_ct_http, +-import(emqx_common_test_http, [ request_api/3 , request_api/5 , get_http_data/1 diff --git a/apps/emqx_retainer/test/emqx_retainer_api_SUITE.erl b/apps/emqx_retainer/test/emqx_retainer_api_SUITE.erl index 90eb9f615..266ec0ed6 100644 --- a/apps/emqx_retainer/test/emqx_retainer_api_SUITE.erl +++ b/apps/emqx_retainer/test/emqx_retainer_api_SUITE.erl @@ -22,13 +22,13 @@ -include_lib("eunit/include/eunit.hrl"). -include_lib("common_test/include/ct.hrl"). --import(emqx_ct_http, [ request_api/3 - , request_api/5 - , get_http_data/1 - , create_default_app/0 - , delete_default_app/0 - , default_auth_header/0 - ]). +-import(emqx_common_test_http, [ request_api/3 + , request_api/5 + , get_http_data/1 + , create_default_app/0 + , delete_default_app/0 + , default_auth_header/0 + ]). -define(HOST, "http://127.0.0.1:8081/"). -define(API_VERSION, "v4").