@@ -55,6 +55,31 @@ func createProxyTestServerIPv6(t *testing.T, handler fiber.Handler) (*fiber.App,
55
55
return createProxyTestServer (t , handler , fiber .NetworkTCP6 , "[::1]:0" )
56
56
}
57
57
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
+
58
83
// go test -run Test_Proxy_Empty_Host
59
84
func Test_Proxy_Empty_Upstream_Servers (t * testing.T ) {
60
85
t .Parallel ()
@@ -501,9 +526,14 @@ func Test_Proxy_Do_RestoreOriginalURL(t *testing.T) {
501
526
// go test -race -run Test_Proxy_Do_WithRealURL
502
527
func Test_Proxy_Do_WithRealURL (t * testing.T ) {
503
528
t .Parallel ()
529
+
530
+ _ , addr := createProxyTestServerIPv4 (t , func (c fiber.Ctx ) error {
531
+ return c .SendString ("real url" )
532
+ })
533
+
504
534
app := fiber .New ()
505
535
app .Get ("/test" , func (c fiber.Ctx ) error {
506
- return Do (c , "https ://www.google.com" )
536
+ return Do (c , "http ://" + addr )
507
537
})
508
538
509
539
resp , err1 := app .Test (httptest .NewRequest (fiber .MethodGet , "/test" , nil ), fiber.TestConfig {
@@ -515,15 +545,17 @@ func Test_Proxy_Do_WithRealURL(t *testing.T) {
515
545
require .Equal (t , "/test" , resp .Request .URL .String ())
516
546
body , err := io .ReadAll (resp .Body )
517
547
require .NoError (t , err )
518
- require .Contains (t , string (body ), "https://www.google.com/" )
548
+ require .Equal (t , "real url" , string (body ))
519
549
}
520
550
521
551
// go test -race -run Test_Proxy_Do_WithRedirect
522
552
func Test_Proxy_Do_WithRedirect (t * testing.T ) {
523
553
t .Parallel ()
554
+
555
+ addr := createRedirectServer (t )
524
556
app := fiber .New ()
525
557
app .Get ("/test" , func (c fiber.Ctx ) error {
526
- return Do (c , "https ://google.com" )
558
+ return Do (c , "http ://" + addr )
527
559
})
528
560
529
561
resp , err1 := app .Test (httptest .NewRequest (fiber .MethodGet , "/test" , nil ), fiber.TestConfig {
@@ -533,35 +565,40 @@ func Test_Proxy_Do_WithRedirect(t *testing.T) {
533
565
require .NoError (t , err1 )
534
566
body , err := io .ReadAll (resp .Body )
535
567
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 )
538
570
}
539
571
540
572
// go test -race -run Test_Proxy_DoRedirects_RestoreOriginalURL
541
573
func Test_Proxy_DoRedirects_RestoreOriginalURL (t * testing.T ) {
542
574
t .Parallel ()
575
+
576
+ addr := createRedirectServer (t )
543
577
app := fiber .New ()
544
578
app .Get ("/test" , func (c fiber.Ctx ) error {
545
- return DoRedirects (c , "http://google.com" , 1 )
579
+ return DoRedirects (c , "http://" + addr , 1 )
546
580
})
547
581
548
582
resp , err1 := app .Test (httptest .NewRequest (fiber .MethodGet , "/test" , nil ), fiber.TestConfig {
549
583
Timeout : 2 * time .Second ,
550
584
FailOnTimeout : true ,
551
585
})
552
586
require .NoError (t , err1 )
553
- _ , err := io .ReadAll (resp .Body )
587
+ body , err := io .ReadAll (resp .Body )
554
588
require .NoError (t , err )
589
+ require .Equal (t , "final" , string (body ))
555
590
require .Equal (t , fiber .StatusOK , resp .StatusCode )
556
591
require .Equal (t , "/test" , resp .Request .URL .String ())
557
592
}
558
593
559
594
// go test -race -run Test_Proxy_DoRedirects_TooManyRedirects
560
595
func Test_Proxy_DoRedirects_TooManyRedirects (t * testing.T ) {
561
596
t .Parallel ()
597
+
598
+ addr := createRedirectServer (t )
562
599
app := fiber .New ()
563
600
app .Get ("/test" , func (c fiber.Ctx ) error {
564
- return DoRedirects (c , "http://google.com" , 0 )
601
+ return DoRedirects (c , "http://" + addr , 0 )
565
602
})
566
603
567
604
resp , err1 := app .Test (httptest .NewRequest (fiber .MethodGet , "/test" , nil ), fiber.TestConfig {
0 commit comments