Skip to content

Commit

Permalink
Fix header being overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
offa committed Oct 23, 2023
1 parent c7f74ca commit 86be861
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/HTTP.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ namespace influxdb::transports

void HTTP::setAuthToken(const std::string& token)
{
session.SetHeader(cpr::Header{{"Authorization", "Token " + token}});
session.UpdateHeader(cpr::Header{{"Authorization", "Token " + token}});
}

void HTTP::send(std::string&& lineprotocol)
{
session.SetUrl(cpr::Url{endpointUrl + "/write"});
session.SetHeader(cpr::Header{{"Content-Type", "application/json"}});
session.UpdateHeader(cpr::Header{{"Content-Type", "application/json"}});
session.SetParameters(cpr::Parameters{{"db", databaseName}});
session.SetBody(cpr::Body{lineprotocol});

Expand Down
10 changes: 5 additions & 5 deletions test/HttpTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace influxdb::test

REQUIRE_CALL(sessionMock, Post()).RETURN(createResponse(cpr::ErrorCode::OK, cpr::status::HTTP_OK));
REQUIRE_CALL(sessionMock, SetUrl(eq("http://localhost:8086/write")));
REQUIRE_CALL(sessionMock, SetHeader(_)).WITH(_1.at("Content-Type") == "application/json");
REQUIRE_CALL(sessionMock, UpdateHeader(_)).WITH(_1.at("Content-Type") == "application/json");
REQUIRE_CALL(sessionMock, SetBody(_)).WITH(_1.str() == data);
REQUIRE_CALL(sessionMock, SetParameters(ParamMap{{"db", "test"}}));

Expand All @@ -95,7 +95,7 @@ namespace influxdb::test

REQUIRE_CALL(sessionMock, Post()).RETURN(createResponse(cpr::ErrorCode::INTERNAL_ERROR, cpr::status::HTTP_OK));
ALLOW_CALL(sessionMock, SetUrl(_));
ALLOW_CALL(sessionMock, SetHeader(_));
ALLOW_CALL(sessionMock, UpdateHeader(_));
ALLOW_CALL(sessionMock, SetBody(_));
ALLOW_CALL(sessionMock, SetParameters(_));

Expand All @@ -108,7 +108,7 @@ namespace influxdb::test

REQUIRE_CALL(sessionMock, Post()).RETURN(createResponse(cpr::ErrorCode::OK, cpr::status::HTTP_OK));
ALLOW_CALL(sessionMock, SetUrl(_));
ALLOW_CALL(sessionMock, SetHeader(_));
ALLOW_CALL(sessionMock, UpdateHeader(_));
ALLOW_CALL(sessionMock, SetBody(_));
ALLOW_CALL(sessionMock, SetParameters(_));

Expand All @@ -121,7 +121,7 @@ namespace influxdb::test

REQUIRE_CALL(sessionMock, Post()).RETURN(createResponse(cpr::ErrorCode::OK, cpr::status::HTTP_NOT_FOUND));
ALLOW_CALL(sessionMock, SetUrl(_));
ALLOW_CALL(sessionMock, SetHeader(_));
ALLOW_CALL(sessionMock, UpdateHeader(_));
ALLOW_CALL(sessionMock, SetBody(_));
ALLOW_CALL(sessionMock, SetParameters(_));

Expand Down Expand Up @@ -229,7 +229,7 @@ namespace influxdb::test
{
auto http = createHttp();

REQUIRE_CALL(sessionMock, SetHeader(_)).WITH(_1.at("Authorization") == "Token not-a-real-api-token");
REQUIRE_CALL(sessionMock, UpdateHeader(_)).WITH(_1.at("Authorization") == "Token not-a-real-api-token");
http.setAuthToken("not-a-real-api-token");
}

Expand Down
4 changes: 4 additions & 0 deletions test/mock/CprMock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ namespace cpr
influxdb::test::sessionMock.SetHeader(header);
}

void Session::UpdateHeader(const Header& header)
{
influxdb::test::sessionMock.UpdateHeader(header);
}

Parameters::Parameters(const std::initializer_list<Parameter>& parameters)
: CurlContainer<Parameter>(parameters)
Expand Down
1 change: 1 addition & 0 deletions test/mock/CprMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace influxdb::test
MAKE_MOCK0(Post, cpr::Response());
MAKE_MOCK1(SetUrl, void(const cpr::Url&));
MAKE_MOCK1(SetHeader, void(const cpr::Header&));
MAKE_MOCK1(UpdateHeader, void(const cpr::Header&));
MAKE_MOCK1(SetBody, void(cpr::Body&&));
MAKE_MOCK1(SetParameters, void(std::map<std::string, std::string>));
MAKE_MOCK1(SetAuth, void(const cpr::Authentication&));
Expand Down

1 comment on commit 86be861

@kasparthommen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll check tomorrow!

Please sign in to comment.