@@ -45,8 +45,8 @@ func (suite *HandlerTestSuite) SetupTest() {
4545 }
4646 allowedPaths := map [string ]struct {}{
4747 // TODO - To fix this test for a valid path (i.e.- '/test/path'), the path suffix needs to be removed from h.parseTargetURL
48- "// test/path/test/path" : {},
49- "// test/path/to/multiple/test/path/to/multiple" : {},
48+ "/test/path/test/path" : {},
49+ "/test/path/to/multiple/test/path/to/multiple" : {},
5050 }
5151 // Start a test server that always returns 200
5252 suite .httpServer = httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
@@ -75,7 +75,7 @@ func (suite *HandlerTestSuite) TearDownTest() {
7575}
7676
7777func (suite * HandlerTestSuite ) TestHandleRequest () {
78- for _ , tc := range []struct {
78+ for _ , testCase := range []struct {
7979 name string
8080 resolveServiceNameErr error
8181 initialCachedData * kube.IngressValue
@@ -89,22 +89,22 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
8989 resolveServiceNameErr : nil ,
9090 initialCachedData : & kube.IngressValue {
9191 Host : "www.example.com" ,
92- Path : "/ test/path" ,
92+ Path : "test/path" ,
9393 Targets : []string {"test-targets-name-1" },
9494 },
9595 reqHost : "www.example.com" ,
96- reqPath : "/ test/path" ,
96+ reqPath : "test/path" ,
9797 expectedStatus : http .StatusOK ,
9898 }, {
99- name : "No ingress headers,multiple targets found in ingress cache" ,
99+ name : "No ingress headers, multiple targets found in ingress cache" ,
100100 resolveServiceNameErr : nil ,
101101 initialCachedData : & kube.IngressValue {
102102 Host : "www.example.com" ,
103- Path : "/ test/path/to/multiple" ,
103+ Path : "test/path/to/multiple" ,
104104 Targets : []string {"test-targets-name-1" , "test-targets-name-2" },
105105 },
106106 reqHost : "www.example.com" ,
107- reqPath : "/ test/path/to/multiple" ,
107+ reqPath : "test/path/to/multiple" ,
108108 expectedStatus : http .StatusOK ,
109109 },
110110 {
@@ -120,41 +120,37 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
120120 resolveServiceNameErr : errors .New ("fail" ),
121121 initialCachedData : & kube.IngressValue {
122122 Host : "www.example.com" ,
123- Path : "/ test/path" ,
123+ Path : "test/path" ,
124124 Targets : []string {"test-targets-name-1" },
125125 },
126126 reqHost : "www.example.com" ,
127- reqPath : "/ test/path" ,
127+ reqPath : "test/path" ,
128128 expectedStatus : http .StatusInternalServerError ,
129129 },
130130 } {
131- suite .Run (tc .name , func () {
131+ suite .Run (testCase .name , func () {
132132 // test case setup
133133 suite .scaler .ExpectedCalls = nil
134- suite .scaler .On ("ResolveServiceName" , mock .Anything ).Return (suite .backendHost , tc .resolveServiceNameErr )
134+ suite .scaler .On ("ResolveServiceName" , mock .Anything ).Return (suite .backendHost , testCase .resolveServiceNameErr )
135135 suite .scaler .On ("SetScaleCtx" , mock .Anything , mock .Anything , mock .Anything ).Return (nil )
136- testIngressCache := ingresscache .NewIngressCache (suite .logger )
137- if tc .initialCachedData != nil {
138- err := testIngressCache .Set (tc .initialCachedData .Host , tc .initialCachedData .Path , tc .initialCachedData .Targets )
139- suite .Require ().NoError (err )
140- }
141136
142- testHandler := suite .createTestHandler (suite .backendPort , testIngressCache )
143- testRequest := suite .createTestHTTPRequest (tc .reqHeaders , tc .reqHost , tc .reqPath )
137+ testHandler , err := suite .createTestHandlerAndInitTestCache (suite .backendPort , testCase .initialCachedData )
138+ suite .Require ().NoError (err )
139+ testRequest := suite .createTestHTTPRequest (testCase .reqHeaders , testCase .reqHost , testCase .reqPath )
144140 testResponse := httptest .NewRecorder ()
145141
146142 // call the testHandler
147143 testHandler .handleRequest (testResponse , testRequest )
148144
149145 // validate the response
150- suite .Require ().Equal (tc .expectedStatus , testResponse .Code )
146+ suite .Require ().Equal (testCase .expectedStatus , testResponse .Code )
151147 suite .scaler .AssertExpectations (suite .T ())
152148 })
153149 }
154150}
155151
156152func (suite * HandlerTestSuite ) TestGetPathAndResourceNames () {
157- for _ , tc := range []struct {
153+ for _ , testCase := range []struct {
158154 name string
159155 errMsg string
160156 initialCachedData * kube.IngressValue
@@ -169,71 +165,86 @@ func (suite *HandlerTestSuite) TestGetPathAndResourceNames() {
169165 name : "No ingress headers, host and path found in ingress cache" ,
170166 initialCachedData : & kube.IngressValue {
171167 Host : "www.example.com" ,
172- Path : "/ test/path" ,
168+ Path : "test/path" ,
173169 Targets : []string {"test-targets-name-1" },
174170 },
175171 reqHost : "www.example.com" ,
176- reqPath : "/ test/path" ,
177- expectedPath : "/ test/path" ,
172+ reqPath : "test/path" ,
173+ expectedPath : "test/path" ,
178174 expectedResourceNames : []string {"test-targets-name-1" },
179175 }, {
180176 name : "Ingress headers, host and path did not found in ingress cache" ,
181177 reqHost : "www.example.com" ,
182- reqPath : "/ test/path" ,
183- expectedPath : "/ test/path" ,
178+ reqPath : "test/path" ,
179+ expectedPath : "test/path" ,
184180 expectedResourceNames : []string {"test-targets-name-1" },
185181 reqHeaders : map [string ]string {
186182 "X-Resource-Name" : "test-targets-name-1" ,
187- "X-Resource-Path" : "/ test/path" ,
183+ "X-Resource-Path" : "test/path" ,
188184 },
189185 }, {
190186 name : "Missing both ingress headers and host and path did not found in ingress cache" ,
191187 reqHost : "www.example.com" ,
192- reqPath : "/ test/path" ,
188+ reqPath : "test/path" ,
193189 expectErr : true ,
194190 errMsg : "No target name header found" ,
191+ }, {
192+ name : "Both ingress headers and found in ingress cache, cache results should be taken" ,
193+ reqHost : "www.example.com" ,
194+ reqPath : "test/path" ,
195+ initialCachedData : & kube.IngressValue {
196+ Host : "www.example.com" ,
197+ Path : "test/path" ,
198+ Targets : []string {"test-targets-from-cache" },
199+ },
200+ reqHeaders : map [string ]string {
201+ "X-Resource-Name" : "test-targets-from-headers" ,
202+ "X-Resource-Path" : "test/path" ,
203+ },
204+ expectedPath : "test/path" ,
205+ expectedResourceNames : []string {"test-targets-from-cache" },
195206 },
196207 } {
197- suite .Run (tc .name , func () {
208+ suite .Run (testCase .name , func () {
198209 // test case setup
199- testIngressCache := ingresscache .NewIngressCache (suite .logger )
200- if tc .initialCachedData != nil {
201- err := testIngressCache .Set (tc .initialCachedData .Host , tc .initialCachedData .Path , tc .initialCachedData .Targets )
202- suite .Require ().NoError (err )
203- }
204-
205- testHandler := suite .createTestHandler (suite .backendPort , testIngressCache )
206- testRequest := suite .createTestHTTPRequest (tc .reqHeaders , tc .reqHost , tc .reqPath )
210+ testHandler , err := suite .createTestHandlerAndInitTestCache (suite .backendPort , testCase .initialCachedData )
211+ suite .Require ().NoError (err )
212+ testRequest := suite .createTestHTTPRequest (testCase .reqHeaders , testCase .reqHost , testCase .reqPath )
207213 resultPath , resultResourceNames , err := testHandler .getPathAndResourceNames (testRequest )
208214
209215 // validate the result
210- if tc .expectErr {
216+ if testCase .expectErr {
211217 suite .Require ().Error (err )
212- suite .Require ().ErrorContains (err , tc .errMsg )
218+ suite .Require ().ErrorContains (err , testCase .errMsg )
213219 } else {
214220 suite .Require ().NoError (err )
215- suite .Require ().Equal (tc .expectedPath , resultPath )
216- suite .Require ().Equal (tc .expectedResourceNames , resultResourceNames )
221+ suite .Require ().Equal (testCase .expectedPath , resultPath )
222+ suite .Require ().Equal (testCase .expectedResourceNames , resultResourceNames )
217223 }
218224 })
219225 }
220226}
221227
222228// --- HandlerTestSuite suite methods ---
223229
224- func (suite * HandlerTestSuite ) createTestHandler (targetPort int , cache ingresscache.IngressHostCacheReader ) Handler {
225- handler , err := NewHandler (
230+ func (suite * HandlerTestSuite ) createTestHandlerAndInitTestCache (targetPort int , initialCachedData * kube.IngressValue ) (Handler , error ) {
231+ testIngressCache := ingresscache .NewIngressCache (suite .logger )
232+ if initialCachedData != nil {
233+ if err := testIngressCache .Set (initialCachedData .Host , initialCachedData .Path , initialCachedData .Targets ); err != nil {
234+ return Handler {}, err
235+ }
236+ }
237+
238+ return NewHandler (
226239 suite .logger ,
227240 suite .starter ,
228241 suite .scaler ,
229242 "X-Resource-Name" ,
230243 "X-Resource-Path" ,
231244 targetPort ,
232245 scalertypes .MultiTargetStrategyPrimary ,
233- cache ,
246+ testIngressCache ,
234247 )
235- suite .Require ().NoError (err )
236- return handler
237248}
238249
239250func (suite * HandlerTestSuite ) createTestHTTPRequest (
0 commit comments