@@ -408,6 +408,9 @@ func Test_Run_ObtainsCertificatesUsingAcme(t *testing.T) {
408408 assert .NoError (t , err )
409409 assert .Equal (t , http .StatusOK , res .StatusCode )
410410 assert .True (t , strings .Contains (res .TLS .PeerCertificates [0 ].Issuer .CommonName , "Pebble Intermediate CA" ))
411+
412+ signalChan <- os .Interrupt
413+ <- doneChan
411414 return
412415 }
413416
@@ -493,6 +496,94 @@ func Test_Run_ValidateFlag_WorksWithDifferentConfigPaths(t *testing.T) {
493496 assert .NoError (t , err )
494497}
495498
499+ func Test_Run_RedirectsToPrimaryDomain (t * testing.T ) {
500+ upstream := startStaticServer (8701 )
501+ defer upstream .stop (context .Background ())
502+
503+ signalChan := make (chan os.Signal , 1 )
504+ doneChan := make (chan struct {}, 1 )
505+
506+ go func () {
507+ err := runTest (
508+ signalChan ,
509+ "CONFIG" , testdata .Path ("domain-redirect.conf" ),
510+ "PROVIDER" , "selfsigned" ,
511+ "FRONTEND" , "tcp" ,
512+ "HTTP_PORT" , "8702" ,
513+ "HTTPS_PORT" , "8703" ,
514+ )
515+ assert .NoError (t , err )
516+ doneChan <- struct {}{}
517+ }()
518+
519+ time .Sleep (2 * time .Second )
520+
521+ // Test HTTPS redirect from www.example.com to example.com
522+ res , err := proxyGet (8703 , "https://www.example.com/test?param=value" )
523+ assert .NoError (t , err )
524+ assert .Equal (t , http .StatusPermanentRedirect , res .StatusCode )
525+ assert .Equal (t , "https://example.com/test?param=value" , res .Header .Get ("Location" ))
526+
527+ // Test that requests to primary domain (example.com) are not redirected
528+ res , err = proxyGet (8703 , "https://example.com/test" )
529+ assert .NoError (t , err )
530+ assert .Equal (t , http .StatusOK , res .StatusCode )
531+
532+ b , _ := io .ReadAll (res .Body )
533+ defer res .Body .Close ()
534+ assert .Contains (t , string (b ), "This is the upstream on port 8701" )
535+
536+ signalChan <- os .Interrupt
537+ <- doneChan
538+ }
539+
540+ func Test_Run_RedirectsHttpToHttps (t * testing.T ) {
541+ upstream := startStaticServer (8701 )
542+ defer upstream .stop (context .Background ())
543+
544+ signalChan := make (chan os.Signal , 1 )
545+ doneChan := make (chan struct {}, 1 )
546+
547+ go func () {
548+ err := runTest (
549+ signalChan ,
550+ "CONFIG" , testdata .Path ("simple-proxy.conf" ),
551+ "PROVIDER" , "selfsigned" ,
552+ "FRONTEND" , "tcp" ,
553+ "HTTP_PORT" , "8702" ,
554+ "HTTPS_PORT" , "8703" ,
555+ )
556+ assert .NoError (t , err )
557+ doneChan <- struct {}{}
558+ }()
559+
560+ time .Sleep (2 * time .Second )
561+
562+ // Test HTTP to HTTPS redirect
563+ res , err := getClientProxy (8702 ).Get ("http://example.com/test?param=value" )
564+ assert .NoError (t , err )
565+ assert .Equal (t , http .StatusPermanentRedirect , res .StatusCode )
566+ assert .Equal (t , "https://example.com/test?param=value" , res .Header .Get ("Location" ))
567+
568+ // Test HTTP to HTTPS redirect strips port
569+ res , err = getClientProxy (8702 ).Get ("http://example.com:80/test" )
570+ assert .NoError (t , err )
571+ assert .Equal (t , http .StatusPermanentRedirect , res .StatusCode )
572+ assert .Equal (t , "https://example.com/test" , res .Header .Get ("Location" ))
573+
574+ // Test that invalid host header returns 400
575+ req , err := http .NewRequest (http .MethodGet , "http://example.com/test" , nil )
576+ assert .NoError (t , err )
577+ req .Host = "invalid..domain"
578+
579+ res , err = getClientProxy (8702 ).Do (req )
580+ assert .NoError (t , err )
581+ assert .Equal (t , http .StatusBadRequest , res .StatusCode )
582+
583+ signalChan <- os .Interrupt
584+ <- doneChan
585+ }
586+
496587func runTest (signalChan <- chan os.Signal , cfg ... string ) error {
497588 flag .CommandLine .VisitAll (func (f * flag.Flag ) {
498589 if ! strings .HasPrefix (f .Name , "test" ) {
@@ -560,6 +651,9 @@ func getClientProxy(realPort int) *http.Client {
560651 InsecureSkipVerify : true ,
561652 },
562653 },
654+ CheckRedirect : func (req * http.Request , via []* http.Request ) error {
655+ return http .ErrUseLastResponse
656+ },
563657 }
564658}
565659
0 commit comments