test(coap): cover the params optional logic to create connection
This commit is contained in:
parent
5c901a52bd
commit
800b154582
|
@ -486,23 +486,21 @@ enrich_conninfo(
|
||||||
conninfo = ConnInfo
|
conninfo = ConnInfo
|
||||||
}
|
}
|
||||||
) ->
|
) ->
|
||||||
ClientId =
|
case Queries of
|
||||||
case maps:get(<<"clientid">>, Queries, undefined) of
|
#{<<"clientid">> := ClientId} ->
|
||||||
undefined ->
|
Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)),
|
||||||
emqx_gateway_utils:random_clientid(coap);
|
NConnInfo = ConnInfo#{
|
||||||
ClientId0 ->
|
clientid => ClientId,
|
||||||
ClientId0
|
proto_name => <<"CoAP">>,
|
||||||
end,
|
proto_ver => <<"1">>,
|
||||||
Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)),
|
clean_start => true,
|
||||||
NConnInfo = ConnInfo#{
|
keepalive => Interval,
|
||||||
clientid => ClientId,
|
expiry_interval => 0
|
||||||
proto_name => <<"CoAP">>,
|
},
|
||||||
proto_ver => <<"1">>,
|
{ok, Channel#channel{conninfo = NConnInfo}};
|
||||||
clean_start => true,
|
_ ->
|
||||||
keepalive => Interval,
|
{error, "clientid is required", Channel}
|
||||||
expiry_interval => 0
|
end.
|
||||||
},
|
|
||||||
{ok, Channel#channel{conninfo = NConnInfo}}.
|
|
||||||
|
|
||||||
enrich_clientinfo(
|
enrich_clientinfo(
|
||||||
{Queries, Msg},
|
{Queries, Msg},
|
||||||
|
|
|
@ -133,6 +133,42 @@ t_connection(_) ->
|
||||||
end,
|
end,
|
||||||
do(Action).
|
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(_) ->
|
t_connection_with_authn_failed(_) ->
|
||||||
ChId = {{127, 0, 0, 1}, 5683},
|
ChId = {{127, 0, 0, 1}, 5683},
|
||||||
{ok, Sock} = er_coap_udp_socket:start_link(),
|
{ok, Sock} = er_coap_udp_socket:start_link(),
|
||||||
|
|
|
@ -782,7 +782,7 @@ enrich_clientinfo(InClientInfo = #{proto_name := ProtoName}, ClientInfo) ->
|
||||||
default_conninfo(ConnInfo) ->
|
default_conninfo(ConnInfo) ->
|
||||||
ConnInfo#{
|
ConnInfo#{
|
||||||
clean_start => true,
|
clean_start => true,
|
||||||
clientid => anonymous_clientid(),
|
clientid => emqx_gateway_utils:random_clientid(exproto),
|
||||||
username => undefined,
|
username => undefined,
|
||||||
conn_props => #{},
|
conn_props => #{},
|
||||||
connected => true,
|
connected => true,
|
||||||
|
@ -822,6 +822,3 @@ proto_name_to_protocol(<<>>) ->
|
||||||
exproto;
|
exproto;
|
||||||
proto_name_to_protocol(ProtoName) when is_binary(ProtoName) ->
|
proto_name_to_protocol(ProtoName) when is_binary(ProtoName) ->
|
||||||
binary_to_atom(ProtoName).
|
binary_to_atom(ProtoName).
|
||||||
|
|
||||||
anonymous_clientid() ->
|
|
||||||
iolist_to_binary(["exproto-", emqx_utils:gen_id()]).
|
|
||||||
|
|
Loading…
Reference in New Issue