@@ -157,8 +157,8 @@ func TestIPHashPolicy(t *testing.T) {
157
157
// We should be able to predict where every request is routed.
158
158
req .RemoteAddr = "172.0.0.1:80"
159
159
h := ipHash .Select (pool , req , nil )
160
- if h != pool [1 ] {
161
- t .Error ("Expected ip hash policy host to be the second host." )
160
+ if h != pool [0 ] {
161
+ t .Error ("Expected ip hash policy host to be the first host." )
162
162
}
163
163
req .RemoteAddr = "172.0.0.2:80"
164
164
h = ipHash .Select (pool , req , nil )
@@ -167,8 +167,8 @@ func TestIPHashPolicy(t *testing.T) {
167
167
}
168
168
req .RemoteAddr = "172.0.0.3:80"
169
169
h = ipHash .Select (pool , req , nil )
170
- if h != pool [1 ] {
171
- t .Error ("Expected ip hash policy host to be the second host." )
170
+ if h != pool [0 ] {
171
+ t .Error ("Expected ip hash policy host to be the first host." )
172
172
}
173
173
req .RemoteAddr = "172.0.0.4:80"
174
174
h = ipHash .Select (pool , req , nil )
@@ -179,8 +179,8 @@ func TestIPHashPolicy(t *testing.T) {
179
179
// we should get the same results without a port
180
180
req .RemoteAddr = "172.0.0.1"
181
181
h = ipHash .Select (pool , req , nil )
182
- if h != pool [1 ] {
183
- t .Error ("Expected ip hash policy host to be the second host." )
182
+ if h != pool [0 ] {
183
+ t .Error ("Expected ip hash policy host to be the first host." )
184
184
}
185
185
req .RemoteAddr = "172.0.0.2"
186
186
h = ipHash .Select (pool , req , nil )
@@ -189,8 +189,8 @@ func TestIPHashPolicy(t *testing.T) {
189
189
}
190
190
req .RemoteAddr = "172.0.0.3"
191
191
h = ipHash .Select (pool , req , nil )
192
- if h != pool [1 ] {
193
- t .Error ("Expected ip hash policy host to be the second host." )
192
+ if h != pool [0 ] {
193
+ t .Error ("Expected ip hash policy host to be the first host." )
194
194
}
195
195
req .RemoteAddr = "172.0.0.4"
196
196
h = ipHash .Select (pool , req , nil )
@@ -203,8 +203,8 @@ func TestIPHashPolicy(t *testing.T) {
203
203
req .RemoteAddr = "172.0.0.4"
204
204
pool [1 ].setHealthy (false )
205
205
h = ipHash .Select (pool , req , nil )
206
- if h != pool [0 ] {
207
- t .Error ("Expected ip hash policy host to be the first host." )
206
+ if h != pool [2 ] {
207
+ t .Error ("Expected ip hash policy host to be the third host." )
208
208
}
209
209
210
210
req .RemoteAddr = "172.0.0.2"
@@ -217,8 +217,8 @@ func TestIPHashPolicy(t *testing.T) {
217
217
req .RemoteAddr = "172.0.0.3"
218
218
pool [2 ].setHealthy (false )
219
219
h = ipHash .Select (pool , req , nil )
220
- if h != pool [1 ] {
221
- t .Error ("Expected ip hash policy host to be the second host." )
220
+ if h != pool [0 ] {
221
+ t .Error ("Expected ip hash policy host to be the first host." )
222
222
}
223
223
req .RemoteAddr = "172.0.0.4"
224
224
h = ipHash .Select (pool , req , nil )
@@ -300,8 +300,8 @@ func TestClientIPHashPolicy(t *testing.T) {
300
300
// We should be able to predict where every request is routed.
301
301
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.1:80" )
302
302
h := ipHash .Select (pool , req , nil )
303
- if h != pool [1 ] {
304
- t .Error ("Expected ip hash policy host to be the second host." )
303
+ if h != pool [0 ] {
304
+ t .Error ("Expected ip hash policy host to be the first host." )
305
305
}
306
306
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.2:80" )
307
307
h = ipHash .Select (pool , req , nil )
@@ -310,8 +310,8 @@ func TestClientIPHashPolicy(t *testing.T) {
310
310
}
311
311
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.3:80" )
312
312
h = ipHash .Select (pool , req , nil )
313
- if h != pool [1 ] {
314
- t .Error ("Expected ip hash policy host to be the second host." )
313
+ if h != pool [0 ] {
314
+ t .Error ("Expected ip hash policy host to be the first host." )
315
315
}
316
316
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.4:80" )
317
317
h = ipHash .Select (pool , req , nil )
@@ -322,8 +322,8 @@ func TestClientIPHashPolicy(t *testing.T) {
322
322
// we should get the same results without a port
323
323
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.1" )
324
324
h = ipHash .Select (pool , req , nil )
325
- if h != pool [1 ] {
326
- t .Error ("Expected ip hash policy host to be the second host." )
325
+ if h != pool [0 ] {
326
+ t .Error ("Expected ip hash policy host to be the first host." )
327
327
}
328
328
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.2" )
329
329
h = ipHash .Select (pool , req , nil )
@@ -332,8 +332,8 @@ func TestClientIPHashPolicy(t *testing.T) {
332
332
}
333
333
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.3" )
334
334
h = ipHash .Select (pool , req , nil )
335
- if h != pool [1 ] {
336
- t .Error ("Expected ip hash policy host to be the second host." )
335
+ if h != pool [0 ] {
336
+ t .Error ("Expected ip hash policy host to be the first host." )
337
337
}
338
338
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.4" )
339
339
h = ipHash .Select (pool , req , nil )
@@ -346,8 +346,8 @@ func TestClientIPHashPolicy(t *testing.T) {
346
346
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.4" )
347
347
pool [1 ].setHealthy (false )
348
348
h = ipHash .Select (pool , req , nil )
349
- if h != pool [0 ] {
350
- t .Error ("Expected ip hash policy host to be the first host." )
349
+ if h != pool [2 ] {
350
+ t .Error ("Expected ip hash policy host to be the third host." )
351
351
}
352
352
353
353
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.2" )
@@ -360,8 +360,8 @@ func TestClientIPHashPolicy(t *testing.T) {
360
360
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.3" )
361
361
pool [2 ].setHealthy (false )
362
362
h = ipHash .Select (pool , req , nil )
363
- if h != pool [1 ] {
364
- t .Error ("Expected ip hash policy host to be the second host." )
363
+ if h != pool [0 ] {
364
+ t .Error ("Expected ip hash policy host to be the first host." )
365
365
}
366
366
caddyhttp .SetVar (req .Context (), caddyhttp .ClientIPVarKey , "172.0.0.4" )
367
367
h = ipHash .Select (pool , req , nil )
@@ -470,21 +470,21 @@ func TestQueryHashPolicy(t *testing.T) {
470
470
471
471
request = httptest .NewRequest (http .MethodGet , "/?foo=100000" , nil )
472
472
h = queryPolicy .Select (pool , request , nil )
473
- if h != pool [0 ] {
474
- t .Error ("Expected query policy host to be the first host." )
473
+ if h != pool [1 ] {
474
+ t .Error ("Expected query policy host to be the second host." )
475
475
}
476
476
477
477
request = httptest .NewRequest (http .MethodGet , "/?foo=1" , nil )
478
478
pool [0 ].setHealthy (false )
479
479
h = queryPolicy .Select (pool , request , nil )
480
- if h != pool [1 ] {
481
- t .Error ("Expected query policy host to be the second host." )
480
+ if h != pool [2 ] {
481
+ t .Error ("Expected query policy host to be the third host." )
482
482
}
483
483
484
484
request = httptest .NewRequest (http .MethodGet , "/?foo=100000" , nil )
485
485
h = queryPolicy .Select (pool , request , nil )
486
- if h != pool [2 ] {
487
- t .Error ("Expected query policy host to be the third host." )
486
+ if h != pool [1 ] {
487
+ t .Error ("Expected query policy host to be the second host." )
488
488
}
489
489
490
490
// We should be able to resize the host pool and still be able to predict
@@ -533,14 +533,14 @@ func TestURIHashPolicy(t *testing.T) {
533
533
534
534
request := httptest .NewRequest (http .MethodGet , "/test" , nil )
535
535
h := uriPolicy .Select (pool , request , nil )
536
- if h != pool [1 ] {
537
- t .Error ("Expected uri policy host to be the second host." )
536
+ if h != pool [2 ] {
537
+ t .Error ("Expected uri policy host to be the third host." )
538
538
}
539
539
540
540
pool [2 ].setHealthy (false )
541
541
h = uriPolicy .Select (pool , request , nil )
542
- if h != pool [1 ] {
543
- t .Error ("Expected uri policy host to be the second host." )
542
+ if h != pool [0 ] {
543
+ t .Error ("Expected uri policy host to be the first host." )
544
544
}
545
545
546
546
request = httptest .NewRequest (http .MethodGet , "/test_2" , nil )
@@ -691,54 +691,54 @@ func TestCookieHashPolicy(t *testing.T) {
691
691
}
692
692
693
693
func TestCookieHashPolicyWithSecureRequest (t * testing.T ) {
694
- ctx , cancel := caddy .NewContext (caddy.Context {Context : context .Background ()})
695
- defer cancel ()
696
- cookieHashPolicy := CookieHashSelection {}
697
- if err := cookieHashPolicy .Provision (ctx ); err != nil {
698
- t .Errorf ("Provision error: %v" , err )
699
- t .FailNow ()
700
- }
701
-
702
- pool := testPool ()
703
- pool [0 ].Dial = "localhost:8080"
704
- pool [1 ].Dial = "localhost:8081"
705
- pool [2 ].Dial = "localhost:8082"
706
- pool [0 ].setHealthy (true )
707
- pool [1 ].setHealthy (false )
708
- pool [2 ].setHealthy (false )
709
-
710
- // Create a test server that serves HTTPS requests
711
- ts := httptest .NewTLSServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
712
- h := cookieHashPolicy .Select (pool , r , w )
713
- if h != pool [0 ] {
714
- t .Error ("Expected cookieHashPolicy host to be the first only available host." )
715
- }
716
- }))
717
- defer ts .Close ()
718
-
719
- // Make a new HTTPS request to the test server
720
- client := ts .Client ()
721
- request , err := http .NewRequest (http .MethodGet , ts .URL + "/test" , nil )
722
- if err != nil {
723
- t .Fatal (err )
724
- }
725
- response , err := client .Do (request )
726
- if err != nil {
727
- t .Fatal (err )
728
- }
729
-
730
- // Check if the cookie set is Secure and has SameSiteNone mode
731
- cookies := response .Cookies ()
732
- if len (cookies ) == 0 {
733
- t .Fatal ("Expected a cookie to be set" )
734
- }
735
- cookie := cookies [0 ]
736
- if ! cookie .Secure {
737
- t .Error ("Expected cookie Secure attribute to be true when request is secure" )
738
- }
739
- if cookie .SameSite != http .SameSiteNoneMode {
740
- t .Error ("Expected cookie SameSite attribute to be None when request is secure" )
741
- }
694
+ ctx , cancel := caddy .NewContext (caddy.Context {Context : context .Background ()})
695
+ defer cancel ()
696
+ cookieHashPolicy := CookieHashSelection {}
697
+ if err := cookieHashPolicy .Provision (ctx ); err != nil {
698
+ t .Errorf ("Provision error: %v" , err )
699
+ t .FailNow ()
700
+ }
701
+
702
+ pool := testPool ()
703
+ pool [0 ].Dial = "localhost:8080"
704
+ pool [1 ].Dial = "localhost:8081"
705
+ pool [2 ].Dial = "localhost:8082"
706
+ pool [0 ].setHealthy (true )
707
+ pool [1 ].setHealthy (false )
708
+ pool [2 ].setHealthy (false )
709
+
710
+ // Create a test server that serves HTTPS requests
711
+ ts := httptest .NewTLSServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
712
+ h := cookieHashPolicy .Select (pool , r , w )
713
+ if h != pool [0 ] {
714
+ t .Error ("Expected cookieHashPolicy host to be the first only available host." )
715
+ }
716
+ }))
717
+ defer ts .Close ()
718
+
719
+ // Make a new HTTPS request to the test server
720
+ client := ts .Client ()
721
+ request , err := http .NewRequest (http .MethodGet , ts .URL + "/test" , nil )
722
+ if err != nil {
723
+ t .Fatal (err )
724
+ }
725
+ response , err := client .Do (request )
726
+ if err != nil {
727
+ t .Fatal (err )
728
+ }
729
+
730
+ // Check if the cookie set is Secure and has SameSiteNone mode
731
+ cookies := response .Cookies ()
732
+ if len (cookies ) == 0 {
733
+ t .Fatal ("Expected a cookie to be set" )
734
+ }
735
+ cookie := cookies [0 ]
736
+ if ! cookie .Secure {
737
+ t .Error ("Expected cookie Secure attribute to be true when request is secure" )
738
+ }
739
+ if cookie .SameSite != http .SameSiteNoneMode {
740
+ t .Error ("Expected cookie SameSite attribute to be None when request is secure" )
741
+ }
742
742
}
743
743
744
744
func TestCookieHashPolicyWithFirstFallback (t * testing.T ) {
0 commit comments