From 2ae0125562d59fc88bc87bd79fb29e3a14c66cab Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Wed, 16 Nov 2022 10:57:13 -0300 Subject: [PATCH] test(refactor): encode toxiproxy payloads with emqx_json --- apps/emqx/test/emqx_common_test_helpers.erl | 40 +++++++++++++-------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/apps/emqx/test/emqx_common_test_helpers.erl b/apps/emqx/test/emqx_common_test_helpers.erl index 196546b7b..8ab3b3ace 100644 --- a/apps/emqx/test/emqx_common_test_helpers.erl +++ b/apps/emqx/test/emqx_common_test_helpers.erl @@ -837,12 +837,13 @@ switch_proxy(Switch, Name, ProxyHost, ProxyPort) -> Url = "http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name, Body = case Switch of - off -> <<"{\"enabled\":false}">>; - on -> <<"{\"enabled\":true}">> + off -> #{<<"enabled">> => false}; + on -> #{<<"enabled">> => true} end, + BodyBin = emqx_json:encode(Body), {ok, {{_, 200, _}, _, _}} = httpc:request( post, - {Url, [], "application/json", Body}, + {Url, [], "application/json", BodyBin}, [], [{body_format, binary}] ). @@ -852,14 +853,17 @@ timeout_proxy(on, Name, ProxyHost, ProxyPort) -> "http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name ++ "/toxics", NameBin = list_to_binary(Name), - Body = - <<"{\"name\":\"", NameBin/binary, - "_timeout\",\"type\":\"timeout\"," - "\"stream\":\"upstream\",\"toxicity\":1.0," - "\"attributes\":{\"timeout\":0}}">>, + Body = #{ + <<"name">> => <>, + <<"type">> => <<"timeout">>, + <<"stream">> => <<"upstream">>, + <<"toxicity">> => 1.0, + <<"attributes">> => #{<<"timeout">> => 0} + }, + BodyBin = emqx_json:encode(Body), {ok, {{_, 200, _}, _, _}} = httpc:request( post, - {Url, [], "application/json", Body}, + {Url, [], "application/json", BodyBin}, [], [{body_format, binary}] ); @@ -881,14 +885,20 @@ latency_up_proxy(on, Name, ProxyHost, ProxyPort) -> "http://" ++ ProxyHost ++ ":" ++ integer_to_list(ProxyPort) ++ "/proxies/" ++ Name ++ "/toxics", NameBin = list_to_binary(Name), - Body = - <<"{\"name\":\"", NameBin/binary, - "_latency_up\",\"type\":\"latency\"," - "\"stream\":\"upstream\",\"toxicity\":1.0," - "\"attributes\":{\"latency\":20000,\"jitter\":3000}}">>, + Body = #{ + <<"name">> => <>, + <<"type">> => <<"latency">>, + <<"stream">> => <<"upstream">>, + <<"toxicity">> => 1.0, + <<"attributes">> => #{ + <<"latency">> => 20_000, + <<"jitter">> => 3_000 + } + }, + BodyBin = emqx_json:encode(Body), {ok, {{_, 200, _}, _, _}} = httpc:request( post, - {Url, [], "application/json", Body}, + {Url, [], "application/json", BodyBin}, [], [{body_format, binary}] );