test(api): cover the accept logic for `/configs` API
This commit is contained in:
parent
966b2affc2
commit
71d1f80530
|
@ -122,6 +122,7 @@ schema("/configs") ->
|
||||||
}}
|
}}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
400 => emqx_dashboard_swagger:error_codes(['INVALID_ACCEPT']),
|
||||||
404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND']),
|
404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND']),
|
||||||
500 => emqx_dashboard_swagger:error_codes(['BAD_NODE'])
|
500 => emqx_dashboard_swagger:error_codes(['BAD_NODE'])
|
||||||
}
|
}
|
||||||
|
@ -349,7 +350,7 @@ configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
|
||||||
{error, Errors} -> {400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Errors)}}
|
{error, Errors} -> {400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Errors)}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
find_suitable_accept(Headers, Perferences) ->
|
find_suitable_accept(Headers, Perferences) when is_list(Perferences), length(Perferences) > 0 ->
|
||||||
AcceptVal = maps:get(<<"accept">>, Headers, <<"*/*">>),
|
AcceptVal = maps:get(<<"accept">>, Headers, <<"*/*">>),
|
||||||
%% Multiple types, weighted with the quality value syntax:
|
%% Multiple types, weighted with the quality value syntax:
|
||||||
%% Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8
|
%% Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8
|
||||||
|
@ -362,12 +363,12 @@ find_suitable_accept(Headers, Perferences) ->
|
||||||
),
|
),
|
||||||
case lists:member(<<"*/*">>, Accepts) of
|
case lists:member(<<"*/*">>, Accepts) of
|
||||||
true ->
|
true ->
|
||||||
{ok, lists:first(Perferences)};
|
{ok, lists:nth(1, Perferences)};
|
||||||
fales ->
|
false ->
|
||||||
Found = lists:filter(fun(Accept) -> lists:member(Accept, Accepts) end, Perferences),
|
Found = lists:filter(fun(Accept) -> lists:member(Accept, Accepts) end, Perferences),
|
||||||
case Found of
|
case Found of
|
||||||
[] -> {error, no_suitalbe_accept};
|
[] -> {error, no_suitalbe_accept};
|
||||||
_ -> lists:first(Found)
|
_ -> {ok, lists:nth(1, Found)}
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -323,6 +323,36 @@ t_configs_key(_Config) ->
|
||||||
?assertEqual(<<"error">>, read_conf([<<"log">>, <<"console">>, <<"level">>])),
|
?assertEqual(<<"error">>, read_conf([<<"log">>, <<"console">>, <<"level">>])),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
t_get_configs_in_different_accept(_Config) ->
|
||||||
|
[Key | _] = lists:sort(emqx_conf_cli:keys()),
|
||||||
|
URI = emqx_mgmt_api_test_util:api_path(["configs?key=" ++ Key]),
|
||||||
|
Auth = emqx_mgmt_api_test_util:auth_header_(),
|
||||||
|
Request = fun(Accept) ->
|
||||||
|
Headers = [{"accept", Accept}, Auth],
|
||||||
|
case
|
||||||
|
emqx_mgmt_api_test_util:request_api(get, URI, [], Headers, [], #{return_all => true})
|
||||||
|
of
|
||||||
|
{ok, {{_, Code, _}, RespHeaders, Body}} ->
|
||||||
|
Type = proplists:get_value("content-type", RespHeaders),
|
||||||
|
{Code, Type, Body};
|
||||||
|
{error, {{_, Code, _}, RespHeaders, Body}} ->
|
||||||
|
Type = proplists:get_value("content-type", RespHeaders),
|
||||||
|
{Code, Type, Body}
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
%% returns text/palin if text/plain is acceptable
|
||||||
|
?assertMatch({200, "text/plain", _}, Request(<<"text/plain">>)),
|
||||||
|
?assertMatch({200, "text/plain", _}, Request(<<"*/*">>)),
|
||||||
|
?assertMatch(
|
||||||
|
{200, "text/plain", _},
|
||||||
|
Request(<<"application/json, application/xml;q=0.9, image/webp, */*;q=0.8">>)
|
||||||
|
),
|
||||||
|
%% returns application/json if it only support it
|
||||||
|
?assertMatch({200, "application/json", _}, Request(<<"application/json">>)),
|
||||||
|
%% returns error if it set to other type
|
||||||
|
?assertMatch({400, "application/json", _}, Request(<<"application/xml">>)).
|
||||||
|
|
||||||
%% Helpers
|
%% Helpers
|
||||||
|
|
||||||
get_config(Name) ->
|
get_config(Name) ->
|
||||||
|
|
Loading…
Reference in New Issue