Skip to content

Commit eb10ea2

Browse files
committed
test: normalize localhost:<port> references
1 parent 0a3d076 commit eb10ea2

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

__snapshots__/main_test.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,39 +60,39 @@ You must provide at least one path to either a lockfile or a directory containin
6060

6161
[TestRun_APIError/#00 - 1]
6262
Loaded the following OSV databases:
63-
api#http://127.0.0.1:37453 (using batches of 1000)
63+
api#http://<localhost>:<port> (using batches of 1000)
6464
RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
6565

6666
<tempdir>/Gemfile.lock: found 1 package
6767
Using config at <tempdir>/.osv-detector.yml (0 ignores)
68-
Using db api#http://127.0.0.1:37453 (using batches of 1000)
68+
Using db api#http://<localhost>:<port> (using batches of 1000)
6969
Using db RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
7070

7171
no known vulnerabilities found
7272

7373
---
7474

7575
[TestRun_APIError/#00 - 2]
76-
an api error occurred while trying to check the packages listed in <tempdir>/Gemfile.lock: api returned unexpected status (POST http://127.0.0.1:37453/querybatch 400)
76+
an api error occurred while trying to check the packages listed in <tempdir>/Gemfile.lock: api returned unexpected status (POST http://<localhost>:<port>/querybatch 400)
7777

7878
---
7979

8080
[TestRun_APIError/#01 - 1]
8181
Loaded the following OSV databases:
82-
api#http://127.0.0.1:38803 (using batches of 1000)
82+
api#http://<localhost>:<port> (using batches of 1000)
8383
RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
8484

8585
<tempdir>/Gemfile.lock: found 1 package
8686
Using config at <tempdir>/.osv-detector.yml (0 ignores)
87-
Using db api#http://127.0.0.1:38803 (using batches of 1000)
87+
Using db api#http://<localhost>:<port> (using batches of 1000)
8888
Using db RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
8989

9090
no known vulnerabilities found
9191

9292
---
9393

9494
[TestRun_APIError/#01 - 2]
95-
an api error occurred while trying to check the packages listed in <tempdir>/Gemfile.lock: api response could not be parsed as json (POST http://127.0.0.1:38803/querybatch): invalid character '<' looking for beginning of value
95+
an api error occurred while trying to check the packages listed in <tempdir>/Gemfile.lock: api response could not be parsed as json (POST http://<localhost>:<port>/querybatch): invalid character '<' looking for beginning of value
9696

9797
---
9898

main_normalize_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ func normalizeDatabaseStats(t *testing.T, str string) string {
8989
return re.ReplaceAllString(str, "$1 (%% vulnerabilities, including withdrawn - last updated %%)")
9090
}
9191

92+
// normalizeLocalhostPort attempts to replace references to 127.0.0.1:<port>
93+
// with a placeholder, to ensure tests pass when using httptest.Server
94+
func normalizeLocalhostPort(t *testing.T, str string) string {
95+
t.Helper()
96+
97+
re := cachedregexp.MustCompile(`127\.0\.0\.1:\d+`)
98+
99+
return re.ReplaceAllString(str, "<localhost>:<port>")
100+
}
101+
92102
// normalizeErrors attempts to replace error messages on alternative OSs with their
93103
// known linux equivalents, to ensure tests pass across different OSs
94104
func normalizeErrors(t *testing.T, str string) string {
@@ -110,6 +120,7 @@ func normalizeSnapshot(t *testing.T, str string) string {
110120
normalizeTempDirectory,
111121
normalizeUserCacheDirectory,
112122
normalizeDatabaseStats,
123+
normalizeLocalhostPort,
113124
normalizeErrors,
114125
} {
115126
str = normalizer(t, str)

0 commit comments

Comments
 (0)