feat(gitea): support extra outbound HTTP headers via GITEA_EXTRA_HEADERS

Add a GITEA_EXTRA_HEADERS environment variable that accepts a JSON
object of header name/value pairs (e.g. Cloudflare Access
credentials) and applies them to every outbound request to Gitea,
both the raw pkg/gitea.DoJSON/DoBytes path and the SDK-backed
pkg/gitea.NewClient path, without overriding Authorization,
Content-Type, or Accept.

Co-Authored-By: Codet <codet@commitgo.dev> (GPT-5-Codex)
This commit is contained in:
Lunny Xiao
2026-08-23 23:33:36 -07:00
parent 815a0e26aa
commit 6744cab627
8 changed files with 197 additions and 3 deletions
+41
View File
@@ -62,3 +62,44 @@ func TestDoJSON_LimitsErrorResponseBody(t *testing.T) {
t.Fatalf("expected body length %d, got %d", errBodySnippetSize, len(httpErr.Body))
}
}
func TestDoJSON_SendsExtraHeaders(t *testing.T) {
var gotClientID, gotAuthorization string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotClientID = r.Header.Get("CF-Access-Client-Id")
gotAuthorization = r.Header.Get("Authorization")
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "{}")
}))
defer srv.Close()
origHost := flag.Host
origToken := flag.Token
origExtraHeaders := flag.ExtraHeaders
defer func() {
flag.Host = origHost
flag.Token = origToken
flag.ExtraHeaders = origExtraHeaders
}()
flag.Host = srv.URL
flag.Token = "the-token"
flag.ExtraHeaders = http.Header{
"Cf-Access-Client-Id": []string{"client-id"},
"Authorization": []string{"should-not-override"},
}
var out map[string]any
status, err := DoJSON(context.Background(), http.MethodGet, "repos/owner/repo", nil, nil, &out)
if err != nil {
t.Fatalf("DoJSON returned error: %v", err)
}
if status != http.StatusOK {
t.Fatalf("expected status %d, got %d", http.StatusOK, status)
}
if gotClientID != "client-id" {
t.Fatalf("CF-Access-Client-Id header = %q, want %q", gotClientID, "client-id")
}
if gotAuthorization != "token the-token" {
t.Fatalf("Authorization header = %q, want %q", gotAuthorization, "token the-token")
}
}