test: api returns for lwm2m client posts

Add test coverage for HTTP 204 returns from lwm2m client API
posts. This includes a small refactoring of the request functionality
in emqx_mgmt_api_test_util.
This commit is contained in:
Erik Timan 2022-12-05 15:30:03 +01:00
parent a8c9d02871
commit 8009f0a3a8
3 changed files with 15 additions and 7 deletions

View File

@ -253,7 +253,7 @@ t_read(Config) ->
test_recv_mqtt_response(RespTopic),
%% step2, call Read API
call_send_api(Epn, "read", "path=/3/0/0"),
?assertMatch({204, []}, call_send_api(Epn, "read", "path=/3/0/0")),
timer:sleep(100),
#coap_message{type = Type, method = Method, options = Opts} = test_recv_coap_request(UdpSock),
?assertEqual(con, Type),
@ -289,7 +289,7 @@ t_write(Config) ->
test_recv_mqtt_response(RespTopic),
%% step2, call write API
call_send_api(Epn, "write", "path=/3/0/13&type=Integer&value=123"),
?assertMatch({204, []}, call_send_api(Epn, "write", "path=/3/0/13&type=Integer&value=123")),
timer:sleep(100),
#coap_message{type = Type, method = Method, options = Opts} = test_recv_coap_request(UdpSock),
?assertEqual(con, Type),
@ -326,7 +326,7 @@ t_observe(Config) ->
test_recv_mqtt_response(RespTopic),
%% step2, call observe API
call_deprecated_send_api(Epn, "observe", "path=/3/0/1&enable=false"),
?assertMatch({204, []}, call_deprecated_send_api(Epn, "observe", "path=/3/0/1&enable=false")),
timer:sleep(100),
#coap_message{type = Type, method = Method, options = Opts} = test_recv_coap_request(UdpSock),
?assertEqual(con, Type),
@ -354,9 +354,12 @@ call_deprecated_send_api(ClientId, Cmd, Query) ->
call_send_api(ClientId, Cmd, Query, API) ->
ApiPath = emqx_mgmt_api_test_util:api_path([API, ClientId, Cmd]),
Auth = emqx_mgmt_api_test_util:auth_header_(),
{ok, Response} = emqx_mgmt_api_test_util:request_api(post, ApiPath, Query, Auth),
Opts = #{return_body => true},
{ok, {{"HTTP/1.1", StatusCode, _}, _Headers, Response}} = emqx_mgmt_api_test_util:request_api(
post, ApiPath, Query, Auth, [], Opts
),
?LOGT("rest api response:~ts~n", [Response]),
Response.
{StatusCode, Response}.
no_received_request(ClientId, Path, Action) ->
Response = call_lookup_api(ClientId, Path, Action),

View File

@ -264,6 +264,7 @@ get_configs(Node, Opts) ->
end,
URI = emqx_mgmt_api_test_util:api_path(Path),
case emqx_mgmt_api_test_util:request_api(get, URI, [], [], [], Opts) of
{ok, {_, _, Res}} -> {ok, emqx_json:decode(Res, [return_maps])};
{ok, Res} -> {ok, emqx_json:decode(Res, [return_maps])};
Error -> Error
end.

View File

@ -94,10 +94,14 @@ do_request_api(Method, Request, Opts) ->
case httpc:request(Method, Request, [], []) of
{error, socket_closed_remotely} ->
{error, socket_closed_remotely};
{ok, {{"HTTP/1.1", Code, _}, _, Return}} when
{ok, {{"HTTP/1.1", Code, _} = Reason, Headers, Body}} when
Code >= 200 andalso Code =< 299 andalso ReturnBody
->
{ok, {Reason, Headers, Body}};
{ok, {{"HTTP/1.1", Code, _}, _, Body}} when
Code >= 200 andalso Code =< 299
->
{ok, Return};
{ok, Body};
{ok, {Reason, Headers, Body}} when ReturnBody ->
{error, {Reason, Headers, Body}};
{ok, {Reason, _Headers, _Body}} ->