Skip to content

Commit 5369d6c

Browse files
committed
test: set request host to localhost in various test files
1 parent 3208aa5 commit 5369d6c

6 files changed

Lines changed: 31 additions & 6 deletions

File tree

.github/workflows/test-coraza.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
strategy:
2323
matrix:
2424
go-version:
25-
- 1.25.x
2625
- 1.26.x
2726
steps:
2827
- name: Fetch Repository

v3/casbin/casbin_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func Test_RequiresPermission(t *testing.T) {
211211
if err != nil {
212212
t.Fatal(err)
213213
}
214+
req.Host = "localhost"
214215

215216
resp, err := app.Test(req)
216217
if err != nil {
@@ -322,6 +323,7 @@ func Test_RequiresRoles(t *testing.T) {
322323
if err != nil {
323324
t.Fatal(err)
324325
}
326+
req.Host = "localhost"
325327

326328
resp, err := app.Test(req)
327329
if err != nil {
@@ -430,6 +432,7 @@ func Test_RoutePermission(t *testing.T) {
430432
if err != nil {
431433
t.Fatal(err)
432434
}
435+
req.Host = "localhost"
433436

434437
req.Header.Set("x-subject", tC.subject)
435438
resp, err := app.Test(req)

v3/i18n/embed_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var embedApp = newEmbedServer()
4545
func request(lang language.Tag, name string) (*http.Response, error) {
4646
path := "/" + name
4747
req, _ := http.NewRequestWithContext(context.Background(), "GET", path, nil)
48+
req.Host = "localhost"
4849
req.Header.Add("Accept-Language", lang.String())
4950
req.Method = "GET"
5051
req.RequestURI = path

v3/i18n/i18n_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
func makeRequest(lang language.Tag, name string, app *fiber.App) (*http.Response, error) {
4040
path := "/" + name
4141
req, _ := http.NewRequestWithContext(context.Background(), "GET", path, nil)
42+
req.Host = "localhost"
4243
if lang != language.Und {
4344
req.Header.Add("Accept-Language", lang.String())
4445
}

v3/newrelic/fiber_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestNewRelicAppConfig(t *testing.T) {
6363
})
6464

6565
r := httptest.NewRequest(http.MethodGet, "/", nil)
66+
r.Host = "localhost"
6667
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
6768
assert.Equal(t, http.StatusOK, resp.StatusCode)
6869
})
@@ -92,6 +93,7 @@ func TestNewRelicAppConfig(t *testing.T) {
9293
})
9394

9495
r := httptest.NewRequest("GET", "/", nil)
96+
r.Host = "localhost"
9597
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
9698
assert.Equal(t, 200, resp.StatusCode)
9799
})
@@ -112,6 +114,7 @@ func TestNewRelicAppConfig(t *testing.T) {
112114
})
113115

114116
r := httptest.NewRequest("GET", "/invalid-url", nil)
117+
r.Host = "localhost"
115118
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
116119
assert.Equal(t, 404, resp.StatusCode)
117120
})
@@ -132,6 +135,7 @@ func TestNewRelicAppConfig(t *testing.T) {
132135
})
133136

134137
r := httptest.NewRequest("GET", "/", nil)
138+
r.Host = "localhost"
135139
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
136140
assert.Equal(t, 200, resp.StatusCode)
137141
})
@@ -152,6 +156,7 @@ func TestNewRelicAppConfig(t *testing.T) {
152156
})
153157

154158
r := httptest.NewRequest("GET", "/", nil)
159+
r.Host = "localhost"
155160
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
156161
assert.Equal(t, 200, resp.StatusCode)
157162
})
@@ -172,6 +177,7 @@ func TestNewRelicAppConfig(t *testing.T) {
172177
})
173178

