From 86be86105ac4d66f515b97ab6e591606b734fe80 Mon Sep 17 00:00:00 2001 From: offa Date: Mon, 23 Oct 2023 17:12:30 +0200 Subject: [PATCH] Fix header being overwritten --- src/HTTP.cxx | 4 ++-- test/HttpTest.cxx | 10 +++++----- test/mock/CprMock.cxx | 4 ++++ test/mock/CprMock.h | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/HTTP.cxx b/src/HTTP.cxx index 1cbbeec..4ac9de4 100644 --- a/src/HTTP.cxx +++ b/src/HTTP.cxx @@ -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}); diff --git a/test/HttpTest.cxx b/test/HttpTest.cxx index cc70f19..3344105 100644 --- a/test/HttpTest.cxx +++ b/test/HttpTest.cxx @@ -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"}})); @@ -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(_)); @@ -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(_)); @@ -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(_)); @@ -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"); } diff --git a/test/mock/CprMock.cxx b/test/mock/CprMock.cxx index b820aad..9a53bac 100644 --- a/test/mock/CprMock.cxx +++ b/test/mock/CprMock.cxx @@ -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& parameters) : CurlContainer(parameters) diff --git a/test/mock/CprMock.h b/test/mock/CprMock.h index 400a9b7..210572d 100644 --- a/test/mock/CprMock.h +++ b/test/mock/CprMock.h @@ -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)); MAKE_MOCK1(SetAuth, void(const cpr::Authentication&));