|
4 | 4 | "crypto/md5"
|
5 | 5 | "fmt"
|
6 | 6 | "reflect"
|
| 7 | + "regexp" |
7 | 8 | "testing"
|
8 | 9 |
|
9 | 10 | kapi "k8s.io/kubernetes/pkg/api"
|
@@ -670,3 +671,142 @@ func TestAddRouteEdgeTerminationInsecurePolicy(t *testing.T) {
|
670 | 671 | }
|
671 | 672 | }
|
672 | 673 | }
|
| 674 | + |
| 675 | +func TestGenerateRouteRegexp(t *testing.T) { |
| 676 | + tests := []struct { |
| 677 | + name string |
| 678 | + hostname string |
| 679 | + path string |
| 680 | + wildcard bool |
| 681 | + |
| 682 | + match []string |
| 683 | + nomatch []string |
| 684 | + }{ |
| 685 | + { |
| 686 | + name: "no path", |
| 687 | + hostname: "example.com", |
| 688 | + path: "", |
| 689 | + wildcard: false, |
| 690 | + match: []string{ |
| 691 | + "example.com", |
| 692 | + "example.com:80", |
| 693 | + "example.com/", |
| 694 | + "example.com/sub", |
| 695 | + "example.com/sub/", |
| 696 | + }, |
| 697 | + nomatch: []string{"other.com"}, |
| 698 | + }, |
| 699 | + { |
| 700 | + name: "root path with trailing slash", |
| 701 | + hostname: "example.com", |
| 702 | + path: "/", |
| 703 | + wildcard: false, |
| 704 | + match: []string{ |
| 705 | + "example.com", |
| 706 | + "example.com:80", |
| 707 | + "example.com/", |
| 708 | + "example.com/sub", |
| 709 | + "example.com/sub/", |
| 710 | + }, |
| 711 | + nomatch: []string{"other.com"}, |
| 712 | + }, |
| 713 | + { |
| 714 | + name: "subpath with trailing slash", |
| 715 | + hostname: "example.com", |
| 716 | + path: "/sub/", |
| 717 | + wildcard: false, |
| 718 | + match: []string{ |
| 719 | + "example.com/sub/", |
| 720 | + "example.com/sub/subsub", |
| 721 | + }, |
| 722 | + nomatch: []string{ |
| 723 | + "other.com", |
| 724 | + "example.com", |
| 725 | + "example.com:80", |
| 726 | + "example.com/", |
| 727 | + "example.com/sub", // path with trailing slash doesn't match URL without |
| 728 | + "example.com/subpar", // path segment boundary match required |
| 729 | + }, |
| 730 | + }, |
| 731 | + { |
| 732 | + name: "subpath without trailing slash", |
| 733 | + hostname: "example.com", |
| 734 | + path: "/sub", |
| 735 | + wildcard: false, |
| 736 | + match: []string{ |
| 737 | + "example.com/sub", |
| 738 | + "example.com/sub/", |
| 739 | + "example.com/sub/subsub", |
| 740 | + }, |
| 741 | + nomatch: []string{ |
| 742 | + "other.com", |
| 743 | + "example.com", |
| 744 | + "example.com:80", |
| 745 | + "example.com/", |
| 746 | + "example.com/subpar", // path segment boundary match required |
| 747 | + }, |
| 748 | + }, |
| 749 | + { |
| 750 | + name: "wildcard", |
| 751 | + hostname: "www.example.com", |
| 752 | + path: "/", |
| 753 | + wildcard: true, |
| 754 | + match: []string{ |
| 755 | + "www.example.com", |
| 756 | + "www.example.com/", |
| 757 | + "www.example.com/sub", |
| 758 | + "www.example.com/sub/", |
| 759 | + "www.example.com:80", |
| 760 | + "www.example.com:80/", |
| 761 | + "www.example.com:80/sub", |
| 762 | + "www.example.com:80/sub/", |
| 763 | + "foo.example.com", |
| 764 | + "foo.example.com/", |
| 765 | + "foo.example.com/sub", |
| 766 | + "foo.example.com/sub/", |
| 767 | + }, |
| 768 | + nomatch: []string{ |
| 769 | + "wwwexample.com", |
| 770 | + "foo.bar.example.com", |
| 771 | + }, |
| 772 | + }, |
| 773 | + { |
| 774 | + name: "non-wildcard", |
| 775 | + hostname: "www.example.com", |
| 776 | + path: "/", |
| 777 | + wildcard: false, |
| 778 | + match: []string{ |
| 779 | + "www.example.com", |
| 780 | + "www.example.com/", |
| 781 | + "www.example.com/sub", |
| 782 | + "www.example.com/sub/", |
| 783 | + "www.example.com:80", |
| 784 | + "www.example.com:80/", |
| 785 | + "www.example.com:80/sub", |
| 786 | + "www.example.com:80/sub/", |
| 787 | + }, |
| 788 | + nomatch: []string{ |
| 789 | + "foo.example.com", |
| 790 | + "foo.example.com/", |
| 791 | + "foo.example.com/sub", |
| 792 | + "foo.example.com/sub/", |
| 793 | + "wwwexample.com", |
| 794 | + "foo.bar.example.com", |
| 795 | + }, |
| 796 | + }, |
| 797 | + } |
| 798 | + |
| 799 | + for _, tt := range tests { |
| 800 | + r := regexp.MustCompile(generateRouteRegexp(tt.hostname, tt.path, tt.wildcard)) |
| 801 | + for _, s := range tt.match { |
| 802 | + if !r.Match([]byte(s)) { |
| 803 | + t.Errorf("%s: expected %s to match %s, but didn't", tt.name, r, s) |
| 804 | + } |
| 805 | + } |
| 806 | + for _, s := range tt.nomatch { |
| 807 | + if r.Match([]byte(s)) { |
| 808 | + t.Errorf("%s: expected %s not to match %s, but did", tt.name, r, s) |
| 809 | + } |
| 810 | + } |
| 811 | + } |
| 812 | +} |
0 commit comments