test(coap): cover the params optional logic to create connection

This commit is contained in:
JianBo He 2023-07-05 16:24:15 +08:00
parent 5c901a52bd
commit 800b154582
3 changed files with 52 additions and 21 deletions

View File

@ -486,23 +486,21 @@ enrich_conninfo(
conninfo = ConnInfo
}
) ->
ClientId =
case maps:get(<<"clientid">>, Queries, undefined) of
undefined ->
emqx_gateway_utils:random_clientid(coap);
ClientId0 ->
ClientId0
end,
Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)),
NConnInfo = ConnInfo#{
clientid => ClientId,
proto_name => <<"CoAP">>,
proto_ver => <<"1">>,
clean_start => true,
keepalive => Interval,
expiry_interval => 0
},
{ok, Channel#channel{conninfo = NConnInfo}}.
case Queries of
#{<<"clientid">> := ClientId} ->
Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)),
NConnInfo = ConnInfo#{
clientid => ClientId,
proto_name => <<"CoAP">>,
proto_ver => <<"1">>,
clean_start => true,
keepalive => Interval,
expiry_interval => 0
},
{ok, Channel#channel{conninfo = NConnInfo}};
_ ->
{error, "clientid is required", Channel}
end.
enrich_clientinfo(
{Queries, Msg},

View File

@ -133,6 +133,42 @@ t_connection(_) ->
end,
do(Action).
t_connection_optional_params(_) ->
UsernamePasswordAreOptional =
fun(Channel) ->
URI =
?MQTT_PREFIX ++
"/connection?clientid=client1",
Req = make_req(post),
{ok, created, Data} = do_request(Channel, URI, Req),
#coap_content{payload = Token0} = Data,
Token = binary_to_list(Token0),
timer:sleep(100),
?assertNotEqual(
[],
emqx_gateway_cm_registry:lookup_channels(coap, <<"client1">>)
),
disconnection(Channel, Token),
timer:sleep(100),
?assertEqual(
[],
emqx_gateway_cm_registry:lookup_channels(coap, <<"client1">>)
)
end,
ClientIdIsRequired =
fun(Channel) ->
URI =
?MQTT_PREFIX ++
"/connection",
Req = make_req(post),
{error, bad_request, _} = do_request(Channel, URI, Req)
end,
do(UsernamePasswordAreOptional),
do(ClientIdIsRequired).
t_connection_with_authn_failed(_) ->
ChId = {{127, 0, 0, 1}, 5683},
{ok, Sock} = er_coap_udp_socket:start_link(),

View File

@ -782,7 +782,7 @@ enrich_clientinfo(InClientInfo = #{proto_name := ProtoName}, ClientInfo) ->
default_conninfo(ConnInfo) ->
ConnInfo#{
clean_start => true,
clientid => anonymous_clientid(),
clientid => emqx_gateway_utils:random_clientid(exproto),
username => undefined,
conn_props => #{},
connected => true,
@ -822,6 +822,3 @@ proto_name_to_protocol(<<>>) ->
exproto;
proto_name_to_protocol(ProtoName) when is_binary(ProtoName) ->
binary_to_atom(ProtoName).
anonymous_clientid() ->
iolist_to_binary(["exproto-", emqx_utils:gen_id()]).