|
|
|
@ -19,11 +19,12 @@
|
|
|
|
|
package com.tencent.cloud.metadata.core.intercepter;
|
|
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
|
|
|
|
|
import com.tencent.cloud.common.constant.MetadataConstant;
|
|
|
|
|
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
|
|
|
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
|
|
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
|
|
|
import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor;
|
|
|
|
|
import org.assertj.core.api.Assertions;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
|
@ -32,6 +33,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.boot.web.server.LocalServerPort;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestHeader;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
@ -51,9 +55,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
|
|
|
|
properties = { "spring.config.location = classpath:application-test.yml" })
|
|
|
|
|
public class EncodeTransferMedataRestTemplateInterceptorTest {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private MetadataLocalProperties metadataLocalProperties;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
|
|
|
@ -61,30 +62,14 @@ public class EncodeTransferMedataRestTemplateInterceptorTest {
|
|
|
|
|
private int localServerPort;
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test1() {
|
|
|
|
|
// HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
|
// httpHeaders.set(MetadataConstant.HeaderName.CUSTOM_METADATA,
|
|
|
|
|
// "{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}");
|
|
|
|
|
// HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);
|
|
|
|
|
// String metadata = restTemplate
|
|
|
|
|
// .exchange("http://localhost:" + localServerPort + "/test", HttpMethod.GET,
|
|
|
|
|
// httpEntity, String.class)
|
|
|
|
|
// .getBody();
|
|
|
|
|
// Assertions.assertThat(metadata)
|
|
|
|
|
// .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}");
|
|
|
|
|
// Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
|
|
|
|
|
// .isEqualTo("1");
|
|
|
|
|
// Assertions.assertThat(metadataLocalProperties.getContent().get("b"))
|
|
|
|
|
// .isEqualTo("2");
|
|
|
|
|
// Assertions
|
|
|
|
|
// .assertThat(MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_TRANSITIVE, "a"))
|
|
|
|
|
// .isEqualTo("11");
|
|
|
|
|
// Assertions
|
|
|
|
|
// .assertThat(MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_TRANSITIVE, "b"))
|
|
|
|
|
// .isEqualTo("22");
|
|
|
|
|
// Assertions
|
|
|
|
|
// .assertThat(MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_TRANSITIVE, "c"))
|
|
|
|
|
// .isEqualTo("33");
|
|
|
|
|
public void testTransitiveMetadataFromApplicationConfig() {
|
|
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
|
HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);
|
|
|
|
|
String metadata = restTemplate
|
|
|
|
|
.exchange("http://localhost:" + localServerPort + "/test", HttpMethod.GET,
|
|
|
|
|
httpEntity, String.class)
|
|
|
|
|
.getBody();
|
|
|
|
|
Assertions.assertThat(metadata).isEqualTo("2");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SpringBootApplication
|
|
|
|
@ -100,7 +85,7 @@ public class EncodeTransferMedataRestTemplateInterceptorTest {
|
|
|
|
|
public String test(
|
|
|
|
|
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr)
|
|
|
|
|
throws UnsupportedEncodingException {
|
|
|
|
|
return URLDecoder.decode(customMetadataStr, "UTF-8");
|
|
|
|
|
return MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_TRANSITIVE, "b");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|