|
|
@ -21,15 +21,14 @@ package com.tencent.cloud.common.util;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Sets;
|
|
|
|
|
|
|
|
import com.tencent.cloud.common.util.expresstion.ExpressionLabelUtils;
|
|
|
|
import com.tencent.cloud.common.util.expresstion.ExpressionLabelUtils;
|
|
|
|
import com.tencent.cloud.common.util.expresstion.ServletExpressionLabelUtils;
|
|
|
|
import com.tencent.cloud.common.util.expresstion.ServletExpressionLabelUtils;
|
|
|
|
import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils;
|
|
|
|
import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils;
|
|
|
|
import org.junit.Assert;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpCookie;
|
|
|
|
import org.springframework.http.HttpCookie;
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
@ -39,12 +38,15 @@ import org.springframework.mock.web.MockCookie;
|
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
|
import org.springframework.mock.web.server.MockServerWebExchange;
|
|
|
|
import org.springframework.mock.web.server.MockServerWebExchange;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static java.util.stream.Collectors.toSet;
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* test for {@link ExpressionLabelUtils}.
|
|
|
|
* test for {@link ExpressionLabelUtils}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author lepdou 2022-05-27
|
|
|
|
* @author lepdou 2022-05-27
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
|
public class ExpressionLabelUtilsTest {
|
|
|
|
public class ExpressionLabelUtilsTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -64,20 +66,20 @@ public class ExpressionLabelUtilsTest {
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(validLabel1));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(validLabel1)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(validLabel2));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(validLabel2)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(validLabel3));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(validLabel3)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(validLabel4));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(validLabel4)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(validLabel5));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(validLabel5)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(invalidLabel1));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel1)).isTrue();
|
|
|
|
Assert.assertFalse(ExpressionLabelUtils.isExpressionLabel(invalidLabel2));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel2)).isFalse();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(invalidLabel3));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel3)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(invalidLabel4));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel4)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(invalidLabel5));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel5)).isTrue();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(invalidLabel6));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel6)).isTrue();
|
|
|
|
Assert.assertFalse(ExpressionLabelUtils.isExpressionLabel(invalidLabel7));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel7)).isFalse();
|
|
|
|
Assert.assertTrue(ExpressionLabelUtils.isExpressionLabel(invalidLabel8));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel8)).isTrue();
|
|
|
|
Assert.assertFalse(ExpressionLabelUtils.isExpressionLabel(invalidLabel9));
|
|
|
|
assertThat(ExpressionLabelUtils.isExpressionLabel(invalidLabel9)).isFalse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -97,9 +99,9 @@ public class ExpressionLabelUtilsTest {
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> labelKeys = Sets.newHashSet(validLabel1, validLabel2, validLabel3, validLabel4, validLabel5,
|
|
|
|
Set<String> labelKeys = Stream.of(validLabel1, validLabel2, validLabel3, validLabel4, validLabel5,
|
|
|
|
invalidLabel1, invalidLabel2, invalidLabel3, invalidLabel4, invalidLabel5, invalidLabel6, invalidLabel7,
|
|
|
|
invalidLabel1, invalidLabel2, invalidLabel3, invalidLabel4, invalidLabel5, invalidLabel6, invalidLabel7,
|
|
|
|
invalidLabel8, invalidLabel9);
|
|
|
|
invalidLabel8, invalidLabel9).collect(toSet());
|
|
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
request.setQueryString("uid=zhangsan");
|
|
|
|
request.setQueryString("uid=zhangsan");
|
|
|
@ -110,20 +112,20 @@ public class ExpressionLabelUtilsTest {
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> result = ServletExpressionLabelUtils.resolve(request, labelKeys);
|
|
|
|
Map<String, String> result = ServletExpressionLabelUtils.resolve(request, labelKeys);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel1));
|
|
|
|
assertThat(result.get(validLabel1)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel2));
|
|
|
|
assertThat(result.get(validLabel2)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel3));
|
|
|
|
assertThat(result.get(validLabel3)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertEquals("GET", result.get(validLabel4));
|
|
|
|
assertThat(result.get(validLabel4)).isEqualTo("GET");
|
|
|
|
Assert.assertEquals("/users", result.get(validLabel5));
|
|
|
|
assertThat(result.get(validLabel5)).isEqualTo("/users");
|
|
|
|
Assert.assertNull(result.get(invalidLabel1));
|
|
|
|
assertThat(result.get(invalidLabel1)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel2));
|
|
|
|
assertThat(result.get(invalidLabel2)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel3));
|
|
|
|
assertThat(result.get(invalidLabel3)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel4));
|
|
|
|
assertThat(result.get(invalidLabel4)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel5));
|
|
|
|
assertThat(result.get(invalidLabel5)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel6));
|
|
|
|
assertThat(result.get(invalidLabel6)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel7));
|
|
|
|
assertThat(result.get(invalidLabel7)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel8));
|
|
|
|
assertThat(result.get(invalidLabel8)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel9));
|
|
|
|
assertThat(result.get(invalidLabel9)).isNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -143,9 +145,9 @@ public class ExpressionLabelUtilsTest {
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> labelKeys = Sets.newHashSet(validLabel1, validLabel2, validLabel3, validLabel4, validLabel5,
|
|
|
|
Set<String> labelKeys = Stream.of(validLabel1, validLabel2, validLabel3, validLabel4, validLabel5,
|
|
|
|
invalidLabel1, invalidLabel2, invalidLabel3, invalidLabel4, invalidLabel5, invalidLabel6, invalidLabel7,
|
|
|
|
invalidLabel1, invalidLabel2, invalidLabel3, invalidLabel4, invalidLabel5, invalidLabel6, invalidLabel7,
|
|
|
|
invalidLabel8, invalidLabel9);
|
|
|
|
invalidLabel8, invalidLabel9).collect(toSet());
|
|
|
|
|
|
|
|
|
|
|
|
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://calleeService/user/get?uid=zhangsan")
|
|
|
|
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://calleeService/user/get?uid=zhangsan")
|
|
|
|
.header("uid", "zhangsan")
|
|
|
|
.header("uid", "zhangsan")
|
|
|
@ -154,20 +156,20 @@ public class ExpressionLabelUtilsTest {
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> result = SpringWebExpressionLabelUtils.resolve(exchange, labelKeys);
|
|
|
|
Map<String, String> result = SpringWebExpressionLabelUtils.resolve(exchange, labelKeys);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel1));
|
|
|
|
assertThat(result.get(validLabel1)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel2));
|
|
|
|
assertThat(result.get(validLabel2)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel3));
|
|
|
|
assertThat(result.get(validLabel3)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertEquals("GET", result.get(validLabel4));
|
|
|
|
assertThat(result.get(validLabel4)).isEqualTo("GET");
|
|
|
|
Assert.assertEquals("/user/get", result.get(validLabel5));
|
|
|
|
assertThat(result.get(validLabel5)).isEqualTo("/user/get");
|
|
|
|
Assert.assertNull(result.get(invalidLabel1));
|
|
|
|
assertThat(result.get(invalidLabel1)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel2));
|
|
|
|
assertThat(result.get(invalidLabel2)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel3));
|
|
|
|
assertThat(result.get(invalidLabel3)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel4));
|
|
|
|
assertThat(result.get(invalidLabel4)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel5));
|
|
|
|
assertThat(result.get(invalidLabel5)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel6));
|
|
|
|
assertThat(result.get(invalidLabel6)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel7));
|
|
|
|
assertThat(result.get(invalidLabel7)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel8));
|
|
|
|
assertThat(result.get(invalidLabel8)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel9));
|
|
|
|
assertThat(result.get(invalidLabel9)).isNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -187,9 +189,9 @@ public class ExpressionLabelUtilsTest {
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel8 = "$${http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
String invalidLabel9 = "#{http.uri}";
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> labelKeys = Sets.newHashSet(validLabel1, validLabel2, validLabel3, validLabel4, validLabel5,
|
|
|
|
Set<String> labelKeys = Stream.of(validLabel1, validLabel2, validLabel3, validLabel4, validLabel5,
|
|
|
|
invalidLabel1, invalidLabel2, invalidLabel3, invalidLabel4, invalidLabel5, invalidLabel6, invalidLabel7,
|
|
|
|
invalidLabel1, invalidLabel2, invalidLabel3, invalidLabel4, invalidLabel5, invalidLabel6, invalidLabel7,
|
|
|
|
invalidLabel8, invalidLabel9);
|
|
|
|
invalidLabel8, invalidLabel9).collect(toSet());
|
|
|
|
|
|
|
|
|
|
|
|
MockClientHttpRequest request = new MockClientHttpRequest();
|
|
|
|
MockClientHttpRequest request = new MockClientHttpRequest();
|
|
|
|
request.setMethod(HttpMethod.GET);
|
|
|
|
request.setMethod(HttpMethod.GET);
|
|
|
@ -198,19 +200,19 @@ public class ExpressionLabelUtilsTest {
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> result = SpringWebExpressionLabelUtils.resolve(request, labelKeys);
|
|
|
|
Map<String, String> result = SpringWebExpressionLabelUtils.resolve(request, labelKeys);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel1));
|
|
|
|
assertThat(result.get(validLabel1)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertEquals("zhangsan", result.get(validLabel2));
|
|
|
|
assertThat(result.get(validLabel2)).isEqualTo("zhangsan");
|
|
|
|
Assert.assertNull(result.get(validLabel3));
|
|
|
|
assertThat(result.get(validLabel3)).isNull();
|
|
|
|
Assert.assertEquals("GET", result.get(validLabel4));
|
|
|
|
assertThat(result.get(validLabel4)).isEqualTo("GET");
|
|
|
|
Assert.assertEquals("/user/get", result.get(validLabel5));
|
|
|
|
assertThat(result.get(validLabel5)).isEqualTo("/user/get");
|
|
|
|
Assert.assertNull(result.get(invalidLabel1));
|
|
|
|
assertThat(result.get(invalidLabel1)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel2));
|
|
|
|
assertThat(result.get(invalidLabel2)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel3));
|
|
|
|
assertThat(result.get(invalidLabel3)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel4));
|
|
|
|
assertThat(result.get(invalidLabel4)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel5));
|
|
|
|
assertThat(result.get(invalidLabel5)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel6));
|
|
|
|
assertThat(result.get(invalidLabel6)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel7));
|
|
|
|
assertThat(result.get(invalidLabel7)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel8));
|
|
|
|
assertThat(result.get(invalidLabel8)).isNull();
|
|
|
|
Assert.assertNull(result.get(invalidLabel9));
|
|
|
|
assertThat(result.get(invalidLabel9)).isNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|