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