Skip to content

Commit

Permalink
fix: in HttpClientApplication don't crash when detaching during Statu…
Browse files Browse the repository at this point in the history
…sAvailable (#770)
  • Loading branch information
richardapeters authored Nov 12, 2024
1 parent ea7de38 commit e626133
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 4 additions & 2 deletions services/network/HttpClientAuthentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ namespace services
Observer().StatusAvailable(HttpStatusCode::Unauthorized);
}

Observer().BodyComplete();
if (HttpClient::IsAttached())
Observer().BodyComplete();
}

void HttpClientAuthentication::SendStreamAvailable(infra::SharedPtr<infra::StreamWriter>&& writer)
Expand All @@ -170,7 +171,8 @@ namespace services

void HttpClientAuthentication::Detaching()
{
HttpClient::Detach();
if (HttpClient::IsAttached())
HttpClient::Detach();
HttpClientObserver::Detaching();
}

Expand Down
27 changes: 26 additions & 1 deletion services/network/test/TestHttpClientAuthentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class HttpClientAuthenticationTest

~HttpClientAuthenticationTest() override
{
EXPECT_CALL(*httpClientObserver, Detaching());
if (httpClientObserver->IsAttached())
EXPECT_CALL(*httpClientObserver, Detaching());
}

void CheckHeaders(services::HttpHeaders headersToCheck, infra::BoundedConstString headerValueToAdd)
Expand Down Expand Up @@ -261,3 +262,27 @@ TEST_F(HttpClientAuthenticationTest, unauthorized_is_retried)
}));
httpClient.Observer().BodyComplete();
}

TEST_F(HttpClientAuthenticationTest, detach_during_StatusAvailable)
{
Get();

httpClient.Observer().StatusAvailable(services::HttpStatusCode::Unauthorized);

httpClient.Observer().HeaderAvailable({ "name", "value" });
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
EXPECT_CALL(clientAuthentication, Authenticate(services::HttpVerb::get, "target", "scheme", "value"));
httpClient.Observer().HeaderAvailable({ "WWW-Authenticate", "scheme value" });

testing::StrictMock<infra::StreamReaderMock> reader;
EXPECT_CALL(reader, Empty()).WillOnce(testing::Return(true));
httpClient.Observer().BodyAvailable(infra::UnOwnedSharedPtr(reader));

EXPECT_CALL(clientAuthentication, Retry()).WillOnce(testing::Return(false));
EXPECT_CALL(*httpClientObserver, StatusAvailable(services::HttpStatusCode::Unauthorized)).WillOnce(testing::Invoke([this](services::HttpStatusCode)
{
EXPECT_CALL(*httpClientObserver, Detaching());
httpClientObserver->Detach();
}));
httpClient.Observer().BodyComplete();
}

0 comments on commit e626133

Please sign in to comment.