Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
426d691c
Commit
426d691c
authored
Jan 26, 2026
by
shaw
Browse files
fix(urlvalidator): 移除 ValidateURLFormat 返回值的末尾斜杠
修复 API Key 账号 base_url 末尾带斜杠时导致的双斜杠问题
parent
e9a4c8ab
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/internal/util/urlvalidator/validator.go
View file @
426d691c
...
@@ -46,7 +46,7 @@ func ValidateURLFormat(raw string, allowInsecureHTTP bool) (string, error) {
...
@@ -46,7 +46,7 @@ func ValidateURLFormat(raw string, allowInsecureHTTP bool) (string, error) {
}
}
}
}
return
trimmed
,
nil
return
strings
.
TrimRight
(
trimmed
,
"/"
),
nil
}
}
func
ValidateHTTPSURL
(
raw
string
,
opts
ValidationOptions
)
(
string
,
error
)
{
func
ValidateHTTPSURL
(
raw
string
,
opts
ValidationOptions
)
(
string
,
error
)
{
...
...
backend/internal/util/urlvalidator/validator_test.go
View file @
426d691c
...
@@ -21,4 +21,31 @@ func TestValidateURLFormat(t *testing.T) {
...
@@ -21,4 +21,31 @@ func TestValidateURLFormat(t *testing.T) {
if
_
,
err
:=
ValidateURLFormat
(
"https://example.com:bad"
,
true
);
err
==
nil
{
if
_
,
err
:=
ValidateURLFormat
(
"https://example.com:bad"
,
true
);
err
==
nil
{
t
.
Fatalf
(
"expected invalid port to fail"
)
t
.
Fatalf
(
"expected invalid port to fail"
)
}
}
// 验证末尾斜杠被移除
normalized
,
err
:=
ValidateURLFormat
(
"https://example.com/"
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"expected trailing slash url to pass, got %v"
,
err
)
}
if
normalized
!=
"https://example.com"
{
t
.
Fatalf
(
"expected trailing slash to be removed, got %s"
,
normalized
)
}
// 验证多个末尾斜杠被移除
normalized
,
err
=
ValidateURLFormat
(
"https://example.com///"
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"expected multiple trailing slashes to pass, got %v"
,
err
)
}
if
normalized
!=
"https://example.com"
{
t
.
Fatalf
(
"expected all trailing slashes to be removed, got %s"
,
normalized
)
}
// 验证带路径的 URL 末尾斜杠被移除
normalized
,
err
=
ValidateURLFormat
(
"https://example.com/api/v1/"
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"expected trailing slash url with path to pass, got %v"
,
err
)
}
if
normalized
!=
"https://example.com/api/v1"
{
t
.
Fatalf
(
"expected trailing slash to be removed from path, got %s"
,
normalized
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment