Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adding AbstractHttpClientTest and an echo target in TestHttpServer
  • Loading branch information
shashank11p committed Jan 29, 2021
commit f84f83d45b279857f63c38bb858757937df10091
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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;

public abstract class AbstractHttpClientTest extends AbstractInstrumenterTest {

protected static final TestHttpServer testHttpServer = new TestHttpServer();

public static final String GET_NO_CONTENT_PATH = "http://localhost:%d/get_no_content";

public static final String GET_JSON_PATH = "http://localhost:%d/get_json";

public static final String POST_PATH = "http://localhost:%d/post";

public static final String POST_REDIRECT_PATH =
"http://localhost:%d/post_redirect_to_get_no_content";

public static final String ECHO_PATH = "http://localhost:%d/echo";
Comment thread
shashank11p marked this conversation as resolved.
Outdated

@BeforeAll
public static void startServer() throws Exception {
testHttpServer.start();
}

@AfterAll
public static void closeServer() throws Exception {
testHttpServer.close();
}

/**
* Test for post request with json body. Use ECHO_PATH as url string
*/
public abstract void echoJson() throws Exception;

/**
* Test for post request with url encoded form body. Use ECHO_PATH as url string
*/
public abstract void echoUrlEncoded() throws Exception;

Comment thread
shashank11p marked this conversation as resolved.
/**
* Test for post request with plain text body. Use ECHO_PATH as url string
*/
public abstract void echoPlainText() throws Exception;

public void assertResponseHeaders(SpanData spanData) {
Comment thread
shashank11p marked this conversation as resolved.
Outdated
Assertions.assertEquals(
TestHttpServer.RESPONSE_HEADER_VALUE,
spanData
.getAttributes()
.get(
HypertraceSemanticAttributes.httpResponseHeader(
TestHttpServer.RESPONSE_HEADER_NAME)));
}

public void assertGetJsonResponseBody(SpanData spanData) {
Assertions.assertEquals(
Comment thread
shashank11p marked this conversation as resolved.
TestHttpServer.GetJsonHandler.RESPONSE_BODY,
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
}

public void assertEchoJson(SpanData spanData) {
assertResponseHeaders(spanData);
Assertions.assertNotNull(spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
Assertions.assertEquals(
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY),
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
}

public void assertEchoUrlEncoded(SpanData spanData) {
assertResponseHeaders(spanData);
Assertions.assertNotNull(spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
Assertions.assertEquals(
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY),
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
}

public void assertEchoPlainText(SpanData spanData) {
Comment thread
shashank11p marked this conversation as resolved.
Outdated
assertResponseHeaders(spanData);
Comment thread
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void start() throws Exception {
handlerList.addHandler(new GetJsonHandler());
handlerList.addHandler(new PostHandler());
handlerList.addHandler(new PostRedirect());
handlerList.addHandler(new EchoHandler());
server.setHandler(handlerList);
server.start();
}
Expand Down Expand Up @@ -152,4 +153,31 @@ public void handle(
}
}
}

static class EchoHandler extends ResponseTestHeadersHandler {
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
super.handle(target, baseRequest, request, response);

if (target.equals("/echo")
&& "post".equalsIgnoreCase(request.getMethod())) {
ServletInputStream inputStream = request.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
while ((nRead = inputStream.read()) != -1) {
buffer.write((byte) nRead);
}

response.setStatus(200);
response.setContentType(request.getContentType());
response.getWriter().print(buffer.toString());
baseRequest.setHandled(true);
}
}
}
}