Skip to content

Commit 8bc0aa8

Browse files
committed
update based on reviews
1 parent 136024d commit 8bc0aa8

File tree

4 files changed

+76
-36
lines changed

4 files changed

+76
-36
lines changed

internal/mode/static/nginx/config/servers.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,6 @@ func createRouteMatch(match dataplane.Match, redirectPath string) routeMatch {
721721
// Query Parameters are case-sensitive so case is preserved.
722722
// The match type is optional and defaults to "Exact".
723723
func createQueryParamKeyValString(p dataplane.HTTPQueryParamMatch) string {
724-
// this condition check is added for unit tests
725-
if p.Type == "" {
726-
p.Type = dataplane.MatchTypeExact
727-
}
728724
return p.Name + "=" + string(p.Type) + "=" + p.Value
729725
}
730726

@@ -736,10 +732,6 @@ func createQueryParamKeyValString(p dataplane.HTTPQueryParamMatch) string {
736732
// We preserve the case of the name here because NGINX allows us to look up the header names in a case-insensitive
737733
// manner.
738734
func createHeaderKeyValString(h dataplane.HTTPHeaderMatch) string {
739-
// this condition check is added for unit tests
740-
if h.Type == "" {
741-
h.Type = dataplane.MatchTypeExact
742-
}
743735
return h.Name + HeaderMatchSeparator + string(h.Type) + HeaderMatchSeparator + h.Value
744736
}
745737

internal/mode/static/nginx/config/servers_test.go

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,20 @@ func TestExecuteForDefaultServers(t *testing.T) {
589589
}
590590
}
591591

592+
func getHeaderMatchType(matchType string) dataplane.MatchType {
593+
if len(matchType) == 0 {
594+
return dataplane.MatchTypeExact
595+
}
596+
return dataplane.MatchType(matchType)
597+
}
598+
599+
func getQueryParamMatchType(matchType string) dataplane.MatchType {
600+
if len(matchType) == 0 {
601+
return dataplane.MatchTypeExact
602+
}
603+
return dataplane.MatchType(matchType)
604+
}
605+
592606
func TestCreateServers(t *testing.T) {
593607
t.Parallel()
594608
const (
@@ -710,25 +724,30 @@ func TestCreateServers(t *testing.T) {
710724
{
711725
Name: "Version",
712726
Value: "V1",
727+
Type: getHeaderMatchType(""),
713728
},
714729
{
715730
Name: "test",
716731
Value: "foo",
732+
Type: getHeaderMatchType(""),
717733
},
718734
{
719735
Name: "my-header",
720736
Value: "my-value",
737+
Type: getHeaderMatchType(""),
721738
},
722739
},
723740
QueryParams: []dataplane.HTTPQueryParamMatch{
724741
{
725742
// query names and values should not be normalized to lowercase
726743
Name: "GrEat",
727744
Value: "EXAMPLE",
745+
Type: getQueryParamMatchType(""),
728746
},
729747
{
730748
Name: "test",
731749
Value: "foo=bar",
750+
Type: getQueryParamMatchType(""),
732751
},
733752
},
734753
},
@@ -797,6 +816,7 @@ func TestCreateServers(t *testing.T) {
797816
{
798817
Name: "redirect",
799818
Value: "this",
819+
Type: getHeaderMatchType(""),
800820
},
801821
},
802822
},
@@ -839,6 +859,7 @@ func TestCreateServers(t *testing.T) {
839859
{
840860
Name: "rewrite",
841861
Value: "this",
862+
Type: getHeaderMatchType(""),
842863
},
843864
},
844865
},
@@ -878,6 +899,7 @@ func TestCreateServers(t *testing.T) {
878899
{
879900
Name: "filter",
880901
Value: "this",
902+
Type: getHeaderMatchType(""),
881903
},
882904
},
883905
},
@@ -2702,14 +2724,17 @@ func TestCreateRouteMatch(t *testing.T) {
27022724
{
27032725
Name: "header-1",
27042726
Value: "val-1",
2727+
Type: getHeaderMatchType(""),
27052728
},
27062729
{
27072730
Name: "header-2",
27082731
Value: "val-2",
2732+
Type: getHeaderMatchType(""),
27092733
},
27102734
{
27112735
Name: "header-3",
27122736
Value: "val-3",
2737+
Type: getHeaderMatchType(""),
27132738
},
27142739
}
27152740

@@ -2725,14 +2750,17 @@ func TestCreateRouteMatch(t *testing.T) {
27252750
{
27262751
Name: "arg1",
27272752
Value: "val1",
2753+
Type: getQueryParamMatchType(""),
27282754
},
27292755
{
27302756
Name: "arg2",
27312757
Value: "val2=another-val",
2758+
Type: getQueryParamMatchType(""),
27322759
},
27332760
{
27342761
Name: "arg3",
27352762
Value: "==val3",
2763+
Type: getQueryParamMatchType(""),
27362764
},
27372765
}
27382766

