|
|
|
@ -18,6 +18,9 @@
|
|
|
|
|
|
|
|
|
|
package com.tencent.cloud.polaris.router.feign;
|
|
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -29,7 +32,6 @@ import com.tencent.cloud.common.metadata.MetadataContext;
|
|
|
|
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
|
|
|
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
|
|
|
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
|
|
|
|
import com.tencent.cloud.common.util.ExpressionLabelUtils;
|
|
|
|
|
import com.tencent.cloud.common.util.JacksonUtils;
|
|
|
|
|
import com.tencent.cloud.polaris.router.RouterConstants;
|
|
|
|
|
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
|
|
|
|
@ -50,6 +52,7 @@ import static org.mockito.Mockito.when;
|
|
|
|
|
/**
|
|
|
|
|
* test for {@link RouterLabelFeignInterceptor}
|
|
|
|
|
* @author lepdou 2022-05-26
|
|
|
|
|
* @author cheese8 2022-06-20
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
|
public class RouterLabelFeignInterceptorTest {
|
|
|
|
@ -117,26 +120,24 @@ public class RouterLabelFeignInterceptorTest {
|
|
|
|
|
|
|
|
|
|
Collection<String> routerLabels = requestTemplate.headers().get(RouterConstants.ROUTER_LABEL_HEADER);
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(routerLabels);
|
|
|
|
|
for (String value : routerLabels) {
|
|
|
|
|
Map<String, String> labels = unescape(JacksonUtils.deserialize2Map(value));
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("v1", labels.get("k1"));
|
|
|
|
|
Assert.assertEquals("v22", labels.get("k2"));
|
|
|
|
|
Assert.assertEquals("v3", labels.get("k3"));
|
|
|
|
|
Assert.assertEquals("v4", labels.get("k4"));
|
|
|
|
|
Assert.assertEquals(headerUidValue, labels.get("${http.header.uid}"));
|
|
|
|
|
Assert.assertEquals("", labels.get("${http.header.name}"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Map<String, String> routerLabelsMap = new HashMap<>();
|
|
|
|
|
try {
|
|
|
|
|
String routerLabelContent = routerLabels.stream().findFirst().get();
|
|
|
|
|
routerLabelsMap.putAll(JacksonUtils.deserialize2Map(URLDecoder.decode(routerLabelContent, StandardCharsets.UTF_8.name())));
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedEncodingException e) {
|
|
|
|
|
throw new RuntimeException("unsupported charset exception " + StandardCharsets.UTF_8.name());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, String> unescape(Map<String, String> labels) {
|
|
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
|
|
for (Map.Entry<String, String> entry : labels.entrySet()) {
|
|
|
|
|
result.put(ExpressionLabelUtils.unescape(entry.getKey()), ExpressionLabelUtils.unescape(entry.getValue()));
|
|
|
|
|
Assert.assertNotNull(routerLabels);
|
|
|
|
|
Assert.assertEquals("v1", routerLabelsMap.get("k1"));
|
|
|
|
|
Assert.assertEquals("v22", routerLabelsMap.get("k2"));
|
|
|
|
|
Assert.assertEquals("v3", routerLabelsMap.get("k3"));
|
|
|
|
|
Assert.assertEquals("v4", routerLabelsMap.get("k4"));
|
|
|
|
|
Assert.assertEquals(headerUidValue, routerLabelsMap.get("${http.header.uid}"));
|
|
|
|
|
Assert.assertEquals("", routerLabelsMap.get("${http.header.name}"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|