test(refactor): encode toxiproxy payloads with emqx_json

This commit is contained in:
Thales Macedo Garitezi 2022-11-16 10:57:13 -03:00
parent 0f9cc0d93f
commit 2ae0125562
1 changed files with 25 additions and 15 deletions

View File

@ -837,12 +837,13 @@ switch_proxy(Switch, Name, ProxyHost, ProxyPort) ->
Url = "http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name, Url = "http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name,
Body = Body =
case Switch of case Switch of
off -> <<"{\"enabled\":false}">>; off -> #{<<"enabled">> => false};
on -> <<"{\"enabled\":true}">> on -> #{<<"enabled">> => true}
end, end,
BodyBin = emqx_json:encode(Body),
{ok, {{_, 200, _}, _, _}} = httpc:request( {ok, {{_, 200, _}, _, _}} = httpc:request(
post, post,
{Url, [], "application/json", Body}, {Url, [], "application/json", BodyBin},
[], [],
[{body_format, binary}] [{body_format, binary}]
). ).
@ -852,14 +853,17 @@ timeout_proxy(on, Name, ProxyHost, ProxyPort) ->
"http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name ++ "http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name ++
"/toxics", "/toxics",
NameBin = list_to_binary(Name), NameBin = list_to_binary(Name),
Body = Body = #{
<<"{\"name\":\"", NameBin/binary, <<"name">> => <<NameBin/binary, "_timeout">>,
"_timeout\",\"type\":\"timeout\"," <<"type">> => <<"timeout">>,
"\"stream\":\"upstream\",\"toxicity\":1.0," <<"stream">> => <<"upstream">>,
"\"attributes\":{\"timeout\":0}}">>, <<"toxicity">> => 1.0,
<<"attributes">> => #{<<"timeout">> => 0}
},
BodyBin = emqx_json:encode(Body),
{ok, {{_, 200, _}, _, _}} = httpc:request( {ok, {{_, 200, _}, _, _}} = httpc:request(
post, post,
{Url, [], "application/json", Body}, {Url, [], "application/json", BodyBin},
[], [],
[{body_format, binary}] [{body_format, binary}]
); );
@ -881,14 +885,20 @@ latency_up_proxy(on, Name, ProxyHost, ProxyPort) ->
"http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name ++ "http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name ++
"/toxics", "/toxics",
NameBin = list_to_binary(Name), NameBin = list_to_binary(Name),
Body = Body = #{
<<"{\"name\":\"", NameBin/binary, <<"name">> => <<NameBin/binary, "_latency_up">>,
"_latency_up\",\"type\":\"latency\"," <<"type">> => <<"latency">>,
"\"stream\":\"upstream\",\"toxicity\":1.0," <<"stream">> => <<"upstream">>,
"\"attributes\":{\"latency\":20000,\"jitter\":3000}}">>, <<"toxicity">> => 1.0,
<<"attributes">> => #{
<<"latency">> => 20_000,
<<"jitter">> => 3_000
}
},
BodyBin = emqx_json:encode(Body),
{ok, {{_, 200, _}, _, _}} = httpc:request( {ok, {{_, 200, _}, _, _}} = httpc:request(
post, post,
{Url, [], "application/json", Body}, {Url, [], "application/json", BodyBin},
[], [],
[{body_format, binary}] [{body_format, binary}]
); );