Skip to content

Apply trailing slash sanitization to API_BASE_URL#5

Merged
tayyebi merged 4 commits intomainfrom
copilot/sanitize-api-base-url
Jan 7, 2026
Merged

Apply trailing slash sanitization to API_BASE_URL#5
tayyebi merged 4 commits intomainfrom
copilot/sanitize-api-base-url

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 7, 2026

API_BASE_URL lacks the trailing slash normalization applied to PUBLIC_BASE_URL, causing double slashes in constructed URLs when users provide trailing slashes.

Changes

  • Applied sanitize_base_url() to get_api_base_url() - single line change ensures consistent URL normalization
  • Added 9 unit tests - covers sanitization behavior (trailing slashes, whitespace, empty values) and environment variable handling

Example

// Before: double slash when API_BASE_URL has trailing slash
API_BASE_URL="https://api.cloudzy.com/developers/"
format!("{}{}", api_base_url, "/v1/instances")
// → https://api.cloudzy.com/developers//v1/instances

// After: normalized to single slash
get_api_base_url() // → "https://api.cloudzy.com/developers"
format!("{}{}", api_base_url, "/v1/instances")
// → https://api.cloudzy.com/developers/v1/instances

Maintains backward compatibility - URLs without trailing slashes behave identically.

Original prompt

Problem

Currently, only PUBLIC_BASE_URL has trailing slash sanitization applied via the sanitize_base_url() helper function. The API_BASE_URL does not undergo the same normalization, which can lead to inconsistent behavior when users provide URLs with trailing slashes.

Since all API endpoints start with / (e.g., /v1/instances, /v1/products), and the URL construction in src/api/client.rs uses simple concatenation:

let url = format!("{}{}", api_base_url, endpoint);

A trailing slash in API_BASE_URL would result in double slashes like:
https://api.cloudzy.com/developers//v1/instances

Solution

Apply the existing sanitize_base_url() function to API_BASE_URL in addition to PUBLIC_BASE_URL to ensure consistent handling of trailing slashes.

Changes Required

In src/config.rs, update the get_api_base_url() function on line 26:

Before:

pub fn get_api_base_url() -> String {
    env::var("API_BASE_URL").unwrap_or_else(|_| DEFAULT_API_BASE_URL.to_string())
}

After:

pub fn get_api_base_url() -> String {
    sanitize_base_url(&env::var("API_BASE_URL").unwrap_or_else(|_| DEFAULT_API_BASE_URL.to_string()))
}

This change:

  • Prevents double slashes in API URLs
  • Maintains backward compatibility - URLs without trailing slashes work identically
  • Provides consistent behavior with PUBLIC_BASE_URL
  • No breaking changes - existing implementations continue to work

Testing

The fix should be tested with:

  1. API_BASE_URL=https://api.cloudzy.com/developers (no trailing slash)
  2. API_BASE_URL=https://api.cloudzy.com/developers/ (with trailing slash)
  3. API_BASE_URL= (empty, should use default)

All cases should produce valid API URLs without double slashes.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 7, 2026 12:36
Co-authored-by: tayyebi <14053493+tayyebi@users.noreply.github.com>
Co-authored-by: tayyebi <14053493+tayyebi@users.noreply.github.com>
@tayyebi tayyebi marked this pull request as ready for review January 7, 2026 12:40
Copilot AI changed the title [WIP] Apply trailing slash sanitization to API_BASE_URL Apply trailing slash sanitization to API_BASE_URL Jan 7, 2026
Copilot AI requested a review from tayyebi January 7, 2026 12:43
- Updated the GitHub Actions release workflow to run tests before building release binaries.
- Made the `sanitize_base_url` function public and added comprehensive tests for URL sanitization and API base URL retrieval.
- Removed redundant test cases from various modules and organized tests into dedicated files for better maintainability.
@tayyebi tayyebi merged commit 696a374 into main Jan 7, 2026
@tayyebi tayyebi deleted the copilot/sanitize-api-base-url branch January 7, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants