Skip to content

Commit

Permalink
[coap] set request URI for only response message (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
wgtdkp authored Apr 14, 2021
1 parent f9dc23f commit 1b5dd47
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/library/coap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,11 @@ void Coap::HandleResponse(Response &aResponse)
}

requestHolder->mRequest->GetUriPath(requestUri).IgnoreError();
aResponse.SetRequestUri(requestUri);
if (aResponse.IsResponse())
{
aResponse.SetRequestUri(requestUri);
}

switch (aResponse.GetType())
{
case Type::kReset:
Expand Down
34 changes: 33 additions & 1 deletion src/library/coap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ class MockEndpoint : public Endpoint

TEST_CASE("coap-message-confirmable", "[coap]")
{
bool sendSeparateEmptyAck = false;

Address localhost;
REQUIRE(localhost.Set("127.0.0.1") == ErrorCode::kNone);

Expand All @@ -359,7 +361,7 @@ TEST_CASE("coap-message-confirmable", "[coap]")
Coap coap1{eventBase, peer1};

REQUIRE(coap1.AddResource(
{"/hello", [&coap1](const Request &aRequest) {
{"/hello", [&coap1, &sendSeparateEmptyAck](const Request &aRequest) {
REQUIRE(aRequest.IsRequest());
REQUIRE(aRequest.GetType() == Type::kConfirmable);
REQUIRE(aRequest.GetCode() == Code::kGet);
Expand All @@ -374,6 +376,11 @@ TEST_CASE("coap-message-confirmable", "[coap]")

REQUIRE(aRequest.GetPayload() == ByteArray{'h', 'e', 'l', 'l', 'o', ',', ' ', 'C', 'o', 'A', 'P'});

if (sendSeparateEmptyAck)
{
REQUIRE(coap1.SendAck(aRequest) == ErrorCode::kNone);
}

Response response{Type::kAcknowledgment, Code::kContent};
REQUIRE(response.SetContentFormat(ContentFormat::kTextPlain) == ErrorCode::kNone);
response.Append("Ack...");
Expand Down Expand Up @@ -403,6 +410,31 @@ TEST_CASE("coap-message-confirmable", "[coap]")
});
}

SECTION("basic confirmable message with separate ack")
{
Message request{Type::kConfirmable, Code::kGet};
REQUIRE(request.SetUriPath("/hello") == ErrorCode::kNone);
REQUIRE(request.SetContentFormat(ContentFormat::kTextPlain) == ErrorCode::kNone);
request.Append("hello, CoAP");

sendSeparateEmptyAck = true;

coap0.SendRequest(request, [&eventBase](const Response *aResponse, Error aError) {
REQUIRE(aResponse != nullptr);
REQUIRE(aError == ErrorCode::kNone);

REQUIRE(aResponse->GetType() == Type::kAcknowledgment);

ContentFormat contentFormat;
REQUIRE(aResponse->GetContentFormat(contentFormat) == ErrorCode::kNone);
REQUIRE(contentFormat == ContentFormat::kTextPlain);

REQUIRE(aResponse->GetPayload() == ByteArray{'A', 'c', 'k', '.', '.', '.'});

event_base_loopbreak(eventBase);
});
}

SECTION("basic confirmable message retransmission")
{
Message request{Type::kConfirmable, Code::kGet};
Expand Down

0 comments on commit 1b5dd47

Please sign in to comment.