|
9 | 9 | "testing" |
10 | 10 |
|
11 | 11 | "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
12 | 13 | ) |
13 | 14 |
|
14 | 15 | func TestQueryMatcher_Match(t *testing.T) { |
@@ -622,7 +623,7 @@ func TestMatchQueryRegexp(t *testing.T) { |
622 | 623 | } |
623 | 624 | assert.NoError(t, err) |
624 | 625 | assert.Equal(t, tc.wantKey, m.Key()) |
625 | | - assert.NotNil(t, m.Regex()) |
| 626 | + assert.Equal(t, tc.expr, m.Value()) |
626 | 627 | }) |
627 | 628 | } |
628 | 629 | } |
@@ -727,7 +728,50 @@ func TestMatchHeaderRegexp(t *testing.T) { |
727 | 728 | } |
728 | 729 | assert.NoError(t, err) |
729 | 730 | assert.Equal(t, tc.wantKey, m.Key()) |
730 | | - assert.NotNil(t, m.Regex()) |
| 731 | + assert.Equal(t, tc.expr, m.Value()) |
| 732 | + }) |
| 733 | + } |
| 734 | +} |
| 735 | + |
| 736 | +func TestRegexpMatcherAlternationPrecedence(t *testing.T) { |
| 737 | + mq, err := MatchQueryRegexp("scope", "read|write") |
| 738 | + require.NoError(t, err) |
| 739 | + mh, err := MatchHeaderRegexp("X-Role", "admin|user|guest") |
| 740 | + require.NoError(t, err) |
| 741 | + |
| 742 | + queryCases := []struct { |
| 743 | + url string |
| 744 | + want bool |
| 745 | + }{ |
| 746 | + {"/?scope=read", true}, |
| 747 | + {"/?scope=write", true}, |
| 748 | + {"/?scope=readBYPASS", false}, |
| 749 | + {"/?scope=EVILwrite", false}, |
| 750 | + } |
| 751 | + for _, tc := range queryCases { |
| 752 | + t.Run("query "+tc.url, func(t *testing.T) { |
| 753 | + req := httptest.NewRequest(http.MethodGet, tc.url, nil) |
| 754 | + c := NewTestContextOnly(httptest.NewRecorder(), req) |
| 755 | + assert.Equal(t, tc.want, mq.Match(c)) |
| 756 | + }) |
| 757 | + } |
| 758 | + |
| 759 | + headerCases := []struct { |
| 760 | + value string |
| 761 | + want bool |
| 762 | + }{ |
| 763 | + {"admin", true}, |
| 764 | + {"user", true}, |
| 765 | + {"guest", true}, |
| 766 | + {"adminBYPASS", false}, |
| 767 | + {"EVILguest", false}, |
| 768 | + } |
| 769 | + for _, tc := range headerCases { |
| 770 | + t.Run("header "+tc.value, func(t *testing.T) { |
| 771 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 772 | + req.Header.Set("X-Role", tc.value) |
| 773 | + c := NewTestContextOnly(httptest.NewRecorder(), req) |
| 774 | + assert.Equal(t, tc.want, mh.Match(c)) |
731 | 775 | }) |
732 | 776 | } |
733 | 777 | } |
|
0 commit comments