From e0c17d8d485e960f6d22f7621dedeb9da6e9bed2 Mon Sep 17 00:00:00 2001 From: shouyuwang <93957183+shouyuwang@users.noreply.github.com> Date: Tue, 28 Jun 2022 23:07:33 +0800 Subject: [PATCH] UT add metadata-transfer unit test 2020.0 (#334) --- CHANGELOG.md | 1 + .../CustomTransitiveMetadataResolverTest.java | 55 +++++++++++++ .../EncodeTransferMedataScgFilterTest.java | 78 +++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/CustomTransitiveMetadataResolverTest.java create mode 100644 spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/filter/EncodeTransferMedataScgFilterTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a2a8bee0..a2df501d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,3 +16,4 @@ - [Fix config file format misspell](https://github.com/Tencent/spring-cloud-tencent/pull/320) - [feat:Add GitHub action of codecov.yml.](https://github.com/Tencent/spring-cloud-tencent/pull/330) - [Feature: add spring cloud tencent logo](https://github.com/Tencent/spring-cloud-tencent/pull/331) +- [UT: add metadata-transfer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/334) diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/CustomTransitiveMetadataResolverTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/CustomTransitiveMetadataResolverTest.java new file mode 100644 index 000000000..463c093d4 --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/CustomTransitiveMetadataResolverTest.java @@ -0,0 +1,55 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.metadata; + +import java.util.Map; + +import com.tencent.cloud.metadata.core.CustomTransitiveMetadataResolver; +import org.assertj.core.api.Assertions; +import org.junit.Test; + +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.server.MockServerWebExchange; + +/** + * Test for {@link CustomTransitiveMetadataResolver}. + * @author quan + */ +public class CustomTransitiveMetadataResolverTest { + + @Test + public void test() { + MockServerHttpRequest.BaseBuilder builder = MockServerHttpRequest.get(""); + builder.header("X-SCT-Metadata-Transitive-a", "test"); + MockServerWebExchange exchange = MockServerWebExchange.from(builder); + Map resolve = CustomTransitiveMetadataResolver.resolve(exchange); + Assertions.assertThat(resolve.size()).isEqualTo(1); + Assertions.assertThat(resolve.get("a")).isEqualTo("test"); + } + + @Test + public void testServlet() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.addHeader("X-SCT-Metadata-Transitive-a", "test"); + Map resolve = CustomTransitiveMetadataResolver.resolve(request); + Assertions.assertThat(resolve.size()).isEqualTo(1); + Assertions.assertThat(resolve.get("a")).isEqualTo("test"); + } +} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/filter/EncodeTransferMedataScgFilterTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/filter/EncodeTransferMedataScgFilterTest.java new file mode 100644 index 000000000..c09c01b0a --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/filter/EncodeTransferMedataScgFilterTest.java @@ -0,0 +1,78 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.metadata.core.filter; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.Map; + +import com.tencent.cloud.common.constant.MetadataConstant; +import com.tencent.cloud.common.util.JacksonUtils; +import com.tencent.cloud.metadata.core.EncodeTransferMedataScgFilter; +import org.assertj.core.api.Assertions; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.context.ApplicationContext; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.server.MockServerWebExchange; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * Test for {@link EncodeTransferMedataScgFilter}. + * @author quan + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, + classes = EncodeTransferMedataScgFilterTest.TestApplication.class, + properties = { "spring.config.location = classpath:application-test.yml", "spring.main.web-application-type = reactive" }) +public class EncodeTransferMedataScgFilterTest { + + @Autowired + private ApplicationContext applicationContext; + + @Mock + private GatewayFilterChain chain; + + @Test + public void testTransitiveMetadataFromApplicationConfig() throws UnsupportedEncodingException { + EncodeTransferMedataScgFilter filter = applicationContext.getBean(EncodeTransferMedataScgFilter.class); + MockServerHttpRequest.BaseBuilder builder = MockServerHttpRequest.get(""); + MockServerWebExchange exchange = MockServerWebExchange.from(builder); + filter.filter(exchange, chain); + String metadataStr = exchange.getRequest().getHeaders().getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA); + String decode = URLDecoder.decode(metadataStr, "UTF-8"); + Map transitiveMap = JacksonUtils.deserialize2Map(decode); + Assertions.assertThat(transitiveMap.size()).isEqualTo(1); + Assert.assertEquals(transitiveMap.get("b"), "2"); + } + + @SpringBootApplication + protected static class TestApplication { + + } +}