@@ -20,6 +20,78 @@ func init() {
2020 gin .SetMode (gin .TestMode )
2121}
2222
23+ func TestInjectSiteTitle (t * testing.T ) {
24+ t .Run ("replaces_title_with_site_name" , func (t * testing.T ) {
25+ html := []byte (`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>` )
26+ settingsJSON := []byte (`{"site_name":"MyCustomSite"}` )
27+
28+ result := injectSiteTitle (html , settingsJSON )
29+
30+ assert .Contains (t , string (result ), "<title>MyCustomSite - AI API Gateway</title>" )
31+ assert .NotContains (t , string (result ), "Sub2API" )
32+ })
33+
34+ t .Run ("returns_unchanged_when_site_name_empty" , func (t * testing.T ) {
35+ html := []byte (`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>` )
36+ settingsJSON := []byte (`{"site_name":""}` )
37+
38+ result := injectSiteTitle (html , settingsJSON )
39+
40+ assert .Equal (t , string (html ), string (result ))
41+ })
42+
43+ t .Run ("returns_unchanged_when_site_name_missing" , func (t * testing.T ) {
44+ html := []byte (`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>` )
45+ settingsJSON := []byte (`{"other_field":"value"}` )
46+
47+ result := injectSiteTitle (html , settingsJSON )
48+
49+ assert .Equal (t , string (html ), string (result ))
50+ })
51+
52+ t .Run ("returns_unchanged_when_invalid_json" , func (t * testing.T ) {
53+ html := []byte (`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>` )
54+ settingsJSON := []byte (`{invalid json}` )
55+
56+ result := injectSiteTitle (html , settingsJSON )
57+
58+ assert .Equal (t , string (html ), string (result ))
59+ })
60+
61+ t .Run ("returns_unchanged_when_no_title_tag" , func (t * testing.T ) {
62+ html := []byte (`<html><head></head><body></body></html>` )
63+ settingsJSON := []byte (`{"site_name":"MyCustomSite"}` )
64+
65+ result := injectSiteTitle (html , settingsJSON )
66+
67+ assert .Equal (t , string (html ), string (result ))
68+ })
69+
70+ t .Run ("returns_unchanged_when_title_has_attributes" , func (t * testing.T ) {
71+ // The function looks for "<title>" literally, so attributes are not supported
72+ // This is acceptable since index.html uses plain <title> without attributes
73+ html := []byte (`<html><head><title lang="en">Sub2API</title></head><body></body></html>` )
74+ settingsJSON := []byte (`{"site_name":"NewSite"}` )
75+
76+ result := injectSiteTitle (html , settingsJSON )
77+
78+ // Should return unchanged since <title> with attributes is not matched
79+ assert .Equal (t , string (html ), string (result ))
80+ })
81+
82+ t .Run ("preserves_rest_of_html" , func (t * testing.T ) {
83+ html := []byte (`<html><head><meta charset="UTF-8"><title>Sub2API</title><script src="app.js"></script></head><body><div id="app"></div></body></html>` )
84+ settingsJSON := []byte (`{"site_name":"TestSite"}` )
85+
86+ result := injectSiteTitle (html , settingsJSON )
87+
88+ assert .Contains (t , string (result ), `<meta charset="UTF-8">` )
89+ assert .Contains (t , string (result ), `<script src="app.js"></script>` )
90+ assert .Contains (t , string (result ), `<div id="app"></div>` )
91+ assert .Contains (t , string (result ), "<title>TestSite - AI API Gateway</title>" )
92+ })
93+ }
94+
2395func TestReplaceNoncePlaceholder (t * testing.T ) {
2496 t .Run ("replaces_single_placeholder" , func (t * testing.T ) {
2597 html := []byte (`<script nonce="__CSP_NONCE_VALUE__">console.log('test');</script>` )
0 commit comments