Skip to content

Commit 1e0ce7c

Browse files
committed
Update UTs and add UTs to cover more cases
1 parent 52a6a88 commit 1e0ce7c

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

pkg/dlx/handler_test.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func (suite *HandlerTestSuite) SetupTest() {
4444
resourceReadinessTimeout: 3 * time.Second,
4545
}
4646
allowedPaths := map[string]struct{}{
47-
"/test/path/test/path": {},
48-
"/test/path/to/multiple/test/path/to/multiple": {},
47+
"/test/path": {},
48+
"/test/path/to/multiple": {},
4949
}
5050
// Start a test server that always returns 200
5151
suite.httpServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -130,13 +130,34 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
130130
name: "Request headers flow",
131131
reqHeaders: map[string]string{
132132
"X-Forwarded-Host": "127.0.0.1",
133-
"X-Original-Uri": "test/path",
133+
"X-Original-Uri": "",
134134
"X-Resource-Name": "test-targets-name-1",
135135
},
136136
reqHost: "www.example.com",
137137
reqPath: "test/path",
138138
expectedStatus: http.StatusOK,
139139
},
140+
{
141+
name: "Request headers negative flow - duplicate request path should fail",
142+
reqHeaders: map[string]string{
143+
"X-Forwarded-Host": "127.0.0.1",
144+
"X-Original-Uri": "test/path",
145+
"X-Resource-Name": "test-targets-name-1",
146+
},
147+
reqHost: "www.example.com",
148+
reqPath: "test/path",
149+
expectedStatus: http.StatusBadRequest,
150+
},
151+
{
152+
name: "Request headers flow with resource path header- should fail because path is duplicated",
153+
reqHeaders: map[string]string{
154+
"X-Resource-Name": "test-targets-name-1",
155+
"X-Resource-Path": "test/path",
156+
},
157+
reqHost: "www.example.com",
158+
reqPath: "test/path",
159+
expectedStatus: http.StatusBadRequest,
160+
},
140161
} {
141162
suite.Run(testCase.name, func() {
142163
// test case setup
@@ -166,7 +187,6 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
166187
reqHost string
167188
reqPath string
168189
expectErr bool
169-
expectedPath string
170190
expectedResourceNames []string
171191
}{
172192
{
@@ -178,13 +198,11 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
178198
},
179199
reqHost: "www.example.com",
180200
reqPath: "test/path",
181-
expectedPath: "test/path",
182201
expectedResourceNames: []string{"test-targets-name-1"},
183202
}, {
184203
name: "request headers, host and path did not found in ingress cache",
185204
reqHost: "www.example.com",
186205
reqPath: "test/path",
187-
expectedPath: "test/path",
188206
expectedResourceNames: []string{"test-targets-name-1"},
189207
reqHeaders: map[string]string{
190208
"X-Resource-Name": "test-targets-name-1",
@@ -209,7 +227,6 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
209227
"X-Resource-Name": "test-targets-from-headers",
210228
"X-Resource-Path": "test/path",
211229
},
212-
expectedPath: "test/path",
213230
expectedResourceNames: []string{"test-targets-from-cache"},
214231
},
215232
{
@@ -230,15 +247,14 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
230247
testHandler, err := suite.createTestHandlerAndInitTestCache(suite.backendPort, testCase.initialCachedData)
231248
suite.Require().NoError(err)
232249
testRequest := suite.createTestHTTPRequest(testCase.name, testCase.reqHeaders, testCase.reqHost, testCase.reqPath)
233-
resultPath, resultResourceNames, err := testHandler.getPathAndResourceNames(testRequest)
250+
resultResourceNames, err := testHandler.getResourceNames(testRequest)
234251

235252
// validate the result
236253
if testCase.expectErr {
237254
suite.Require().Error(err)
238255
suite.Require().ErrorContains(err, testCase.errMsg)
239256
} else {
240257
suite.Require().NoError(err)
241-
suite.Require().Equal(testCase.expectedPath, resultPath)
242258
suite.Require().Equal(testCase.expectedResourceNames, resultResourceNames)
243259
}
244260
})
@@ -308,7 +324,8 @@ func (suite *HandlerTestSuite) setScalerMocksBasedOnTestCase(
308324
case "No request headers, scaler fails":
309325
suite.scaler.On("ResolveServiceName", mock.Anything).Return(suite.backendHost, resolveServiceNameErr)
310326
case "No request headers, not found in ingress cache":
311-
case "Request headers flow":
327+
case "Request headers flow",
328+
"Request headers negative flow - duplicate request path should fail":
312329
suite.scaler.On("SetScaleCtx", mock.Anything, mock.Anything, mock.Anything).Return(nil)
313330
default:
314331
suite.scaler.On("ResolveServiceName", mock.Anything).Return(suite.backendHost, resolveServiceNameErr)

0 commit comments

Comments
 (0)