-
Notifications
You must be signed in to change notification settings - Fork 14
Add abstract client tests #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f84f83d
adding AbstractHttpClientTest and an echo target in TestHttpServer
shashank11p 029e2dd
updating AbstractHttpClientTest
shashank11p d775706
updating AbstractHttpClientTest
shashank11p b7190b2
updating okHttp and apache http client tests
shashank11p 7cbcb85
review comments, refactoring
shashank11p a405879
review comments and removing apache async http client changes
shashank11p e163156
review comments
shashank11p File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
testing-common/src/main/java/org/hypertrace/agent/testing/AbstractHttpClientTest.java
This file contains hidden or 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,149 @@ | ||
| /* | ||
| * Copyright The Hypertrace Authors | ||
| * | ||
| * 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 org.hypertrace.agent.testing; | ||
|
|
||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| public abstract class AbstractHttpClientTest extends AbstractInstrumenterTest { | ||
|
|
||
| protected static final TestHttpServer testHttpServer = new TestHttpServer(); | ||
|
|
||
| public static final String ECHO_PATH = "http://localhost:%d/echo"; | ||
|
|
||
| @BeforeAll | ||
| public static void startServer() throws Exception { | ||
| testHttpServer.start(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void closeServer() throws Exception { | ||
| testHttpServer.close(); | ||
| } | ||
|
|
||
| /** | ||
| * Make request using client and return response status code | ||
| * @param uri URI to send request to | ||
| * @param headers Request headers | ||
| * @param body Request body if applicable on given method | ||
| * @param contentType Content-type of request body | ||
| * @return status code of response | ||
| */ | ||
| public abstract int doPostRequest(String uri, Map<String, String> headers, String body, String contentType) throws IOException; | ||
|
shashank11p marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Test | ||
| public void echoJson() throws TimeoutException, InterruptedException, IOException { | ||
| String body = "{\"foo\": \"bar\"}"; | ||
| Map<String, String> headers = new HashMap<>(); | ||
| headers.put("headerName", "headerValue"); | ||
|
shashank11p marked this conversation as resolved.
Outdated
|
||
| String uri = String.format(ECHO_PATH, testHttpServer.port()); | ||
|
|
||
| int status = doPostRequest(uri, headers, body, "application/json"); | ||
|
|
||
| Assertions.assertEquals(200, status); | ||
|
|
||
| TEST_WRITER.waitForTraces(1); | ||
| List<List<SpanData>> traces = TEST_WRITER.getTraces(); | ||
| Assertions.assertEquals(1, traces.size()); | ||
| Assertions.assertEquals(1, traces.get(0).size()); | ||
| SpanData clientSpan = traces.get(0).get(0); | ||
|
|
||
| assertEchoJson(clientSpan, body); | ||
| } | ||
|
|
||
| @Test | ||
| public void echoUrlEncoded() throws TimeoutException, InterruptedException, IOException { | ||
| String body = "key1=value1&key2=value2"; | ||
| Map<String, String> headers = new HashMap<>(); | ||
| headers.put("headerName", "headerValue"); | ||
| String uri = String.format(ECHO_PATH, testHttpServer.port()); | ||
|
|
||
| int status = doPostRequest(uri, headers, body, "application/x-www-form-urlencoded"); | ||
|
|
||
| Assertions.assertEquals(200, status); | ||
|
|
||
| TEST_WRITER.waitForTraces(1); | ||
| List<List<SpanData>> traces = TEST_WRITER.getTraces(); | ||
| Assertions.assertEquals(1, traces.size()); | ||
| Assertions.assertEquals(1, traces.get(0).size()); | ||
| SpanData clientSpan = traces.get(0).get(0); | ||
|
|
||
| assertEchoUrlEncoded(clientSpan, body); | ||
| } | ||
|
|
||
| @Test | ||
| public void echoPlainText() throws TimeoutException, InterruptedException, IOException { | ||
| String body = "foobar"; | ||
| Map<String, String> headers = new HashMap<>(); | ||
| headers.put("headerName", "headerValue"); | ||
| String uri = String.format(ECHO_PATH, testHttpServer.port()); | ||
|
|
||
| int status = doPostRequest(uri, headers, body, "text/plain"); | ||
|
|
||
| Assertions.assertEquals(200, status); | ||
|
|
||
| TEST_WRITER.waitForTraces(1); | ||
| List<List<SpanData>> traces = TEST_WRITER.getTraces(); | ||
| Assertions.assertEquals(1, traces.size()); | ||
| Assertions.assertEquals(1, traces.get(0).size()); | ||
| SpanData clientSpan = traces.get(0).get(0); | ||
|
|
||
| assertEchoPlainText(clientSpan); | ||
| } | ||
|
|
||
|
shashank11p marked this conversation as resolved.
|
||
|
|
||
| public void assertResponseHeaders(SpanData spanData) { | ||
|
shashank11p marked this conversation as resolved.
Outdated
|
||
| Assertions.assertEquals( | ||
| TestHttpServer.RESPONSE_HEADER_VALUE, | ||
| spanData | ||
| .getAttributes() | ||
| .get( | ||
| HypertraceSemanticAttributes.httpResponseHeader( | ||
| TestHttpServer.RESPONSE_HEADER_NAME))); | ||
| } | ||
|
|
||
| public void assertEchoJson(SpanData spanData, String requestBody) { | ||
| assertResponseHeaders(spanData); | ||
| Assertions.assertEquals(requestBody, spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); | ||
| Assertions.assertEquals( | ||
|
shashank11p marked this conversation as resolved.
|
||
| spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY), | ||
| spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); | ||
| } | ||
|
|
||
| public void assertEchoUrlEncoded(SpanData spanData, String requestBody) { | ||
| assertEchoJson(spanData, requestBody); | ||
| } | ||
|
|
||
| public void assertEchoPlainText(SpanData spanData) { | ||
|
shashank11p marked this conversation as resolved.
Outdated
|
||
| assertResponseHeaders(spanData); | ||
|
shashank11p marked this conversation as resolved.
Outdated
|
||
| Assertions.assertNull( | ||
| spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); | ||
| Assertions.assertNull( | ||
| spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.