Skip to content

Commit 732e07e

Browse files
authored
feat(testurl): streamline port handling in URL functions 🌐 (#4)
Introduced constants for HTTP and HTTPS ports to reduce redundancy. Now the URLs sing the same tune sans repetition!
1 parent d8274dd commit 732e07e

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

things/testurl/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/madflojo/testlazy/things/testurl
2+
3+
go 1.24.3

things/testurl/url.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ Package testurl makes URL generation in Go tests a breeze—no more boilerplate
33
url.Parse calls or manual query-string concat. Simply import testurl and pick
44
the helper you need to focus on your test logic, not the URL plumbing.
55
6-
github.com/madflojo/testlazy/things/testurl
7-
8-
What’s inside?
9-
10-
- HTTP vs HTTPS for example.com and localhost.
11-
- Ports, paths, queries, and fragments.
12-
- Intentionally broken URLs for negative testing.
6+
github.com/madflojo/testlazy/things/testurl
137
148
Example usage
159
@@ -30,6 +24,8 @@ import (
3024
const (
3125
ExampleHost = "example.com"
3226
LocalhostHost = "localhost"
27+
HTTPPort = "8080"
28+
HTTPSPort = "8443"
3329
)
3430

3531
// URLHTTPBad returns a *url.URL for "http://bad-url" which is technically valid but "should" not resolve correctly.
@@ -69,12 +65,12 @@ func URLHTTPS() *url.URL {
6965

7066
// URLHTTPWithPort returns a *url.URL for "http://example.com:8080/".
7167
func URLHTTPWithPort() *url.URL {
72-
return MustParse("http://" + ExampleHost + ":8080/")
68+
return MustParse("http://" + ExampleHost + ":" + HTTPPort + "/")
7369
}
7470

7571
// URLHTTPSWithPort returns a *url.URL for "https://example.com:8443/".
7672
func URLHTTPSWithPort() *url.URL {
77-
return MustParse("https://" + ExampleHost + ":8443/")
73+
return MustParse("https://" + ExampleHost + ":" + HTTPSPort + "/")
7874
}
7975

8076
// URLHTTPWithQuery returns a *url.URL for "http://example.com/?query=1".
@@ -129,12 +125,12 @@ func URLHTTPSLocalhost() *url.URL {
129125

130126
// URLHTTPLocalhostWithPort returns a *url.URL for "http://localhost:8080/".
131127
func URLHTTPLocalhostWithPort() *url.URL {
132-
return MustParse("http://" + LocalhostHost + ":8080/")
128+
return MustParse("http://" + LocalhostHost + ":" + HTTPPort + "/")
133129
}
134130

135131
// URLHTTPSLocalhostWithPort returns a *url.URL for "https://localhost:8443/".
136132
func URLHTTPSLocalhostWithPort() *url.URL {
137-
return MustParse("https://" + LocalhostHost + ":8443/")
133+
return MustParse("https://" + LocalhostHost + ":" + HTTPSPort + "/")
138134
}
139135

140136
// MustParse is a helper function that parses a URL string and panics if it fails.

0 commit comments

Comments
 (0)