#include "openpu/api_client.hpp" #include #include using namespace openpu; namespace { /// Replays canned responses and records the requests it received. class FakeTransport final : public Transport { public: std::deque responses; std::vector requests; bool failNetwork = false; HttpResponse perform(const HttpRequest& request) override { requests.push_back(request); if (failNetwork) throw NetworkError("could not resolve host"); REQUIRE_FALSE(responses.empty()); HttpResponse r = responses.front(); responses.pop_front(); return r; } }; HttpResponse ok(std::string body) { HttpResponse r; r.status = 200; r.body = std::move(body); return r; } HttpResponse status(long code) { HttpResponse r; r.status = code; return r; } const char* const kVin = "VR3ABCDEFGH123456"; ApiConfig testConfig() { ApiConfig c; c.devicesUrl = "https://api.test/v1/devices/"; c.updatesUrl = "https://api.test/v1/getAvailableUpdate"; c.clientId = "cid"; return c; } const char* const kUpdates = R"({"requestResult":"OK","software":[ {"softwareType":"rcc-firmware","updateRequestResult":"OK","update":[ {"updateId":"1","updateSize":"100","updateVersion":"v2","updateDate":"2024-01-01", "updateURL":"https://dl.test/UpdateDownload?updateId=1","licenseURL":"https://dl.test/LicenseDownload?mediaVersion=1"}]}, {"softwareType":"map-eur","updateRequestResult":"OK","update":[ {"updateId":"2","updateSize":"50","updateVersion":"m9","updateURL":"https://dl.test/UpdateDownload?updateId=2"}]}]})"; } // namespace TEST_CASE("checkVehicle issues devices then getAvailableUpdate with client_id") { FakeTransport t; t.responses.push_back(ok(R"({"devices":[{"ecuType":"RCC_EU_W3"}]})")); t.responses.push_back(ok(kUpdates)); ApiClient api(t, testConfig()); const VehicleCheck c = api.checkVehicle(kVin); CHECK(c.exists); CHECK(c.type == HeadUnitType::Rcc); REQUIRE(t.requests.size() == 2); CHECK(t.requests[0].method == HttpMethod::Get); CHECK(t.requests[0].url == std::string("https://api.test/v1/devices/") + kVin + "?client_id=cid"); CHECK(t.requests[1].method == HttpMethod::Post); CHECK(t.requests[1].url == "https://api.test/v1/getAvailableUpdate?client_id=cid"); CHECK(t.requests[1].body.find("\"crony2-map-western_europe\"") != std::string::npos); // all 7 types bool hasContentType = false; for (const auto& [k, v] : t.requests[1].headers) hasContentType |= (k == "Content-Type" && v == "application/json"); CHECK(hasContentType); } TEST_CASE("checkVehicle: 404 on devices means unknown VIN, no second call") { FakeTransport t; t.responses.push_back(status(404)); ApiClient api(t, testConfig()); CHECK_FALSE(api.checkVehicle(kVin).exists); CHECK(t.requests.size() == 1); } TEST_CASE("errors are mapped to ApiError kinds") { FakeTransport t; ApiClient api(t, testConfig()); SUBCASE("server error") { t.responses.push_back(status(503)); try { api.checkVehicle(kVin); FAIL("expected ApiError"); } catch (const ApiError& e) { CHECK(e.kind() == ApiError::Kind::Server); CHECK(e.status() == 503); } } SUBCASE("network error") { t.failNetwork = true; try { api.getUpdates(kVin, HeadUnitType::Rcc); FAIL("expected ApiError"); } catch (const ApiError& e) { CHECK(e.kind() == ApiError::Kind::Network); } } SUBCASE("bad body") { t.responses.push_back(ok("maintenance")); try { api.getUpdates(kVin, HeadUnitType::Rcc); FAIL("expected ApiError"); } catch (const ApiError& e) { CHECK(e.kind() == ApiError::Kind::BadResponse); } } SUBCASE("invalid VIN never reaches the network") { CHECK_THROWS_AS(api.checkVehicle("VF3SHORT"), ApiError); CHECK(t.requests.empty()); } } TEST_CASE("getUpdates requests the pair of types of the head unit") { FakeTransport t; t.responses.push_back(ok(kUpdates)); ApiClient api(t, testConfig()); const AvailableUpdates u = api.getUpdates(kVin, HeadUnitType::Rcc); REQUIRE(u.firmware.has_value()); CHECK(u.firmware->version == "v2"); CHECK(u.firmware->size == 100); REQUIRE(u.map.has_value()); CHECK(u.map->version == "m9"); CHECK(t.requests[0].body == std::string(R"({"softwareTypes":[{"softwareType":"rcc-firmware"},{"softwareType":"map-eur"}],"vin":")") + kVin + "\"}"); }