Skip to content

Commit 59e9312

Browse files
committed
test(httpc): Fix some tests
1 parent 2d2f385 commit 59e9312

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

src/ekka_httpc.erl

+14-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ get(Addr, Path, Params, Headers) ->
4040

4141
get(Addr, Path, Params, Headers, HttpOpts) ->
4242
URL = build_url(Addr, Path, Params),
43-
parse_response(hackney:request(get, URL, Headers, <<>>, [with_body | HttpOpts])).
43+
parse_response(hackney:request(get, URL, Headers, <<>>, HttpOpts)).
4444

4545
post(Addr, Path, Params) ->
4646
post(Addr, Path, Params, []).
@@ -49,7 +49,7 @@ post(Addr, Path, Params, HttpOpts) ->
4949
URL = build_url(Addr, Path),
5050
Body = build_query(Params),
5151
Headers = [{<<"Content-Type">>, <<"application/x-www-form-urlencoded">>}],
52-
parse_response(hackney:request(post, URL, Headers, Body, [with_body | HttpOpts])).
52+
parse_response(hackney:request(post, URL, Headers, Body, HttpOpts)).
5353

5454
put(Addr, Path, Params) ->
5555
put(Addr, Path, Params, []).
@@ -58,7 +58,7 @@ put(Addr, Path, Params, HttpOpts) ->
5858
URL = build_url(Addr, Path),
5959
Body = build_query(Params),
6060
Headers = [{<<"Content-Type">>, <<"application/x-www-form-urlencoded">>}],
61-
parse_response(hackney:request(put, URL, Headers, Body, [with_body | HttpOpts])).
61+
parse_response(hackney:request(put, URL, Headers, Body, HttpOpts)).
6262

6363
delete(Addr, Path, Params) ->
6464
delete(Addr, Path, Params, []).
@@ -98,13 +98,19 @@ urlencode(B) when is_binary(B) ->
9898
urlencode(binary_to_list(B)).
9999
-endif.
100100

101-
parse_response({ok, Status, RespHeaders, Body}) when Status =:= 200;
101+
parse_response({ok, Status, _RespHeaders, Ref}) when Status =:= 200;
102102
Status =:= 201 ->
103-
case hackney_headers:parse(<<"content-type">>, RespHeaders) of
104-
{<<"application">>, <<"json">>, _} ->
103+
case hackney:body(Ref) of
104+
{ok, Body} ->
105+
%% case hackney_headers:parse(<<"content-type">>, RespHeaders) of
106+
%% {<<"application">>, <<"json">>, _} ->
107+
%% {ok, jsone:decode(Body)};
108+
%% CC ->
109+
%% {error, {unexpected_content_type, CC}}
110+
%% end;
105111
{ok, jsone:decode(Body)};
106-
CC ->
107-
{error, {unexpected_content_type, CC}}
112+
Err ->
113+
{error, {get_body, Err}}
108114
end;
109115
parse_response({ok, 204, _RespHeaders, _}) ->
110116
{ok, []};

test/ekka_cluster_etcd_SUITE.erl

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%%--------------------------------------------------------------------
2-
%% Copyright (c) 2019-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
2+
%% Copyright (c) 2019-2022, 2025 EMQ Technologies Co., Ltd. All Rights Reserved.
33
%%
44
%% Licensed under the Apache License, Version 2.0 (the "License");
55
%% you may not use this file except in compliance with the License.
@@ -39,53 +39,55 @@ init_per_testcase(t_restart_process, Config) ->
3939
{skip, no_etcd}
4040
end;
4141
init_per_testcase(_TestCase, Config) ->
42-
ok = meck:new(httpc, [non_strict, passthrough, no_history]),
42+
ok = meck:new(ekka_httpc, [non_strict, no_history]),
4343
Config.
4444

4545
end_per_testcase(t_restart_process, _Config) ->
4646
application:stop(eetcd);
4747
end_per_testcase(TestCase, _Config) ->
48-
ok = meck:unload(httpc),
48+
ok = meck:unload(ekka_httpc),
4949
ekka_ct:cleanup(TestCase).
5050

5151
t_discover(_Config) ->
5252
Json = <<"{\"node\": {\"nodes\": [{\"key\": \"ekkacl/[email protected]\"}]}}">>,
53-
ok = meck:expect(httpc, request, fun(get, _Req, _Opts, _) -> {ok, 200, Json} end),
53+
ok = meck:expect(ekka_httpc, get, fun(_Server, _Path, _Params, _Opts) ->
54+
{ok, jsone:decode(Json)}
55+
end),
5456
{ok, ['[email protected]']} = ekka_cluster_strategy:discover(ekka_cluster_etcd, ?OPTIONS).
5557

