From 2eb8897706128f3590a5b4f9ee8f3e755b74f097 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 9 Sep 2025 23:24:35 +0100 Subject: Add test over mocked user agent fetch Uses a file:// URL to curl fetch a local fixture. --- test/Jamfile.jam | 2 ++ test/fixtures/userAgent.json | 14 ++++++++++++++ test/test-ingest.cpp | 15 +++++++++++++++ test/test-util.hpp | 3 +++ 4 files changed, 34 insertions(+) create mode 100644 test/fixtures/userAgent.json (limited to 'test') diff --git a/test/Jamfile.jam b/test/Jamfile.jam index 0542a73..217feec 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -5,10 +5,12 @@ lib stdc++fs ; path-constant src : ../src ; path-constant test : . ; +path-constant fixtures : fixtures ; project WebStat-Testing : requirements SRC=\"$(src)\" TEST=\"$(test)\" + FIXTURES=\"$(fixtures)\" $(src)//webstat ; diff --git a/test/fixtures/userAgent.json b/test/fixtures/userAgent.json new file mode 100644 index 0000000..a3a8fcb --- /dev/null +++ b/test/fixtures/userAgent.json @@ -0,0 +1,14 @@ +{ + "agent_type": "Browser", + "agent_name": "Chrome", + "agent_version": "140.0.0.0", + "os_type": "Linux", + "os_name": "Linux", + "os_versionName": "", + "os_versionNumber": "", + "os_producer": "", + "os_producerURL": "", + "linux_distibution": "Null", + "agent_language": "", + "agent_languageTag": "" +} diff --git a/test/test-ingest.cpp b/test/test-ingest.cpp index fd7ad65..9b21e44 100644 --- a/test/test-ingest.cpp +++ b/test/test-ingest.cpp @@ -208,6 +208,21 @@ BOOST_AUTO_TEST_CASE(StoreLog, *boost::unit_test::depends_on("I/StoreLogLine")) BOOST_AUTO_TEST_SUITE_END(); +BOOST_AUTO_TEST_CASE(FetchMockUserAgentDetail) +{ + const auto uaDetailReq = WebStat::curlGetUserAgentDetail( + R"(Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36)", + (FIXTURE_URL_BASE + "/userAgent.json").c_str()); + BOOST_REQUIRE(uaDetailReq); + BOOST_REQUIRE_EQUAL(CURLE_OK, curl_easy_perform(uaDetailReq->hnd.get())); + + BOOST_TEST_CONTEXT(uaDetailReq->result) { + BOOST_CHECK(uaDetailReq->result.contains(R"("agent_type":)")); + BOOST_CHECK(uaDetailReq->result.contains(R"("os_type":)")); + BOOST_CHECK(uaDetailReq->result.contains(R"("Chrome")")); + } +} + BOOST_AUTO_TEST_CASE(FetchRealUserAgentDetail, *boost::unit_test::disabled()) { const auto uaDetailReq = WebStat::curlGetUserAgentDetail( diff --git a/test/test-util.hpp b/test/test-util.hpp index c17418f..ac96ee6 100644 --- a/test/test-util.hpp +++ b/test/test-util.hpp @@ -10,6 +10,9 @@ namespace WebStat { #define XSTR(s) STR(s) #define STR(s) #s inline const std::filesystem::path SRC_DIR(XSTR(SRC)); + inline const std::filesystem::path TEST_DIR(XSTR(TEST)); + inline const std::filesystem::path FIXTURE_DIR(XSTR(FIXTURES)); + inline const std::string FIXTURE_URL_BASE = "file://" + std::filesystem::canonical(FIXTURE_DIR).string(); #undef XSTR #undef STR -- cgit v1.2.3