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
6028efd2
Commit
6028efd2
authored
Mar 18, 2026
by
wucm667
Browse files
test: 添加 injectSiteTitle 函数的单元测试
parent
94419f43
Changes
1
Hide whitespace changes
Inline
Side-by-side
backend/internal/web/embed_test.go
View file @
6028efd2
...
@@ -20,6 +20,78 @@ func init() {
...
@@ -20,6 +20,78 @@ func init() {
gin
.
SetMode
(
gin
.
TestMode
)
gin
.
SetMode
(
gin
.
TestMode
)
}
}
func
TestInjectSiteTitle
(
t
*
testing
.
T
)
{
t
.
Run
(
"replaces_title_with_site_name"
,
func
(
t
*
testing
.
T
)
{
html
:=
[]
byte
(
`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`
)
settingsJSON
:=
[]
byte
(
`{"site_name":"MyCustomSite"}`
)
result
:=
injectSiteTitle
(
html
,
settingsJSON
)
assert
.
Contains
(
t
,
string
(
result
),
"<title>MyCustomSite - AI API Gateway</title>"
)
assert
.
NotContains
(
t
,
string
(
result
),
"Sub2API"
)
})
t
.
Run
(
"returns_unchanged_when_site_name_empty"
,
func
(
t
*
testing
.
T
)
{
html
:=
[]
byte
(
`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`
)
settingsJSON
:=
[]
byte
(
`{"site_name":""}`
)
result
:=
injectSiteTitle
(
html
,
settingsJSON
)
assert
.
Equal
(
t
,
string
(
html
),
string
(
result
))
})
t
.
Run
(
"returns_unchanged_when_site_name_missing"
,
func
(
t
*
testing
.
T
)
{
html
:=
[]
byte
(
`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`
)
settingsJSON
:=
[]
byte
(
`{"other_field":"value"}`
)
result
:=
injectSiteTitle
(
html
,
settingsJSON
)
assert
.
Equal
(
t
,
string
(
html
),
string
(
result
))
})
t
.
Run
(
"returns_unchanged_when_invalid_json"
,
func
(
t
*
testing
.
T
)
{
html
:=
[]
byte
(
`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`
)
settingsJSON
:=
[]
byte
(
`{invalid json}`
)
result
:=
injectSiteTitle
(
html
,
settingsJSON
)
assert
.
Equal
(
t
,
string
(
html
),
string
(
result
))
})
t
.
Run
(
"returns_unchanged_when_no_title_tag"
,
func
(
t
*
testing
.
T
)
{
html
:=
[]
byte
(
`<html><head></head><body></body></html>`
)
settingsJSON
:=
[]
byte
(
`{"site_name":"MyCustomSite"}`
)
result
:=
injectSiteTitle
(
html
,
settingsJSON
)
assert
.
Equal
(
t
,
string
(
html
),
string
(
result
))
})
t
.
Run
(
"returns_unchanged_when_title_has_attributes"
,
func
(
t
*
testing
.
T
)
{
// The function looks for "<title>" literally, so attributes are not supported
// This is acceptable since index.html uses plain <title> without attributes
html
:=
[]
byte
(
`<html><head><title lang="en">Sub2API</title></head><body></body></html>`
)
settingsJSON
:=
[]
byte
(
`{"site_name":"NewSite"}`
)
result
:=
injectSiteTitle
(
html
,
settingsJSON
)
// Should return unchanged since <title> with attributes is not matched
assert
.
Equal
(
t
,
string
(
html
),
string
(
result
))
})
t
.
Run
(
"preserves_rest_of_html"
,
func
(
t
*
testing
.
T
)
{
html
:=
[]
byte
(
`<html><head><meta charset="UTF-8"><title>Sub2API</title><script src="app.js"></script></head><body><div id="app"></div></body></html>`
)
settingsJSON
:=
[]
byte
(
`{"site_name":"TestSite"}`
)
result
:=
injectSiteTitle
(
html
,
settingsJSON
)
assert
.
Contains
(
t
,
string
(
result
),
`<meta charset="UTF-8">`
)
assert
.
Contains
(
t
,
string
(
result
),
`<script src="app.js"></script>`
)
assert
.
Contains
(
t
,
string
(
result
),
`<div id="app"></div>`
)
assert
.
Contains
(
t
,
string
(
result
),
"<title>TestSite - AI API Gateway</title>"
)
})
}
func
TestReplaceNoncePlaceholder
(
t
*
testing
.
T
)
{
func
TestReplaceNoncePlaceholder
(
t
*
testing
.
T
)
{
t
.
Run
(
"replaces_single_placeholder"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"replaces_single_placeholder"
,
func
(
t
*
testing
.
T
)
{
html
:=
[]
byte
(
`<script nonce="__CSP_NONCE_VALUE__">console.log('test');</script>`
)
html
:=
[]
byte
(
`<script nonce="__CSP_NONCE_VALUE__">console.log('test');</script>`
)
...
...
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