Skip to content

Commit c527ee0

Browse files
committed
feat: enhance URL scheme functions to replace existing schemes with HTTP/HTTPS
1 parent 7fc2455 commit c527ee0

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

urlutil/urlutil.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,34 @@ func HasScheme(rawURL string) bool {
9898
return re.MatchString(rawURL)
9999
}
100100

101-
// EnsureHTTP ensures a URL has an HTTP scheme
101+
// EnsureHTTP ensures that the URL has an HTTP scheme, replacing any existing scheme.
102102
func EnsureHTTP(rawURL string) string {
103103
if !HasScheme(rawURL) {
104-
rawURL = "http://" + rawURL
104+
return "http://" + rawURL
105105
}
106-
return rawURL
106+
107+
u, err := url.Parse(rawURL)
108+
if err != nil {
109+
return "http://" + rawURL
110+
}
111+
112+
u.Scheme = "http"
113+
return u.String()
107114
}
108115

109-
// EnsureHTTPS ensures a URL has an HTTPS scheme
116+
// EnsureHTTPS ensures that the URL has an HTTPS scheme, replacing any existing scheme.
110117
func EnsureHTTPS(rawURL string) string {
111118
if !HasScheme(rawURL) {
112-
rawURL = "https://" + rawURL
119+
return "https://" + rawURL
113120
}
114-
return rawURL
121+
122+
u, err := url.Parse(rawURL)
123+
if err != nil {
124+
return "https://" + rawURL
125+
}
126+
127+
u.Scheme = "https"
128+
return u.String()
115129
}
116130

117131
// HasFileExtension checks if the given rawURL string has a file extension in its path

urlutil/urlutil_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,14 @@ func TestEnsureHTTP(t *testing.T) {
250250
}{
251251
{"example.com", "http://example.com"},
252252
{"http://example.com", "http://example.com"},
253-
{"https://example.com", "https://example.com"},
253+
{"https://example.com", "http://example.com"},
254254
{"example.com/path", "http://example.com/path"},
255255
{"localhost", "http://localhost"},
256256
{"http://localhost", "http://localhost"},
257-
{"https://localhost", "https://localhost"},
257+
{"https://localhost", "http://localhost"},
258258
{"192.168.0.1", "http://192.168.0.1"},
259-
{"http://192.168.0.1", "http://192.168.0.1"},
259+
{"https://192.168.0.1", "http://192.168.0.1"},
260+
{"ftp://example.com", "http://example.com"},
260261
}
261262

262263
for _, tt := range tests {
@@ -275,13 +276,14 @@ func TestEnsureHTTPS(t *testing.T) {
275276
expected string
276277
}{
277278
{"example.com", "https://example.com"},
278-
{"http://example.com", "http://example.com"},
279+
{"http://example.com", "https://example.com"},
279280
{"https://example.com", "https://example.com"},
280281
{"example.com/path", "https://example.com/path"},
281282
{"localhost", "https://localhost"},
282-
{"https://localhost", "https://localhost"},
283+
{"http://localhost", "https://localhost"},
283284
{"192.168.0.1", "https://192.168.0.1"},
284-
{"https://192.168.0.1", "https://192.168.0.1"},
285+
{"http://192.168.0.1", "https://192.168.0.1"},
286+
{"ftp://example.com", "https://example.com"},
285287
}
286288

287289
for _, tt := range tests {

0 commit comments

Comments
 (0)