|
| 1 | +/* |
| 2 | + * Copyright The Hypertrace Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.opentelemetry.javaagent.instrumentation.hypertrace.struts; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | + |
| 21 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.EnumSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.TimeoutException; |
| 26 | +import javax.servlet.DispatcherType; |
| 27 | +import okhttp3.MediaType; |
| 28 | +import okhttp3.Request; |
| 29 | +import okhttp3.RequestBody; |
| 30 | +import okhttp3.Response; |
| 31 | +import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter; |
| 32 | +import org.eclipse.jetty.server.Server; |
| 33 | +import org.eclipse.jetty.servlet.DefaultServlet; |
| 34 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 35 | +import org.eclipse.jetty.util.resource.FileResource; |
| 36 | +import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; |
| 37 | +import org.hypertrace.agent.testing.AbstractInstrumenterTest; |
| 38 | +import org.junit.jupiter.api.AfterAll; |
| 39 | +import org.junit.jupiter.api.Assertions; |
| 40 | +import org.junit.jupiter.api.BeforeAll; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | + |
| 43 | +public class StrutsInstrumentationTest extends AbstractInstrumenterTest { |
| 44 | + |
| 45 | + private static final String REQUEST_BODY = "hello"; |
| 46 | + private static final String REQUEST_HEADER = "requestheader"; |
| 47 | + private static final String REQUEST_HEADER_VALUE = "requestvalue"; |
| 48 | + private static final String RESPONSE_HEADER = "headerName"; |
| 49 | + private static final String RESPONSE_HEADER_VALUE = "headerValue"; |
| 50 | + |
| 51 | + private static Server server = new Server(0); |
| 52 | + private static int serverPort; |
| 53 | + |
| 54 | + @BeforeAll |
| 55 | + public static void startServer() throws Exception { |
| 56 | + ServletContextHandler handler = new ServletContextHandler(); |
| 57 | + FileResource resource = new FileResource(StrutsInstrumentationTest.class.getResource("/")); |
| 58 | + handler.setContextPath("/context"); |
| 59 | + handler.setBaseResource(resource); |
| 60 | + handler.addServlet(DefaultServlet.class, "/"); |
| 61 | + handler.addFilter( |
| 62 | + StrutsPrepareAndExecuteFilter.class, "/*", EnumSet.allOf(DispatcherType.class)); |
| 63 | + server.setHandler(handler); |
| 64 | + server.start(); |
| 65 | + serverPort = server.getConnectors()[0].getLocalPort(); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterAll |
| 69 | + public static void stopServer() throws Exception { |
| 70 | + server.stop(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void postUrlEncoded() throws IOException, TimeoutException, InterruptedException { |
| 75 | + Request request = |
| 76 | + new Request.Builder() |
| 77 | + .url(String.format("http://localhost:%d/context/body", serverPort)) |
| 78 | + .post( |
| 79 | + RequestBody.create( |
| 80 | + REQUEST_BODY, MediaType.get("application/x-www-form-urlencoded"))) |
| 81 | + .build(); |
| 82 | + try (Response response = httpClient.newCall(request).execute()) { |
| 83 | + assertEquals(200, response.code()); |
| 84 | + } |
| 85 | + |
| 86 | + TEST_WRITER.waitForTraces(1); |
| 87 | + List<List<SpanData>> traces = TEST_WRITER.getTraces(); |
| 88 | + assertEquals(1, traces.size()); |
| 89 | + List<SpanData> spans = traces.get(0); |
| 90 | + assertEquals(1, spans.size()); |
| 91 | + SpanData spanData = spans.get(0); |
| 92 | + assertEquals( |
| 93 | + "\"" + new Struts2Action().getJsonString() + "\"", |
| 94 | + spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); |
| 95 | + assertEquals( |
| 96 | + REQUEST_BODY, spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void getHeaders() throws IOException, TimeoutException, InterruptedException { |
| 101 | + Request request = |
| 102 | + new Request.Builder() |
| 103 | + .url(String.format("http://localhost:%d/context/headers", serverPort)) |
| 104 | + .get() |
| 105 | + .header(REQUEST_HEADER, REQUEST_HEADER_VALUE) |
| 106 | + .build(); |
| 107 | + try (Response response = httpClient.newCall(request).execute()) { |
| 108 | + assertEquals(200, response.code()); |
| 109 | + } |
| 110 | + |
| 111 | + TEST_WRITER.waitForTraces(1); |
| 112 | + List<List<SpanData>> traces = TEST_WRITER.getTraces(); |
| 113 | + assertEquals(1, traces.size()); |
| 114 | + List<SpanData> spans = traces.get(0); |
| 115 | + assertEquals(1, spans.size()); |
| 116 | + SpanData spanData = spans.get(0); |
| 117 | + assertEquals( |
| 118 | + RESPONSE_HEADER_VALUE, |
| 119 | + spanData |
| 120 | + .getAttributes() |
| 121 | + .get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER))); |
| 122 | + assertEquals( |
| 123 | + REQUEST_HEADER_VALUE, |
| 124 | + spanData |
| 125 | + .getAttributes() |
| 126 | + .get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER))); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void block() throws IOException, TimeoutException, InterruptedException { |
| 131 | + Request request = |
| 132 | + new Request.Builder() |
| 133 | + .url(String.format("http://localhost:%d/context/body", serverPort)) |
| 134 | + .get() |
| 135 | + .header("mockblock", "true") |
| 136 | + .build(); |
| 137 | + try (Response response = httpClient.newCall(request).execute()) { |
| 138 | + Assertions.assertEquals(403, response.code()); |
| 139 | + } |
| 140 | + |
| 141 | + TEST_WRITER.waitForTraces(1); |
| 142 | + List<List<SpanData>> traces = TEST_WRITER.getTraces(); |
| 143 | + Assertions.assertEquals(1, traces.size()); |
| 144 | + List<SpanData> spans = traces.get(0); |
| 145 | + Assertions.assertEquals(1, spans.size()); |
| 146 | + SpanData spanData = spans.get(0); |
| 147 | + Assertions.assertNull( |
| 148 | + spanData |
| 149 | + .getAttributes() |
| 150 | + .get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER))); |
| 151 | + Assertions.assertNull( |
| 152 | + spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); |
| 153 | + Assertions.assertNull( |
| 154 | + spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); |
| 155 | + } |
| 156 | +} |
0 commit comments