Skip to content

[test] Add tests for proxy.deriveAPIFromServerURL and proxy.DeriveGitHubAPIURL#3274

Merged
lpcox merged 1 commit intomainfrom
add-tests-derive-github-api-url-6d400d5935fa1c44
Apr 6, 2026
Merged

[test] Add tests for proxy.deriveAPIFromServerURL and proxy.DeriveGitHubAPIURL#3274
lpcox merged 1 commit intomainfrom
add-tests-derive-github-api-url-6d400d5935fa1c44

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 6, 2026

Test Coverage Improvement: deriveAPIFromServerURL / DeriveGitHubAPIURL

Function Analyzed

  • Package: internal/proxy
  • File: internal/proxy/proxy.go
  • Functions: deriveAPIFromServerURL (unexported), DeriveGitHubAPIURL (exported)
  • Complexity: Medium – URL parsing with a 3-branch switch (github.com alias, GHEC .ghe.com, GHES fallback), hostname normalisation, scheme preservation, and port handling

Why These Functions?

Static analysis showed that although TestDeriveAPIFromServerURL and TestDeriveGitHubAPIURL exist in proxy_test.go, several real branches were left untested:

  • The www.github.com alias is explicitly in the switch condition alongside github.com but was never exercised by any test
  • Uppercase hostnames (normalised via strings.ToLower) were not tested
  • Non-HTTPS (http) schemes for both GHEC and GHES were not tested (scheme is preserved in the output)
  • parsed.Host == "" edge cases ((redacted) (redacted) were not tested
  • ghe.com itself (without leading dot) was not tested — it falls to the GHES branch, not GHEC
  • Deeply-nested *.ghe.com subdomains were not tested
  • DeriveGitHubAPIURL had no test for when GITHUB_SERVER_URL is an unusable URL, propagating ""

Tests Added

New file: internal/proxy/derive_api_url_test.go

TestDeriveAPIFromServerURL_ExtendedCoverage (12 cases)

  • www.github.com returns DefaultGitHubAPIBase
  • www.github.com/ (trailing slash) returns DefaultGitHubAPIBase
  • GITHUB.COM (uppercase) normalised to DefaultGitHubAPIBase
  • https://github.com/org/repo (path component) returns DefaultGitHubAPIBase
  • (mycompany.ghe.com/redacted) (HTTP GHEC) preserves (redacted) scheme
  • ✅ Deeply-nested (deep.nested.tenant.ghe.com/redacted)copilot-api. prefix
  • (mycompany.ghe.com/redacted) (uppercase GHEC) normalised correctly
  • (ghe.com/redacted) (bare, no dot) treated as GHES → /api/v3
  • (github.mycompany.com/redacted) (HTTP GHES) preserves (redacted) scheme
  • ✅ `(github.example.com/redacted) (GHES with path) path stripped
  • (redacted) (empty host) returns ""`
  • (redacted) (no network host) returns ""`

TestDeriveGitHubAPIURL_ExtendedCoverage (4 cases)

  • GITHUB_SERVER_URL=not-a-valid-url"" propagated
  • GITHUB_SERVER_URL=(redacted) (empty host) → ""` propagated
  • GITHUB_API_URL="" explicitly set falls through to GITHUB_SERVER_URL
  • GITHUB_SERVER_URL=https://www.github.comDefaultGitHubAPIBase

Test Methodology

All expectations were validated by running an equivalent Go function locally (to confirm URL parsing behaviour) before committing:

OK: "https://www.github.com"                 → "https://api.github.com"
OK: "(mycompany.ghe.com/redacted)               → "(copilotapi.mycompany.ghe.com/redacted)
OK: "(mycompany.ghe.com/redacted)"              → "(copilotapi.mycompany.ghe.com/redacted)"
OK: "(ghe.com/redacted)"                        → "(ghe.com/redacted)
OK: "(redacted)                                → ""
OK: "(redacted)                      → ""
... (all 13 cases passed)

Test Execution

Tests follow existing project conventions:

  • package proxy (internal, accessing unexported deriveAPIFromServerURL)
  • Table-driven with t.Run()
  • testify/assert with t.Cleanup() for env var restoration

Generated by Test Coverage Improver

Generated by Test Coverage Improver · ● 13.7M ·

…ubAPIURL

Add internal/proxy/derive_api_url_test.go with two new test functions that
cover branches left untested by the existing TestDeriveAPIFromServerURL and
TestDeriveGitHubAPIURL tests:

TestDeriveAPIFromServerURL_ExtendedCoverage:
- www.github.com (alternate alias handled in the same switch case)
- GITHUB.COM (uppercase hostname normalised by strings.ToLower)
- github.com URL with path component (path is stripped via Hostname())
- http:// GHEC tenant (non-HTTPS scheme preserved in output)
- deeply nested GHEC subdomain (*.ghe.com multi-level)
- uppercase .GHE.COM (normalised before HasSuffix check)
- bare ghe.com treated as GHES (HasSuffix requires a leading dot)
- http:// GHES server (non-HTTPS scheme preserved)
- GHES URL with path (path ignored, only Host used)
- http:// with empty host (parsed.Host ==  → return )
- file:///some/path (no network host → return )

TestDeriveGitHubAPIURL_ExtendedCoverage:
- GITHUB_SERVER_URL set to an unparseable URL → propagates
- GITHUB_SERVER_URL set to http:// (empty host) → propagates
- GITHUB_API_URL explicitly set to "" falls through to GITHUB_SERVER_URL
- GITHUB_SERVER_URL=https://www.github.com derives DefaultGitHubAPIBase

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 6, 2026 14:42
Copilot AI review requested due to automatic review settings April 6, 2026 14:42
@lpcox lpcox merged commit 9b9bf77 into main Apr 6, 2026
3 checks passed
@lpcox lpcox deleted the add-tests-derive-github-api-url-6d400d5935fa1c44 branch April 6, 2026 14:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds additional table-driven tests in internal/proxy to improve coverage of URL-derivation logic used by the proxy when resolving the upstream GitHub API base URL from environment variables.

Changes:

  • Add extended-coverage tests for deriveAPIFromServerURL (www alias, case normalization, scheme preservation, path stripping, and empty-host edge cases).
  • Add extended-coverage tests for DeriveGitHubAPIURL to validate env var fallthrough and propagation of unusable GITHUB_SERVER_URL values.
Show a summary per file
File Description
internal/proxy/derive_api_url_test.go New tests covering previously untested branches/edge cases for deriving GitHub API base URLs.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

// - GITHUB_SERVER_URL set to an invalid URL (deriveAPIFromServerURL returns "") → function returns ""
// - GITHUB_SERVER_URL set to a URL with empty host → function returns ""
// - GITHUB_API_URL set to empty string explicitly → falls through to GITHUB_SERVER_URL
// - Neither var set (already tested) just to confirm baseline
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment lists "Neither var set (already tested) just to confirm baseline", but this test function doesn't include a case where both env vars are unset. Either add that case here or remove/update the bullet to avoid misleading future readers.

Suggested change
// - Neither var set (already tested) just to confirm baseline

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants