Skip to content

Commit 18ed68b

Browse files
gabyCopilot
andauthored
🧹 chore: Fix proxy middleware tests for offline environments (#3467)
* Fix proxy middleware tests to avoid external network * Update proxy_test.go * Update middleware/proxy/proxy_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update proxy_test.go --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 81edaf0 commit 18ed68b

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

middleware/proxy/proxy_test.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ func createProxyTestServerIPv6(t *testing.T, handler fiber.Handler) (*fiber.App,
5555
return createProxyTestServer(t, handler, fiber.NetworkTCP6, "[::1]:0")
5656
}
5757

58+
func createRedirectServer(t *testing.T) string {
59+
t.Helper()
60+
app := fiber.New()
61+
62+
var addr string
63+
app.Get("/", func(c fiber.Ctx) error {
64+
c.Location("http://" + addr + "/final")
65+
return c.Status(fiber.StatusMovedPermanently).SendString("redirect")
66+
})
67+
app.Get("/final", func(c fiber.Ctx) error {
68+
return c.SendString("final")
69+
})
70+
71+
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
72+
require.NoError(t, err)
73+
t.Cleanup(func() {
74+
ln.Close() //nolint:errcheck // It is fine to ignore the error here
75+
})
76+
addr = ln.Addr().String()
77+
78+
startServer(app, ln)
79+
80+
return addr
81+
}
82+
5883
// go test -run Test_Proxy_Empty_Host
5984
func Test_Proxy_Empty_Upstream_Servers(t *testing.T) {
6085
t.Parallel()
@@ -501,9 +526,14 @@ func Test_Proxy_Do_RestoreOriginalURL(t *testing.T) {
501526
// go test -race -run Test_Proxy_Do_WithRealURL
502527
func Test_Proxy_Do_WithRealURL(t *testing.T) {
503528
t.Parallel()
529+
530+
_, addr := createProxyTestServerIPv4(t, func(c fiber.Ctx) error {
531+
return c.SendString("real url")
532+
})
533+
504534
app := fiber.New()
505535
app.Get("/test", func(c fiber.Ctx) error {
506-
return Do(c, "https://www.google.com")
536+
return Do(c, "http://"+addr)
507537
})
508538

509539
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), fiber.TestConfig{
@@ -515,15 +545,17 @@ func Test_Proxy_Do_WithRealURL(t *testing.T) {
515545
require.Equal(t, "/test", resp.Request.URL.String())
516546
body, err := io.ReadAll(resp.Body)
517547
require.NoError(t, err)
518-
require.Contains(t, string(body), "https://www.google.com/")
548+
require.Equal(t, "real url", string(body))
519549
}
520550

521551
// go test -race -run Test_Proxy_Do_WithRedirect
522552
func Test_Proxy_Do_WithRedirect(t *testing.T) {
523553
t.Parallel()
554+
555+
addr := createRedirectServer(t)
524556
app := fiber.New()
525557
app.Get("/test", func(c fiber.Ctx) error {
526-
return Do(c, "https://google.com")
558+
return Do(c, "http://"+addr)
527559
})
528560

529561
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), fiber.TestConfig{
@@ -533,35 +565,40 @@ func Test_Proxy_Do_WithRedirect(t *testing.T) {
533565
require.NoError(t, err1)
534566
body, err := io.ReadAll(resp.Body)
535567
require.NoError(t, err)
536-
require.Contains(t, string(body), "https://www.google.com/")
537-
require.Equal(t, 301, resp.StatusCode)
568+
require.Equal(t, "redirect", string(body))
569+
require.Equal(t, fiber.StatusMovedPermanently, resp.StatusCode)
538570
}
539571

540572
// go test -race -run Test_Proxy_DoRedirects_RestoreOriginalURL
541573
func Test_Proxy_DoRedirects_RestoreOriginalURL(t *testing.T) {
542574
t.Parallel()
575+
576+
addr := createRedirectServer(t)
543577
app := fiber.New()
544578
app.Get("/test", func(c fiber.Ctx) error {
545-
return DoRedirects(c, "http://google.com", 1)
579+
return DoRedirects(c, "http://"+addr, 1)
546580
})
547581

548582
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), fiber.TestConfig{
549583
Timeout: 2 * time.Second,
550584
FailOnTimeout: true,
551585
})
552586
require.NoError(t, err1)
553-
_, err := io.ReadAll(resp.Body)
587+
body, err := io.ReadAll(resp.Body)
554588
require.NoError(t, err)
589+
require.Equal(t, "final", string(body))
555590
require.Equal(t, fiber.StatusOK, resp.StatusCode)
556591
require.Equal(t, "/test", resp.Request.URL.String())
557592
}
558593

559594
// go test -race -run Test_Proxy_DoRedirects_TooManyRedirects
560595
func Test_Proxy_DoRedirects_TooManyRedirects(t *testing.T) {
561596
t.Parallel()
597+
598+
addr := createRedirectServer(t)
562599
app := fiber.New()
563600
app.Get("/test", func(c fiber.Ctx) error {
564-
return DoRedirects(c, "http://google.com", 0)
601+
return DoRedirects(c, "http://"+addr, 0)
565602
})
566603

567604
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), fiber.TestConfig{

0 commit comments

Comments
 (0)