174179
r := httptest.NewRequest("GET", "/", nil)
180+
r.Host = "localhost"
175181
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
176182
assert.Equal(t, 200, resp.StatusCode)
177183
})
@@ -199,6 +205,7 @@ func TestNewRelicAppConfig(t *testing.T) {
199205
assert.NotNil(t, newrelicApp)
200206

201207
r := httptest.NewRequest("GET", "/", nil)
208+
r.Host = "localhost"
202209
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
203210
assert.Equal(t, 200, resp.StatusCode)
204211
})
@@ -241,7 +248,9 @@ func TestNewRelicAppConfig(t *testing.T) {
241248
app.Get("/", func(ctx fiber.Ctx) error { return errors.New("system error") })
242249

243250
// when
244-
resp, err := app.Test(httptest.NewRequest("GET", "/", nil), fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
251+
r := httptest.NewRequest("GET", "/", nil)
252+
r.Host = "localhost"
253+
resp, err := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
245254
assert.NoError(t, err)
246255
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
247256
})
@@ -268,7 +277,9 @@ func TestNewRelicAppConfig(t *testing.T) {
268277
app.Get("/", func(ctx fiber.Ctx) error { return errors.New("system error") })
269278

270279
// when
271-
resp, err := app.Test(httptest.NewRequest("GET", "/", nil), fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
280+
r := httptest.NewRequest("GET", "/", nil)
281+
r.Host = "localhost"
282+
resp, err := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
272283
assert.NoError(t, err)
273284
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
274285
assert.True(t, errorStatusCodeHandlerCalled)
@@ -302,6 +313,7 @@ func TestNewRelicAppConfig(t *testing.T) {
302313
})
303314

304315
r := httptest.NewRequest("GET", "/jump", nil)
316+
r.Host = "localhost"
305317
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
306318
assert.Equal(t, 200, resp.StatusCode)
307319
})
@@ -334,6 +346,7 @@ func TestNewRelicAppConfig(t *testing.T) {
334346
})
335347

336348
r := httptest.NewRequest("GET", "/", nil)
349+
r.Host = "localhost"
337350
resp, _ := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
338351
assert.Equal(t, 200, resp.StatusCode)
339352
})
@@ -370,7 +383,9 @@ func TestDefaultErrorStatusCodeHandler(t *testing.T) {
370383
})
371384

372385
// when
373-
resp, err := app.Test(httptest.NewRequest("GET", "/", nil), fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
386+
r := httptest.NewRequest("GET", "/", nil)
387+
r.Host = "localhost"
388+
resp, err := app.Test(r, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
374389
assert.NoError(t, err)
375390
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
376391
})
@@ -398,6 +413,7 @@ func TestFromContext(t *testing.T) {
398413
})
399414

400415
req := httptest.NewRequest(http.MethodGet, "/foo", http.NoBody)
416+
req.Host = "localhost"
401417

402418
// when
403419
res, err := app.Test(req, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
@@ -417,7 +433,8 @@ func TestCreateWebRequest(t *testing.T) {
417433
return ctx.SendStatus(http.StatusNoContent)
418434
})
419435

420-
r := httptest.NewRequest(http.MethodGet, "http://example.com/", nil)
436+
r := httptest.NewRequest(http.MethodGet, "/", nil)
437+
r.Host = "example.com"
421438
r.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
422439
r.Header.Add("X-Custom", "abc")
423440
r.Header.Add("X-Custom", "def")
@@ -438,7 +455,8 @@ func TestCreateWebRequest(t *testing.T) {
438455
return ctx.SendStatus(http.StatusNoContent)
439456
})
440457

441-
r := httptest.NewRequest(http.MethodGet, "http://example.com/", nil)
458+
r := httptest.NewRequest(http.MethodGet, "/", nil)
459+
r.Host = "example.com"
442460
r.Header.Set("traceparent", "trace-value")
443461
r.Header.Set("Authorization", "Bearer secret")
444462

@@ -464,6 +482,7 @@ func TestFromContext_PassLocalsToContext(t *testing.T) {
464482
})
465483

466484
req := httptest.NewRequest(http.MethodGet, "/foo", http.NoBody)
485+
req.Host = "localhost"
467486
res, err := app.Test(req, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
468487
assert.NoError(t, err)
469488
assert.Equal(t, http.StatusOK, res.StatusCode)

v3/swaggo/swagger_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func Test_Swagger(t *testing.T) {
9393
if err != nil {
9494
t.Fatal(err)
9595
}
96+
req.Host = "localhost"
9697

9798
resp, err := app.Test(req)
9899
if err != nil {
@@ -143,6 +144,7 @@ func Test_Swagger_Proxy_Redirect(t *testing.T) {
143144
t.Fatal(err)
144145
}
145146

147+
req.Host = "localhost"
146148
req.Header.Set("X-Forwarded-Prefix", "/custom/path/")
147149

148150
resp, err := app.Test(req)

0 commit comments

Comments
 (0)