Skip to content

Commit 7e8c261

Browse files
committed
CR comments - Unit tests - add request header flow coverage
1 parent dfc8f38 commit 7e8c261

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

pkg/dlx/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (h *Handler) getValuesFromCache(req *http.Request) (string, []string, error
193193
path := req.URL.Path
194194
resourceNames, err := h.ingressCache.Get(host, path)
195195
if err != nil {
196-
return "", nil, errors.New("Failed to get resourceNames from ingress cache")
196+
return "", nil, errors.New("Failed to get resource names from ingress cache")
197197
}
198198

199199
if len(resourceNames) == 0 {

pkg/dlx/handler_test.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
8484
expectedStatus int
8585
}{
8686
{
87-
name: "No ingress headers, host and path found in ingress cache",
87+
name: "No request headers, host and path found in ingress cache",
8888
resolveServiceNameErr: nil,
8989
initialCachedData: &kube.IngressValue{
9090
Host: "www.example.com",
@@ -95,7 +95,7 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
9595
reqPath: "test/path",
9696
expectedStatus: http.StatusOK,
9797
}, {
98-
name: "No ingress headers, multiple targets found in ingress cache",
98+
name: "No request headers, multiple targets found in ingress cache",
9999
resolveServiceNameErr: nil,
100100
initialCachedData: &kube.IngressValue{
101101
Host: "www.example.com",
@@ -107,15 +107,15 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
107107
expectedStatus: http.StatusOK,
108108
},
109109
{
110-
name: "No ingress headers, not found in ingress cache",
110+
name: "No request headers, not found in ingress cache",
111111
resolveServiceNameErr: nil,
112112
initialCachedData: nil,
113113
reqHost: "unknown",
114114
reqPath: "/notfound",
115115
expectedStatus: http.StatusBadRequest,
116116
},
117117
{
118-
name: "No ingress headers, scaler fails",
118+
name: "No request headers, scaler fails",
119119
resolveServiceNameErr: errors.New("fail"),
120120
initialCachedData: &kube.IngressValue{
121121
Host: "www.example.com",
@@ -126,12 +126,21 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
126126
reqPath: "test/path",
127127
expectedStatus: http.StatusInternalServerError,
128128
},
129+
{
130+
name: "Request headers flow",
131+
reqHeaders: map[string]string{
132+
"X-Forwarded-Host": "127.0.0.1",
133+
"X-Original-Uri": "test/path",
134+
"X-Resource-Name": "test-targets-name-1",
135+
},
136+
reqHost: "www.example.com",
137+
reqPath: "test/path",
138+
expectedStatus: http.StatusOK,
139+
},
129140
} {
130141
suite.Run(testCase.name, func() {
131142
// test case setup
132-
suite.scaler.ExpectedCalls = nil
133-
suite.scaler.On("ResolveServiceName", mock.Anything).Return(suite.backendHost, testCase.resolveServiceNameErr)
134-
suite.scaler.On("SetScaleCtx", mock.Anything, mock.Anything, mock.Anything).Return(nil)
143+
suite.setScalerMocksBasedOnTestCase(testCase.name, testCase.resolveServiceNameErr)
135144

136145
testHandler, err := suite.createTestHandlerAndInitTestCache(suite.backendPort, testCase.initialCachedData)
137146
suite.Require().NoError(err)
@@ -161,7 +170,7 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
161170
expectedResourceNames []string
162171
}{
163172
{
164-
name: "No ingress headers, host and path found in ingress cache",
173+
name: "No request headers, host and path found in ingress cache",
165174
initialCachedData: &kube.IngressValue{
166175
Host: "www.example.com",
167176
Path: "test/path",
@@ -172,7 +181,7 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
172181
expectedPath: "test/path",
173182
expectedResourceNames: []string{"test-targets-name-1"},
174183
}, {
175-
name: "Ingress headers, host and path did not found in ingress cache",
184+
name: "request headers, host and path did not found in ingress cache",
176185
reqHost: "www.example.com",
177186
reqPath: "test/path",
178187
expectedPath: "test/path",
@@ -182,13 +191,13 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
182191
"X-Resource-Path": "test/path",
183192
},
184193
}, {
185-
name: "Missing both ingress headers and host and path did not found in ingress cache",
194+
name: "Missing both request headers and host and path did not found in ingress cache",
186195
reqHost: "www.example.com",
187196
reqPath: "test/path",
188197
expectErr: true,
189198
errMsg: "No target name header found",
190199
}, {
191-
name: "Both ingress headers and found in ingress cache, cache results should be taken",
200+
name: "Both request headers and found in ingress cache, cache results should be taken",
192201
reqHost: "www.example.com",
193202
reqPath: "test/path",
194203
initialCachedData: &kube.IngressValue{
@@ -258,12 +267,35 @@ func (suite *HandlerTestSuite) createTestHTTPRequest(
258267
if reqPath != "" {
259268
req.URL.Path = reqPath
260269
}
270+
271+
if len(reqHeaders) != 0 {
272+
// pull X-Forwarded-Host from suite because it's not yet set during test case creation
273+
reqHeaders["X-Forwarded-Port"] = strconv.Itoa(suite.backendPort)
274+
}
275+
261276
for k, v := range reqHeaders {
262277
req.Header.Set(k, v)
263278
}
264279
return req
265280
}
266281

282+
func (suite *HandlerTestSuite) setScalerMocksBasedOnTestCase(
283+
testName string,
284+
resolveServiceNameErr error,
285+
) {
286+
suite.scaler.ExpectedCalls = nil
287+
switch testName {
288+
case "No request headers, scaler fails":
289+
suite.scaler.On("ResolveServiceName", mock.Anything).Return(suite.backendHost, resolveServiceNameErr)
290+
case "No request headers, not found in ingress cache":
291+
case "Request headers flow":
292+
suite.scaler.On("SetScaleCtx", mock.Anything, mock.Anything, mock.Anything).Return(nil)
293+
default:
294+
suite.scaler.On("ResolveServiceName", mock.Anything).Return(suite.backendHost, resolveServiceNameErr)
295+
suite.scaler.On("SetScaleCtx", mock.Anything, mock.Anything, mock.Anything).Return(nil)
296+
}
297+
}
298+
267299
func TestHandlerTestSuite(t *testing.T) {
268300
suite.Run(t, new(HandlerTestSuite))
269301
}

0 commit comments

Comments
 (0)