5658
t_lock(_Config) ->
57-
ok = meck:expect(httpc, request, fun(put, _Req, _Opts, _) ->
58-
{ok, 200, <<"{\"errorCode\": 0}">>}
59-
end),
59+
ok = meck:expect(ekka_httpc, put, fun(_Server, _Path, _Params, _Opts) ->
60+
{ok, jsone:decode(<<"{\"errorCode\": 0}">>)}
61+
end),
6062
ok = ekka_cluster_strategy:lock(ekka_cluster_etcd, ?OPTIONS).
6163

6264
t_unlock(_) ->
63-
ok = meck:expect(httpc, request, fun(delete, _Req, _Opts, _) ->
64-
{ok, 200, <<"{\"errorCode\": 0}">>}
65-
end),
65+
ok = meck:expect(ekka_httpc, delete, fun(_Server, _Path, _Params, _Opts) ->
66+
{ok, jsone:decode(<<"{\"errorCode\": 0}">>)}
67+
end),
6668
ok = ekka_cluster_strategy:unlock(ekka_cluster_etcd, ?OPTIONS).
6769

6870
t_register(_) ->
6971
ok = meck:new(ekka_cluster_sup, [non_strict, passthrough, no_history]),
7072
ok = meck:expect(ekka_cluster_sup, start_child, fun(_, _) -> {ok, self()} end),
71-
ok = meck:expect(httpc, request, fun(put, _Req, _Opts, _) ->
72-
{ok, 200, <<"{\"errorCode\": 0}">>}
73-
end),
73+
ok = meck:expect(ekka_httpc, put, fun(_Server, _Path, _Params, _Opts) ->
74+
{ok, jsone:decode(<<"{\"errorCode\": 0}">>)}
75+
end),
7476
ok = ekka_cluster_strategy:register(ekka_cluster_etcd, ?OPTIONS),
7577
ok = meck:unload(ekka_cluster_sup).
7678

7779
t_unregister(_) ->
78-
ok = meck:expect(httpc, request, fun(delete, _Req, _Opts, _) ->
79-
{ok, 200, <<"{\"errorCode\": 0}">>}
80-
end),
80+
ok = meck:expect(ekka_httpc, delete, fun(_Server, _Path, _Params, _Opts) ->
81+
{ok, jsone:decode(<<"{\"errorCode\": 0}">>)}
82+
end),
8183
ok = meck:expect(ekka_cluster_sup, stop_child, fun(_) -> ok end),
8284
ok = ekka_cluster_strategy:unregister(ekka_cluster_etcd, ?OPTIONS),
8385
ok = meck:unload(ekka_cluster_sup).
8486

8587
t_etcd_set_node_key(_) ->
86-
ok = meck:expect(httpc, request, fun(put, _Req, _Opts, _) ->
87-
{ok, 200, <<"{\"errorCode\": 0}">>}
88-
end),
88+
ok = meck:expect(ekka_httpc, put, fun(_Server, _Path, _Params, _Opts) ->
89+
{ok, jsone:decode(<<"{\"errorCode\": 0}">>)}
90+
end),
8991
{ok, #{<<"errorCode">> := 0}} = ekka_cluster_etcd:etcd_set_node_key(?OPTIONS).
9092

9193
t_restart_process(_) ->

test/ekka_cluster_k8s_SUITE.erl

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
all() -> ekka_ct:all(?MODULE).
3232

3333
t_discover(_) ->
34-
ok = meck:new(httpc, [non_strict, passthrough, no_history]),
34+
ok = meck:new(ekka_httpc, [non_strict, no_history]),
3535
Json = <<"{\"subsets\": [{\"addresses\": [{\"ip\": \"192.168.10.10\"}]}]}">>,
36-
ok = meck:expect(hackney, request, fun(get, _Url, _, _Headers, _Opts) ->
37-
{ok, 200, [], Json}
38-
end),
36+
ok = meck:expect(ekka_httpc, get, fun(_Server, _Path, _Params, _Headers, _Opts) ->
37+
{ok, jsone:decode(Json)}
38+
end),
3939
{ok, ['[email protected]']} = ekka_cluster_strategy:discover(ekka_cluster_k8s, [{app_name, "ekka"} | ?OPTIONS]),
4040
%% below test relies on rebar3 ct is run with '--name [email protected]'
4141
{ok, ['[email protected]']} = ekka_cluster_strategy:discover(ekka_cluster_k8s, ?OPTIONS),
42-
ok = meck:unload(httpc).
42+
ok = meck:unload(ekka_httpc).
4343

4444
t_lock(_) ->
4545
ignore = ekka_cluster_static:lock([]).

test/ekka_httpc_SUITE.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%%--------------------------------------------------------------------
2-
%% Copyright (c) 2019, 2025 EMQ Technologies Co., Ltd. All Rights Reserved.
2+
%% Copyright (c) 2019-2025 EMQ Technologies Co., Ltd. All Rights Reserved.
33
%%
44
%% Licensed under the Apache License, Version 2.0 (the "License");
55
%% you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)