test:test for TransHeadersTransfer

pull/646/head
wulingxiao 3 years ago
parent 37ca6a740e
commit f53b8217d9

@ -50,12 +50,6 @@
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -18,108 +18,69 @@
package com.tencent.cloud.metadata.core;
import java.util.Map;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.metadata.support.DynamicEnvironmentVariable;
import com.tencent.cloud.metadata.support.DynamicEnvironmentVariablesSpringJUnit4ClassRunner;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.reactive.server.FluxExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.server.WebHandler;
import reactor.core.publisher.Flux;
import java.util.Map;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* Test for {@link TransHeadersTransfer}.
*
* @author lingxiao.wlx
*/
@RunWith(DynamicEnvironmentVariablesSpringJUnit4ClassRunner.class)
@DynamicEnvironmentVariable(name = "SCT_TRAFFIC_CONTENT_RAW_TRANSHEADERS", value = "header1,header2,header3")
@SpringBootTest(webEnvironment = DEFINED_PORT,
classes = TransHeadersTransferTest.TestApplication.class,
properties = {"server.port=8081", "spring.config.location = classpath:application-test.yml"})
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
classes = DecodeTransferMetadataServletFilterTest.TestApplication.class,
properties = {"spring.config.location = classpath:application-test.yml"})
public class TransHeadersTransferTest {
@Autowired
private WebApplicationContext webContext;
@Autowired
private DecodeTransferMetadataServletFilter metadataServletFilter;
private MockMvc mockMvc;
private WebTestClient webTestClient;
@Before
public void setupMockMvc() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webContext).addFilter(metadataServletFilter).build();
webTestClient = WebTestClient.bindToApplicationContext(webContext).build();
@AfterClass
public static void afterClass() {
MetadataContextHolder.remove();
}
@Test
public void transferTest() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/transHeaders")
.header("header1", "1")
.header("header2", "2")
.header("header3", "3")
.characterEncoding("UTF-8"))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print(System.err))
.andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
String contentAsString = response.getContentAsString();
Map<String, String> map = JacksonUtils.deserialize2Map(contentAsString);
System.out.println(contentAsString);
public void transferServletTest() {
MetadataContext metadataContext = MetadataContextHolder.get();
metadataContext.setTransHeaders("header1,header2,header3", "");
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("header1", "1");
request.addHeader("header2", "2");
request.addHeader("header3", "3");
TransHeadersTransfer.transfer(request);
Map<String, String> transHeadersKV = MetadataContextHolder.get().getTransHeadersKV();
Assertions.assertEquals(transHeadersKV.get("header1"), "1");
Assertions.assertEquals(transHeadersKV.get("header2"), "2");
Assertions.assertEquals(transHeadersKV.get("header3"), "3");
}
@Test
public void transferReactiveTest(){
FluxExchangeResult<Map<String, String>> result = webTestClient.get().uri("/transHeaders")
.header("header1", "1")
.header("header2", "2")
.header("header3", "3")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.returnResult(new ParameterizedTypeReference<Map<String, String>>() {
});
Flux<Map<String, String>> responseBody = result.getResponseBody();
}
@SpringBootApplication
@EnableFeignClients
@RestController
protected static class TestApplication {
@GetMapping("/transHeaders")
public Map<String, String> test() {
return MetadataContextHolder.get().getTransHeadersKV();
}
public void transferReactiveTest() {
MetadataContext metadataContext = MetadataContextHolder.get();
metadataContext.setTransHeaders("header1,header2,header3", "");
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.get("");
String[] header1 = {"1"};
String[] header2 = {"2"};
String[] header3 = {"3"};
builder.header("header1", header1);
builder.header("header2", header2);
builder.header("header3", header3);
MockServerHttpRequest request = builder.build();
TransHeadersTransfer.transfer(request);
Map<String, String> transHeadersKV = MetadataContextHolder.get().getTransHeadersKV();
Assertions.assertEquals(transHeadersKV.get("header1"), JacksonUtils.serialize2Json(header1));
Assertions.assertEquals(transHeadersKV.get("header2"), JacksonUtils.serialize2Json(header2));
Assertions.assertEquals(transHeadersKV.get("header3"), JacksonUtils.serialize2Json(header3));
}
}

@ -1,50 +0,0 @@
/*
* 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.support;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* DynamicEnvironmentVariable.
*
* @author lingxiao.wlx
*/
@Repeatable(DynamicEnvironmentVariables.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface DynamicEnvironmentVariable {
/**
* EnvironmentVariable name.
*
* @return EnvironmentVariable name.
*/
String name();
/**
* EnvironmentVariable value.
*
* @return EnvironmentVariable value.
*/
String value();
}

@ -1,41 +0,0 @@
/*
* 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.support;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* DynamicEnvironmentVariables for multiDynamicEnvironmentVariable.
*
* @author lingxiao.wlx
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface DynamicEnvironmentVariables {
/**
* DynamicEnvironmentVariable array.
*
* @return dynamicEnvironmentVariable array.
*/
DynamicEnvironmentVariable[] value();
}

@ -1,64 +0,0 @@
/*
* 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.support;
import java.util.Arrays;
import java.util.Objects;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.runners.model.InitializationError;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Enhance {@link SpringJUnit4ClassRunner} to support add environment variables when Junit4 test run.
*
*
* @author lingxiao.wlx
* @see DynamicEnvironmentVariable
* @see DynamicEnvironmentVariables
*/
public class DynamicEnvironmentVariablesSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
/**
* EnvironmentVariables.
*/
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
public DynamicEnvironmentVariablesSpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
super(clazz);
DynamicEnvironmentVariable dynamicEnvironmentVariable = AnnotationUtils.findAnnotation(clazz, DynamicEnvironmentVariable.class);
if (!Objects.isNull(dynamicEnvironmentVariable)) {
String key = dynamicEnvironmentVariable.name();
String value = dynamicEnvironmentVariable.value();
environmentVariables.set(key, value);
}
DynamicEnvironmentVariables dynamicEnvironmentVariables = AnnotationUtils.findAnnotation(clazz, DynamicEnvironmentVariables.class);
if (!Objects.isNull(dynamicEnvironmentVariables)) {
Arrays.stream(dynamicEnvironmentVariables.value()).forEach(
environmentVariable -> {
environmentVariables.set(environmentVariable.name(), environmentVariable.value());
}
);
}
}
}

@ -80,7 +80,6 @@
<byte-buddy.version>1.12.10</byte-buddy.version>
<protobuf-java.version>3.16.1</protobuf-java.version>
<bcprov-jdk15on.version>1.69</bcprov-jdk15on.version>
<system-rules.version>1.16.1</system-rules.version>
<!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.0</maven-source-plugin.version>
@ -249,13 +248,6 @@
<version>${mocktio.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>${system-rules.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Loading…
Cancel
Save