|
7 | 7 | "os" |
8 | 8 | "runtime" |
9 | 9 | "strings" |
| 10 | + "sync/atomic" |
10 | 11 | "testing" |
11 | 12 |
|
12 | 13 | "github.com/caddyserver/caddy/v2/caddytest" |
@@ -562,3 +563,176 @@ func TestReverseProxyHealthCheckUnixSocketWithoutPort(t *testing.T) { |
562 | 563 |
|
563 | 564 | tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!") |
564 | 565 | } |
| 566 | + |
| 567 | +// TestReverseProxyRetryMatchStatusCode verifies that lb_retry_match with a |
| 568 | +// CEL expression matching on {rp.status_code} causes the request to be |
| 569 | +// retried on the next upstream when the first upstream returns a matching |
| 570 | +// status code |
| 571 | +func TestReverseProxyRetryMatchStatusCode(t *testing.T) { |
| 572 | + // Bad upstream: returns 502 |
| 573 | + badSrv := &http.Server{ |
| 574 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 575 | + w.WriteHeader(http.StatusBadGateway) |
| 576 | + }), |
| 577 | + } |
| 578 | + badLn, err := net.Listen("tcp", "127.0.0.1:0") |
| 579 | + if err != nil { |
| 580 | + t.Fatalf("failed to listen: %v", err) |
| 581 | + } |
| 582 | + go badSrv.Serve(badLn) |
| 583 | + t.Cleanup(func() { badSrv.Close(); badLn.Close() }) |
| 584 | + |
| 585 | + // Good upstream: returns 200 |
| 586 | + goodSrv := &http.Server{ |
| 587 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 588 | + w.Write([]byte("ok")) |
| 589 | + }), |
| 590 | + } |
| 591 | + goodLn, err := net.Listen("tcp", "127.0.0.1:0") |
| 592 | + if err != nil { |
| 593 | + t.Fatalf("failed to listen: %v", err) |
| 594 | + } |
| 595 | + go goodSrv.Serve(goodLn) |
| 596 | + t.Cleanup(func() { goodSrv.Close(); goodLn.Close() }) |
| 597 | + |
| 598 | + tester := caddytest.NewTester(t) |
| 599 | + tester.InitServer(fmt.Sprintf(` |
| 600 | + { |
| 601 | + skip_install_trust |
| 602 | + admin localhost:2999 |
| 603 | + http_port 9080 |
| 604 | + https_port 9443 |
| 605 | + grace_period 1ns |
| 606 | + } |
| 607 | + http://localhost:9080 { |
| 608 | + reverse_proxy %s %s { |
| 609 | + lb_policy round_robin |
| 610 | + lb_retries 1 |
| 611 | + lb_retry_match { |
| 612 | + expression `+"`{rp.status_code} in [502, 503]`"+` |
| 613 | + } |
| 614 | + } |
| 615 | + } |
| 616 | + `, goodLn.Addr().String(), badLn.Addr().String()), "caddyfile") |
| 617 | + |
| 618 | + tester.AssertGetResponse("http://localhost:9080/", 200, "ok") |
| 619 | +} |
| 620 | + |
| 621 | +// TestReverseProxyRetryMatchHeader verifies that lb_retry_match with a CEL |
| 622 | +// expression matching on {rp.header.*} causes the request to be retried when |
| 623 | +// the upstream sets a matching response header |
| 624 | +func TestReverseProxyRetryMatchHeader(t *testing.T) { |
| 625 | + var badHits atomic.Int32 |
| 626 | + |
| 627 | + // Bad upstream: returns 200 but signals retry via header |
| 628 | + badSrv := &http.Server{ |
| 629 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 630 | + badHits.Add(1) |
| 631 | + w.Header().Set("X-Upstream-Retry", "true") |
| 632 | + w.Write([]byte("bad")) |
| 633 | + }), |
| 634 | + } |
| 635 | + badLn, err := net.Listen("tcp", "127.0.0.1:0") |
| 636 | + if err != nil { |
| 637 | + t.Fatalf("failed to listen: %v", err) |
| 638 | + } |
| 639 | + go badSrv.Serve(badLn) |
| 640 | + t.Cleanup(func() { badSrv.Close(); badLn.Close() }) |
| 641 | + |
| 642 | + // Good upstream: returns 200 without retry header |
| 643 | + goodSrv := &http.Server{ |
| 644 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 645 | + w.Write([]byte("good")) |
| 646 | + }), |
| 647 | + } |
| 648 | + goodLn, err := net.Listen("tcp", "127.0.0.1:0") |
| 649 | + if err != nil { |
| 650 | + t.Fatalf("failed to listen: %v", err) |
| 651 | + } |
| 652 | + go goodSrv.Serve(goodLn) |
| 653 | + t.Cleanup(func() { goodSrv.Close(); goodLn.Close() }) |
| 654 | + |
| 655 | + tester := caddytest.NewTester(t) |
| 656 | + tester.InitServer(fmt.Sprintf(` |
| 657 | + { |
| 658 | + skip_install_trust |
| 659 | + admin localhost:2999 |
| 660 | + http_port 9080 |
| 661 | + https_port 9443 |
| 662 | + grace_period 1ns |
| 663 | + } |
| 664 | + http://localhost:9080 { |
| 665 | + reverse_proxy %s %s { |
| 666 | + lb_policy round_robin |
| 667 | + lb_retries 1 |
| 668 | + lb_retry_match { |
| 669 | + expression `+"`{rp.header.X-Upstream-Retry} == \"true\"`"+` |
| 670 | + } |
| 671 | + } |
| 672 | + } |
| 673 | + `, goodLn.Addr().String(), badLn.Addr().String()), "caddyfile") |
| 674 | + |
| 675 | + tester.AssertGetResponse("http://localhost:9080/", 200, "good") |
| 676 | + |
| 677 | + if badHits.Load() != 1 { |
| 678 | + t.Errorf("bad upstream hits: got %d, want 1", badHits.Load()) |
| 679 | + } |
| 680 | +} |
| 681 | + |
| 682 | +// TestReverseProxyRetryMatchCombined verifies that a CEL expression combining |
| 683 | +// request path matching with response status code matching works correctly - |
| 684 | +// only retrying when both conditions are met |
| 685 | +func TestReverseProxyRetryMatchCombined(t *testing.T) { |
| 686 | + // Upstream: returns 502 for all requests |
| 687 | + srv := &http.Server{ |
| 688 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 689 | + w.WriteHeader(http.StatusBadGateway) |
| 690 | + }), |
| 691 | + } |
| 692 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 693 | + if err != nil { |
| 694 | + t.Fatalf("failed to listen: %v", err) |
| 695 | + } |
| 696 | + go srv.Serve(ln) |
| 697 | + t.Cleanup(func() { srv.Close(); ln.Close() }) |
| 698 | + |
| 699 | + // Good upstream |
| 700 | + goodSrv := &http.Server{ |
| 701 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 702 | + w.Write([]byte("ok")) |
| 703 | + }), |
| 704 | + } |
| 705 | + goodLn, err := net.Listen("tcp", "127.0.0.1:0") |
| 706 | + if err != nil { |
| 707 | + t.Fatalf("failed to listen: %v", err) |
| 708 | + } |
| 709 | + go goodSrv.Serve(goodLn) |
| 710 | + t.Cleanup(func() { goodSrv.Close(); goodLn.Close() }) |
| 711 | + |
| 712 | + tester := caddytest.NewTester(t) |
| 713 | + tester.InitServer(fmt.Sprintf(` |
| 714 | + { |
| 715 | + skip_install_trust |
| 716 | + admin localhost:2999 |
| 717 | + http_port 9080 |
| 718 | + https_port 9443 |
| 719 | + grace_period 1ns |
| 720 | + } |
| 721 | + http://localhost:9080 { |
| 722 | + reverse_proxy %s %s { |
| 723 | + lb_policy round_robin |
| 724 | + lb_retries 1 |
| 725 | + lb_retry_match { |
| 726 | + expression `+"`path('/retry*') && {rp.status_code} in [502, 503]`"+` |
| 727 | + } |
| 728 | + } |
| 729 | + } |
| 730 | + `, goodLn.Addr().String(), ln.Addr().String()), "caddyfile") |
| 731 | + |
| 732 | + // /retry path matches the expression - should retry to good upstream |
| 733 | + tester.AssertGetResponse("http://localhost:9080/retry", 200, "ok") |
| 734 | + |
| 735 | + // /other path does NOT match - should return the 502 |
| 736 | + req, _ := http.NewRequest(http.MethodGet, "http://localhost:9080/other", nil) |
| 737 | + tester.AssertResponse(req, 502, "") |
| 738 | +} |
0 commit comments