@@ -2856,31 +2884,49 @@ func TestCreateRouteMatch(t *testing.T) {
28562884

28572885
func TestCreateQueryParamKeyValString(t *testing.T) {
28582886
t.Parallel()
2859-
g := NewWithT(t)
28602887

2861-
expected := "key=Exact=value"
2862-
2863-
result := createQueryParamKeyValString(
2864-
dataplane.HTTPQueryParamMatch{
2865-
Name: "key",
2866-
Value: "value",
2867-
Type: dataplane.MatchTypeExact,
2888+
tests := []struct {
2889+
msg string
2890+
input dataplane.HTTPQueryParamMatch
2891+
expected string
2892+
}{
2893+
{
2894+
msg: "Exact match",
2895+
input: dataplane.HTTPQueryParamMatch{
2896+
Name: "key",
2897+
Value: "value",
2898+
Type: getQueryParamMatchType(string(dataplane.MatchTypeExact)),
2899+
},
2900+
expected: "key=Exact=value",
28682901
},
2869-
)
2870-
2871-
g.Expect(result).To(Equal(expected))
2872-
2873-
expected = "KeY=RegularExpression=vaLUe-[a-z]=="
2874-
2875-
result = createQueryParamKeyValString(
2876-
dataplane.HTTPQueryParamMatch{
2877-
Name: "KeY",
2878-
Value: "vaLUe-[a-z]==",
2879-
Type: dataplane.MatchTypeRegularExpression,
2902+
{
2903+
msg: "RegularExpression match",
2904+
input: dataplane.HTTPQueryParamMatch{
2905+
Name: "KeY",
2906+
Value: "vaLUe-[a-z]==",
2907+
Type: getQueryParamMatchType(string(dataplane.MatchTypeRegularExpression)),
2908+
},
2909+
expected: "KeY=RegularExpression=vaLUe-[a-z]==",
28802910
},
2881-
)
2911+
{
2912+
msg: "empty match type",
2913+
input: dataplane.HTTPQueryParamMatch{
2914+
Name: "keY",
2915+
Value: "vaLUe==",
2916+
Type: getQueryParamMatchType(""),
2917+
},
2918+
expected: "keY=Exact=vaLUe==",
2919+
},
2920+
}
28822921

2883-
g.Expect(result).To(Equal(expected))
2922+
for _, tc := range tests {
2923+
t.Run(tc.msg, func(t *testing.T) {
2924+
t.Parallel()
2925+
g := NewWithT(t)
2926+
result := createQueryParamKeyValString(tc.input)
2927+
g.Expect(result).To(Equal(tc.expected))
2928+
})
2929+
}
28842930
}
28852931

28862932
func TestCreateHeaderKeyValString(t *testing.T) {
@@ -2893,7 +2939,7 @@ func TestCreateHeaderKeyValString(t *testing.T) {
28932939
dataplane.HTTPHeaderMatch{
28942940
Name: "kEy",
28952941
Value: "vALUe",
2896-
Type: dataplane.MatchTypeExact,
2942+
Type: getHeaderMatchType(string(dataplane.MatchTypeExact)),
28972943
},
28982944
)
28992945

@@ -2905,7 +2951,7 @@ func TestCreateHeaderKeyValString(t *testing.T) {
29052951
dataplane.HTTPHeaderMatch{
29062952
Name: "kEy",
29072953
Value: "vALUe-[0-9]",
2908-
Type: dataplane.MatchTypeRegularExpression,
2954+
Type: getHeaderMatchType(string(dataplane.MatchTypeRegularExpression)),
29092955
},
29102956
)
29112957

internal/mode/static/nginx/modules/src/httpmatches.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ function paramsMatch(requestParams, params) {
210210
const idx = p.indexOf('=', firstIdx + 1);
211211

212212
// Three possible error cases for improperly constructed query parameter match:
213-
// (1) if the index is -1, then there are no second occurence of "=" in the string (e.g. "Exactvalue")
213+
// (1) if the index is -1, then there are no second occurence of "=" in the string (e.g. "key=Exactvalue")
214214
// (2) if the index is 0, then there is no value in the string and has only one "=" (e.g. "key=Exact").
215-
// (3) if the index is equal to length -1, then there is no key and type in the string (e.g. "=Exact=value").
215+
// (3) if the index is equal to length -1, then there is no key and type in the string (e.g. "=value").
216216
if (idx === -1 || idx === 0 || idx === p.length - 1) {
217217
throw Error(`invalid query parameter: ${p}`);
218218
}

internal/mode/static/state/dataplane/types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,17 @@ type HTTPURLRewriteFilter struct {
210210
// PathModifierType is the type of the PathModifier in a redirect or rewrite rule.
211211
type PathModifierType string
212212

213-
// MatchType is the type of match in a MatchRule for headers and query parameters.
214-
type MatchType string
215-
216213
const (
217214
// ReplaceFullPath indicates that we replace the full path.
218215
ReplaceFullPath PathModifierType = "ReplaceFullPath"
219216
// ReplacePrefixMatch indicates that we replace a prefix match.
220217
ReplacePrefixMatch PathModifierType = "ReplacePrefixMatch"
218+
)
221219

220+
// MatchType is the type of match in a MatchRule for headers and query parameters.
221+
type MatchType string
222+
223+
const (
222224
// MatchTypeExact indicates that the match type is exact.
223225
MatchTypeExact MatchType = "Exact"
224226

0 commit comments

Comments
 (0)