@@ -318,6 +318,135 @@ func TestUpstream_chash_hash_on_consumer(t *testing.T) {
318318 resp .Body .Close ()
319319}
320320
321+ func TestUpstream_chash_hash_on_wrong_key (t * testing.T ) {
322+ tests := []HttpTestCase {
323+ {
324+ caseDesc : "create chash upstream with wrong key" ,
325+ Object : ManagerApiExpect (t ),
326+ Method : http .MethodPut ,
327+ Path : "/apisix/admin/upstreams/2" ,
328+ Body : `{
329+ "nodes": [{
330+ "host": "172.16.238.20",
331+ "port": 1980,
332+ "weight": 1
333+ },
334+ {
335+ "host": "172.16.238.20",
336+ "port": 1981,
337+ "weight": 1
338+ }],
339+ "type": "chash",
340+ "key": "not_support"
341+ }` ,
342+ Headers : map [string ]string {"Authorization" : token },
343+ ExpectStatus : http .StatusBadRequest ,
344+ ExpectBody : "schema validate failed: (root): Does not match pattern '^((uri|server_name|server_addr|request_uri|remote_port|remote_addr|query_string|host|hostname)|arg_[0-9a-zA-z_-]+)" ,
345+ },
346+ {
347+ caseDesc : "verify upstream with wrong key" ,
348+ Object : ManagerApiExpect (t ),
349+ Method : http .MethodGet ,
350+ Path : "/apisix/admin/routes/2" ,
351+ Headers : map [string ]string {"Authorization" : token },
352+ ExpectStatus : http .StatusNotFound ,
353+ Sleep : sleepTime ,
354+ },
355+ }
356+
357+ for _ , tc := range tests {
358+ testCaseCheck (tc )
359+ }
360+
361+ }
362+
363+ func TestUpstream_chash_hash_on_vars (t * testing.T ) {
364+ tests := []HttpTestCase {
365+ {
366+ caseDesc : "create chash upstream hash_on (vars)" ,
367+ Object : ManagerApiExpect (t ),
368+ Method : http .MethodPut ,
369+ Path : "/apisix/admin/upstreams/1" ,
370+ Body : `{
371+ "nodes": [{
372+ "host": "172.16.238.20",
373+ "port": 1980,
374+ "weight": 1
375+ },
376+ {
377+ "host": "172.16.238.20",
378+ "port": 1981,
379+ "weight": 1
380+ }],
381+ "type": "chash",
382+ "hash_on": "vars",
383+ "key": "arg_device_id"
384+ }` ,
385+ Headers : map [string ]string {"Authorization" : token },
386+ ExpectStatus : http .StatusOK ,
387+ },
388+ {
389+ caseDesc : "verify upstream" ,
390+ Object : ManagerApiExpect (t ),
391+ Method : http .MethodGet ,
392+ Path : "/apisix/admin/upstreams/1" ,
393+ Headers : map [string ]string {"Authorization" : token },
394+ ExpectStatus : http .StatusOK ,
395+ ExpectBody : "\" nodes\" :[{\" host\" :\" 172.16.238.20\" ,\" port\" :1980,\" weight\" :1},{\" host\" :\" 172.16.238.20\" ,\" port\" :1981,\" weight\" :1}],\" type\" :\" chash\" ,\" hash_on\" :\" vars\" ,\" key\" :\" arg_device_id\" " ,
396+ Sleep : sleepTime ,
397+ },
398+ {
399+ caseDesc : "create route using the upstream just created" ,
400+ Object : ManagerApiExpect (t ),
401+ Method : http .MethodPut ,
402+ Path : "/apisix/admin/routes/1" ,
403+ Body : `{
404+ "uri": "/server_port",
405+ "upstream_id": "1"
406+ }` ,
407+ Headers : map [string ]string {"Authorization" : token },
408+ ExpectStatus : http .StatusOK ,
409+ Sleep : sleepTime ,
410+ },
411+ {
412+ caseDesc : "verify route" ,
413+ Object : ManagerApiExpect (t ),
414+ Method : http .MethodGet ,
415+ Path : "/apisix/admin/routes/1" ,
416+ Headers : map [string ]string {"Authorization" : token },
417+ ExpectStatus : http .StatusOK ,
418+ ExpectBody : "\" uri\" :\" /server_port\" ,\" upstream_id\" :\" 1\" " ,
419+ Sleep : sleepTime ,
420+ },
421+ }
422+
423+ for _ , tc := range tests {
424+ testCaseCheck (tc )
425+ }
426+
427+ // hit routes
428+ time .Sleep (time .Duration (500 ) * time .Millisecond )
429+ basepath := "http://127.0.0.1:9080"
430+ var url string
431+ var respBody []byte
432+ res := map [string ]int {}
433+ for i := 0 ; i <= 17 ; i ++ {
434+ url = basepath + "/server_port?device_id=" + strconv .Itoa (i )
435+ req , err := http .NewRequest ("GET" , url , nil )
436+ resp , err := http .DefaultClient .Do (req )
437+ assert .Nil (t , err )
438+ respBody , err = ioutil .ReadAll (resp .Body )
439+ body := string (respBody )
440+ if _ , ok := res [body ]; ! ok {
441+ res [body ] = 1
442+ } else {
443+ res [body ] += 1
444+ }
445+ resp .Body .Close ()
446+ }
447+ assert .True (t , res ["1980" ] == 9 && res ["1981" ] == 9 )
448+ }
449+
321450func TestUpstream_Delete_hash_on (t * testing.T ) {
322451 tests := []HttpTestCase {
323452 {
@@ -348,7 +477,7 @@ func TestUpstream_Delete_hash_on(t *testing.T) {
348477 caseDesc : "hit the route just deleted" ,
349478 Object : APISIXExpect (t ),
350479 Method : http .MethodGet ,
351- Path : "/hello1 " ,
480+ Path : "/server_port " ,
352481 ExpectStatus : http .StatusNotFound ,
353482 ExpectBody : "{\" error_msg\" :\" 404 Route Not Found\" }\n " ,
354483 Sleep : sleepTime ,
0 commit comments