CurlConnectionTests.cpp (3291B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 4 #include "../../util/SFSExceptionMatcher.h" 5 #include "../../util/TestHelper.h" 6 #include "ReportingHandler.h" 7 #include "Result.h" 8 #include "connection/CurlConnection.h" 9 #include "connection/mock/MockConnection.h" 10 11 #include <catch2/catch_test_macros.hpp> 12 #include <catch2/matchers/catch_matchers_string.hpp> 13 #include <nlohmann/json.hpp> 14 15 #define TEST(...) TEST_CASE("[CurlConnectionTests] " __VA_ARGS__) 16 17 using namespace SFS; 18 using namespace SFS::details; 19 using namespace SFS::test; 20 using json = nlohmann::json; 21 22 namespace 23 { 24 class MockCurlConnection : public CurlConnection 25 { 26 public: 27 MockCurlConnection(const ReportingHandler& handler, Result::Code& responseCode, std::string& response) 28 : CurlConnection({}, handler) 29 , m_responseCode(responseCode) 30 , m_response(response) 31 { 32 } 33 34 protected: 35 std::string CurlPerform(const std::string&, CurlHeaderList&) override 36 { 37 if (m_responseCode == Result::Success) 38 { 39 return m_response; 40 } 41 throw SFSException(m_responseCode); 42 } 43 44 private: 45 Result::Code& m_responseCode; 46 std::string& m_response; 47 }; 48 } // namespace 49 50 TEST("Testing CurlConnection()") 51 { 52 ReportingHandler handler; 53 handler.SetLoggingCallback(LogCallbackToTest); 54 Result::Code responseCode = Result::HttpNotFound; 55 std::string response = "expected"; 56 std::unique_ptr<Connection> connection = std::make_unique<MockCurlConnection>(handler, responseCode, response); 57 58 SECTION("Testing CurlConnection::Get()") 59 { 60 std::string out; 61 REQUIRE_THROWS_CODE(out = connection->Get("url"), HttpNotFound); 62 REQUIRE(out.empty()); 63 64 responseCode = Result::Success; 65 REQUIRE_NOTHROW(out = connection->Get("url")); 66 REQUIRE(out == response); 67 } 68 69 SECTION("Testing CurlConnection::Post()") 70 { 71 std::string out; 72 REQUIRE_THROWS_CODE(out = connection->Post("url"), HttpNotFound); 73 REQUIRE(out.empty()); 74 75 const json body = {{{"dummy", {}}}}; 76 REQUIRE_THROWS_CODE(out = connection->Post("url", body.dump()), HttpNotFound); 77 REQUIRE(out.empty()); 78 79 responseCode = Result::Success; 80 REQUIRE_NOTHROW(out = connection->Get("url")); 81 REQUIRE(out == response); 82 83 response.clear(); 84 REQUIRE_NOTHROW(out = connection->Post("url", body.dump())); 85 REQUIRE(out == response); 86 } 87 } 88 89 TEST("Testing CurlConnection constructor passing a cv") 90 { 91 ReportingHandler handler; 92 handler.SetLoggingCallback(LogCallbackToTest); 93 94 ConnectionConfig config; 95 config.baseCV = ""; 96 97 REQUIRE_THROWS_CODE_MSG(std::make_unique<CurlConnection>(config, handler), InvalidArg, "cv must not be empty"); 98 99 config.baseCV = "cv"; 100 REQUIRE_THROWS_CODE_MSG_MATCHES(std::make_unique<CurlConnection>(config, handler), 101 InvalidArg, 102 Catch::Matchers::ContainsSubstring("baseCV is not a valid correlation vector:")); 103 104 config.baseCV = "aaaaaaaaaaaaaaaa.1"; 105 REQUIRE_NOTHROW(std::make_unique<CurlConnection>(config, handler)); 106 }