Files
lemonade/test/cpp/test_github_api.cpp
T
Andrew Vavilchenko 012bc5ab58 fix: add GITHUB_TOKEN / GH_TOKEN support to GitHub API requests (#2995)
* fix: add GITHUB_TOKEN support to GitHub API requests to avoid 403 rate-limiting

Unauthenticated GitHub API calls are capped at 60 requests/hour, causing 403s under typical usage (BackendManager scans multiple repos on startup). Add a github_api_headers() helper that reads GITHUB_TOKEN / GH_TOKEN from the environment and injects an Authorization header, raising the limit to 5 000 req/h.

Applied in fetch_latest_github_tag(), resolve_asset_wildcard(), and recipe_import.cpp — the three code paths that issue HTTP GET to api.github.com.

* fix: address PR #2995 review — CI ON, retry on 401/404, fix empty-token test

- CMakeLists.txt: switch GithubApiTest to CI ON per project testing guide
- github_api.h: retry without auth on 401/403/404 (scoped GITHUB_TOKEN
  can surface as any of these on unrelated repos)
- test_github_api.cpp: set_env() now sets empty strings on POSIX too;
  test case 4 sets GITHUB_TOKEN="" explicitly to cover the empty-string
  fallback branch instead of unsetting it
2026-08-10 16:03:38 +00:00

139 lines
4.1 KiB
C++

#include <lemon/utils/github_api.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#ifdef _WIN32
#define set_env(k, v) _putenv_s(k, v)
#define unset_env(k) _putenv_s(k, "")
#else
static void set_env(const char* k, const char* v) {
setenv(k, v ? v : "", 1);
}
#define unset_env(k) unsetenv(k)
#endif
struct TestResult {
int passed = 0;
int failed = 0;
void ok(const std::string& name) {
printf("[PASS] %s\n", name.c_str());
++passed;
}
void fail(const std::string& name) {
printf("[FAIL] %s\n", name.c_str());
++failed;
}
};
int main() {
TestResult result;
const std::string kUserAgent{"User-Agent"};
const std::string kAccept{"Accept"};
const std::string kAuth{"Authorization"};
// Save and clear both env vars so tests start from a known state.
const char* saved_github = std::getenv("GITHUB_TOKEN");
const char* saved_gh = std::getenv("GH_TOKEN");
const std::string orig_github = saved_github ? saved_github : "";
const std::string orig_gh = saved_gh ? saved_gh : "";
// 1. Neither GITHUB_TOKEN nor GH_TOKEN set — no Authorization header.
{
unset_env("GITHUB_TOKEN");
unset_env("GH_TOKEN");
auto h = lemon::utils::github_api::headers();
if (h.count(kAuth) == 0 &&
h.at(kUserAgent) == "lemonade" &&
h.count(kAccept) > 0) {
result.ok("no token env vars");
} else {
result.fail("no token env vars");
}
}
// 2. GITHUB_TOKEN set — Authorization uses it.
{
set_env("GITHUB_TOKEN", "tok-ghub");
unset_env("GH_TOKEN");
auto h = lemon::utils::github_api::headers();
if (h.count(kAuth) == 1 &&
h.at(kAuth) == "Bearer tok-ghub") {
result.ok("GITHUB_TOKEN set");
} else {
result.fail("GITHUB_TOKEN set");
}
}
// 3. Only GH_TOKEN set — Authorization uses it.
{
unset_env("GITHUB_TOKEN");
set_env("GH_TOKEN", "tok-gh");
auto h = lemon::utils::github_api::headers();
if (h.count(kAuth) == 1 &&
h.at(kAuth) == "Bearer tok-gh") {
result.ok("only GH_TOKEN set");
} else {
result.fail("only GH_TOKEN set");
}
}
// 4. GITHUB_TOKEN empty, GH_TOKEN set — falls back to GH_TOKEN.
{
set_env("GITHUB_TOKEN", ""); // present but empty
set_env("GH_TOKEN", "tok-gh");
auto h = lemon::utils::github_api::headers();
if (h.count(kAuth) == 1 &&
h.at(kAuth) == "Bearer tok-gh") {
result.ok("empty GITHUB_TOKEN falls back to GH_TOKEN");
} else {
result.fail("empty GITHUB_TOKEN falls back to GH_TOKEN");
}
}
// 5. Both set — GITHUB_TOKEN takes priority.
{
set_env("GITHUB_TOKEN", "tok-ghub");
set_env("GH_TOKEN", "tok-gh");
auto h = lemon::utils::github_api::headers();
if (h.count(kAuth) == 1 &&
h.at(kAuth) == "Bearer tok-ghub") {
result.ok("both set, GITHUB_TOKEN wins");
} else {
result.fail("both set, GITHUB_TOKEN wins");
}
}
// 6. Baseline headers are always present (no tokens).
{
unset_env("GITHUB_TOKEN");
unset_env("GH_TOKEN");
auto h = lemon::utils::github_api::headers();
if (h.count(kUserAgent) == 1 && h.at(kUserAgent) == "lemonade" &&
h.count(kAccept) == 1 && h.at(kAccept) == "application/vnd.github+json") {
result.ok("baseline headers present");
} else {
result.fail("baseline headers present");
}
}
// Restore original environment.
if (orig_github.empty())
unset_env("GITHUB_TOKEN");
else
set_env("GITHUB_TOKEN", orig_github.c_str());
if (orig_gh.empty())
unset_env("GH_TOKEN");
else
set_env("GH_TOKEN", orig_gh.c_str());
printf("\n%d passed, %d failed\n", result.passed, result.failed);
return result.failed > 0 ? 1 : 0;
}