-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
httpclient3、Okhttp2、Jdk httpclient 拦截器UT
- Loading branch information
Showing
18 changed files
with
424 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...ant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.huaweicloud.sermant.tag.transmission.interceptors.http.client.httpclient; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; | ||
import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; | ||
|
||
import org.apache.commons.httpclient.HttpMethod; | ||
import org.apache.commons.httpclient.methods.GetMethod; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* HttpClient3xInterceptor 单元测试 | ||
* | ||
* @author lilai | ||
* @since 2023-08-17 | ||
*/ | ||
public class HttpClient3xInterceptorTest extends BaseInterceptorTest { | ||
private final HttpClient3xInterceptor interceptor; | ||
|
||
private final Object[] arguments; | ||
|
||
public HttpClient3xInterceptorTest() { | ||
this.interceptor = new HttpClient3xInterceptor(); | ||
this.arguments = new Object[3]; | ||
} | ||
|
||
@Test | ||
public void testHttpClient3() { | ||
ExecuteContext context; | ||
ExecuteContext resContext; | ||
Map<String, List<String>> addHeaders = new HashMap<>(); | ||
Map<String, List<String>> tags = new HashMap<>(); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 无Headers无Tags | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(0, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders().length); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 有Headers无Tags | ||
addHeaders.put("defaultKey", Collections.singletonList("defaultValue")); | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(1, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders().length); | ||
Assert.assertEquals("defaultValue", | ||
((HttpMethod) resContext.getArguments()[1]).getRequestHeaders()[0].getValue() | ||
); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 有Headers有Tags | ||
List<String> ids = new ArrayList<>(); | ||
ids.add("testId001"); | ||
ids.add("testId002"); | ||
tags.put("id", ids); | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(1, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders("id").length); | ||
Assert.assertEquals("testId001", | ||
((HttpMethod) resContext.getArguments()[1]).getRequestHeaders("id")[0].getValue()); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 测试TagTransmissionConfig开关关闭时 | ||
tagTransmissionConfig.setEnabled(false); | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(0, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders("id").length); | ||
tagTransmissionConfig.setEnabled(true); | ||
TrafficUtils.removeTrafficTag(); | ||
} | ||
|
||
private ExecuteContext buildContext(Map<String, List<String>> addHeaders) { | ||
HttpMethod httpMethod = new GetMethod("sermant.io"); | ||
for (Map.Entry<String, List<String>> entry : addHeaders.entrySet()) { | ||
for (String val : entry.getValue()) { | ||
httpMethod.setRequestHeader(entry.getKey(), val); | ||
} | ||
} | ||
|
||
arguments[1] = httpMethod; | ||
return ExecuteContext.forMemberMethod(new Object(), null, arguments, null, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...d/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.huaweicloud.sermant.tag.transmission.interceptors.http.client.jdk; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; | ||
import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; | ||
|
||
import sun.net.www.MessageHeader; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* JdkHttpClientInterceptor 单元测试 | ||
* | ||
* @author lilai | ||
* @since 2023-08-17 | ||
*/ | ||
public class JdkHttpClientInterceptorTest extends BaseInterceptorTest { | ||
private final JdkHttpClientInterceptor interceptor; | ||
|
||
private final Object[] arguments; | ||
|
||
public JdkHttpClientInterceptorTest() { | ||
this.interceptor = new JdkHttpClientInterceptor(); | ||
this.arguments = new Object[1]; | ||
} | ||
|
||
@Test | ||
public void testHttpClient3() { | ||
ExecuteContext context; | ||
ExecuteContext resContext; | ||
Map<String, List<String>> addHeaders = new HashMap<>(); | ||
Map<String, List<String>> tags = new HashMap<>(); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 无Headers无Tags | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(0, ((MessageHeader) resContext.getArguments()[0]).getHeaders().size()); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 有Headers无Tags | ||
addHeaders.put("defaultKey", Collections.singletonList("defaultValue")); | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(1, ((MessageHeader) resContext.getArguments()[0]).getHeaders().size()); | ||
Assert.assertEquals("defaultValue", | ||
((MessageHeader) resContext.getArguments()[0]).getHeaders().get("defaultKey").get(0)); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 有Headers有Tags | ||
List<String> ids = new ArrayList<>(); | ||
ids.add("testId001"); | ||
ids.add("testId002"); | ||
tags.put("id", ids); | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(2, ((MessageHeader) resContext.getArguments()[0]).getHeaders().get("id").size()); | ||
Assert.assertEquals("testId002", | ||
((MessageHeader) resContext.getArguments()[0]).getHeaders().get("id").get(0)); | ||
TrafficUtils.removeTrafficTag(); | ||
|
||
// 测试TagTransmissionConfig开关关闭时 | ||
tagTransmissionConfig.setEnabled(false); | ||
addHeaders.clear(); | ||
context = buildContext(addHeaders); | ||
TrafficUtils.updateTrafficTag(tags); | ||
resContext = interceptor.before(context); | ||
Assert.assertEquals(0, ((MessageHeader) resContext.getArguments()[0]).getHeaders().size()); | ||
tagTransmissionConfig.setEnabled(true); | ||
TrafficUtils.removeTrafficTag(); | ||
} | ||
|
||
private ExecuteContext buildContext(Map<String, List<String>> addHeaders) { | ||
MessageHeader messageHeader = new MessageHeader(); | ||
for (Map.Entry<String, List<String>> entry : addHeaders.entrySet()) { | ||
for (String val : entry.getValue()) { | ||
messageHeader.add(entry.getKey(), val); | ||
} | ||
} | ||
arguments[0] = messageHeader; | ||
|
||
return ExecuteContext.forMemberMethod(new Object(), null, arguments, null, null); | ||
} | ||
} |
Oops, something went wrong.