-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add forwarded plugin for independent from Trace
- Loading branch information
Showing
5 changed files
with
215 additions
and
95 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...n/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptor.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,50 @@ | ||
/* | ||
* Copyright (c) 2023, MegaEase | ||
* 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.megaease.easeagent.plugin.httpurlconnection.interceptor; | ||
|
||
import com.megaease.easeagent.plugin.annotation.AdviceTo; | ||
import com.megaease.easeagent.plugin.api.Context; | ||
import com.megaease.easeagent.plugin.api.config.ConfigConst; | ||
import com.megaease.easeagent.plugin.enums.Order; | ||
import com.megaease.easeagent.plugin.httpurlconnection.ForwardedPlugin; | ||
import com.megaease.easeagent.plugin.httpurlconnection.HttpURLConnectionPlugin; | ||
import com.megaease.easeagent.plugin.httpurlconnection.advice.HttpURLConnectionGetResponseCodeAdvice; | ||
import com.megaease.easeagent.plugin.interceptor.Interceptor; | ||
import com.megaease.easeagent.plugin.interceptor.MethodInfo; | ||
|
||
import java.net.HttpURLConnection; | ||
|
||
@AdviceTo(value = HttpURLConnectionGetResponseCodeAdvice.class, qualifier = "default", plugin = ForwardedPlugin.class) | ||
public class HttpURLConnectionGetResponseCodeForwardedInterceptor implements Interceptor { | ||
|
||
@Override | ||
public void before(MethodInfo methodInfo, Context context) { | ||
HttpURLConnection httpURLConnection = (HttpURLConnection) methodInfo.getInvoker(); | ||
context.injectForwardedHeaders(httpURLConnection::setRequestProperty); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return ConfigConst.PluginID.FORWARDED; | ||
} | ||
|
||
@Override | ||
public int order() { | ||
return Order.FORWARDED.getOrder(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...tpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptorTest.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,56 @@ | ||
/* | ||
* Copyright (c) 2023, MegaEase | ||
* 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.megaease.easeagent.plugin.httpurlconnection.interceptor; | ||
|
||
import com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner; | ||
import com.megaease.easeagent.plugin.api.Context; | ||
import com.megaease.easeagent.plugin.api.config.ConfigConst; | ||
import com.megaease.easeagent.plugin.bridge.EaseAgent; | ||
import com.megaease.easeagent.plugin.interceptor.MethodInfo; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.net.HttpURLConnection; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
@RunWith(EaseAgentJunit4ClassRunner.class) | ||
public class HttpURLConnectionGetResponseCodeForwardedInterceptorTest { | ||
|
||
@Test | ||
public void before() { | ||
Context context = EaseAgent.getContext(); | ||
HttpURLConnection conn = TestUtils.mockHttpURLConnection(); | ||
MethodInfo methodInfo = TestUtils.mockMethodInfo(conn); | ||
HttpURLConnectionGetResponseCodeForwardedInterceptor httpClientDoExecuteForwardedInterceptor = new HttpURLConnectionGetResponseCodeForwardedInterceptor(); | ||
|
||
httpClientDoExecuteForwardedInterceptor.before(methodInfo, context); | ||
assertNull(conn.getRequestProperty(TestUtils.FORWARDED_NAME)); | ||
context.put(TestUtils.FORWARDED_NAME, TestUtils.FORWARDED_VALUE); | ||
httpClientDoExecuteForwardedInterceptor.before(methodInfo, context); | ||
assertEquals(TestUtils.FORWARDED_VALUE, conn.getRequestProperty(TestUtils.FORWARDED_NAME)); | ||
context.remove(TestUtils.FORWARDED_NAME); | ||
} | ||
|
||
@Test | ||
public void getType() { | ||
HttpURLConnectionGetResponseCodeForwardedInterceptor httpClientDoExecuteForwardedInterceptor = new HttpURLConnectionGetResponseCodeForwardedInterceptor(); | ||
assertEquals(ConfigConst.PluginID.FORWARDED, httpClientDoExecuteForwardedInterceptor.getType()); | ||
} | ||
} |
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
23 changes: 0 additions & 23 deletions
23
.../src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestConst.java
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
.../src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestUtils.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,105 @@ | ||
/* | ||
* Copyright (c) 2023, MegaEase | ||
* 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.megaease.easeagent.plugin.httpurlconnection.interceptor; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.net.HttpHeaders; | ||
import com.megaease.easeagent.plugin.interceptor.MethodInfo; | ||
import lombok.SneakyThrows; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
final class TestUtils { | ||
public static final String FORWARDED_NAME = "X-Forwarded-For"; | ||
public static final String FORWARDED_VALUE = "testForwarded"; | ||
public static final String RESPONSE_TAG_NAME = "X-EG-Test"; | ||
public static final String RESPONSE_TAG_VALUE = "X-EG-Test-Value"; | ||
|
||
@SneakyThrows | ||
static MethodInfo mockMethodInfo() { | ||
HttpURLConnection httpURLConnection = mockHttpURLConnection(); | ||
return mockMethodInfo(httpURLConnection); | ||
} | ||
|
||
static MethodInfo mockMethodInfo(HttpURLConnection httpURLConnection) { | ||
MethodInfo methodInfo = MethodInfo.builder() | ||
.invoker(httpURLConnection).retValue(httpURLConnection) | ||
.build(); | ||
return methodInfo; | ||
} | ||
|
||
@SneakyThrows | ||
static HttpURLConnection mockHttpURLConnection() { | ||
URL url = new URL("http://127.0.0.1:8080"); | ||
Map<String, String> responseHeader = ImmutableMap.of(TestUtils.RESPONSE_TAG_NAME, TestUtils.RESPONSE_TAG_VALUE); | ||
return getConnection(url, "GET", null, responseHeader); | ||
} | ||
|
||
static HttpURLConnection getConnection( | ||
URL url, String method, Map<String, String> requestHeaders, Map<String, String> responseHeader) throws IOException { | ||
|
||
HttpURLConnection conn = new HttpURLConnection(url) { | ||
|
||
@Override | ||
public void connect() throws IOException { | ||
|
||
} | ||
|
||
@Override | ||
public void disconnect() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean usingProxy() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public int getResponseCode() throws IOException { | ||
return 200; | ||
} | ||
|
||
@Override | ||
public Map<String, List<String>> getHeaderFields() { | ||
Map<String, List<String>> fields = new HashMap<>(); | ||
for (String key : responseHeader.keySet()) { | ||
fields.put(key, Lists.newArrayList(responseHeader.get(key))); | ||
} | ||
return fields; | ||
} | ||
}; | ||
conn.setRequestMethod(method); | ||
conn.setDoInput(true); | ||
conn.setDoOutput(true); | ||
conn.setRequestProperty(HttpHeaders.HOST, url.getHost()); | ||
if (requestHeaders != null) { | ||
for (String key : requestHeaders.keySet()) { | ||
conn.setRequestProperty(key, requestHeaders.get(key)); | ||
} | ||
} | ||
|
||
return conn; | ||
} | ||
} |