Skip to content

fix(config): trim trailing slash from base_url in config layer#68

Open
azbh111 wants to merge 1 commit intoriba2534:mainfrom
azbh111:fix/config-base-url-trailing-slash
Open

fix(config): trim trailing slash from base_url in config layer#68
azbh111 wants to merge 1 commit intoriba2534:mainfrom
azbh111:fix/config-base-url-trailing-slash

Conversation

@azbh111
Copy link
Copy Markdown

@azbh111 azbh111 commented Mar 29, 2026

Summary

  • config.Init() 中对 BaseURL 统一执行 strings.TrimRight(url, "/"),从源头消除尾部斜杠
  • 新增 2 个测试函数覆盖环境变量和配置文件两种来源

Background

私有化部署用户在 config.yaml 中配置 base_url 时,尾部可能带斜杠(如 https://private-deploy.example.com/)。飞书 SDK 和项目中多处手动拼接路径时会产生双斜杠(https://host//open-apis/...),部分私有化部署服务器对双斜杠返回 404,导致所有 API 调用失败。

此 PR 是 #66 的改进方案。#66 仅在 client.goGetClient() 做 TrimRight,但 message.go(2 处)和 oauth.go(2 处)中手动拼接 URL 的代码未覆盖。维护者建议在 config 层统一处理。

新方案将 TrimRight 移至 config.Init() —— 所有配置的唯一入口点,无论配置来源(环境变量、配置文件、默认值),BaseURL 都在此处完成清理,所有下游消费方自动受益,无需逐个修补。

Changes

文件 改动说明
internal/config/config.go Init()Unmarshal 后新增 strings.TrimRight(cfg.BaseURL, "/")
internal/config/config_test.go 新增 TestInit_BaseURLTrimsTrailingSlash(表驱动,3 子测试)和 TestInit_BaseURLTrimsTrailingSlashFromFile

Test Plan

常规检查

  • go build ./... 编译通过
  • go vet ./... 静态检查通过
  • go clean -testcache && go test ./... 全部测试通过

新增用例

  • TestInit_BaseURLTrimsTrailingSlash:表驱动覆盖单斜杠 /、多斜杠 ///、无斜杠三种输入(环境变量来源)
  • TestInit_BaseURLTrimsTrailingSlashFromFile:配置文件来源的尾部斜杠处理

Normalize BaseURL by removing trailing slashes in config.Init(),
so all downstream consumers (SDK client, raw HTTP requests in
message.go, oauth.go, etc.) automatically get a clean URL.

This prevents double-slash in API paths (e.g. https://host//open-apis/...)
which causes 404 on some private deployments.
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.

1 participant