Skip to content

Commit

Permalink
Merge pull request #360 from newrelic/unittest/netty-reactor
Browse files Browse the repository at this point in the history
Added unit tests for Netty Reactor Route detection support
  • Loading branch information
IshikaDawda authored Dec 12, 2024
2 parents 244b214 + a0927ae commit 18ee1c5
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.newrelic.agent.security.introspec.InstrumentationTestConfig;
import com.newrelic.agent.security.introspec.SecurityInstrumentationTestRunner;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.security.instrumentation.helpers.URLMappingsHelper;
import com.newrelic.api.agent.security.schema.ApplicationURLMapping;
import com.newrelic.api.agent.security.schema.Framework;
import com.newrelic.api.agent.security.schema.SecurityMetaData;
import io.netty.handler.codec.http.HttpMethod;
import org.junit.AfterClass;
import org.junit.Assert;
Expand All @@ -14,6 +17,10 @@
import reactor.ipc.netty.http.server.HttpServer;
import reactor.ipc.netty.tcp.BlockingNettyContext;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -22,25 +29,29 @@
@InstrumentationTestConfig(includePrefixes = "reactor.ipc.netty.http.server")
public class APIEndpointTest {
private static BlockingNettyContext server;

private static int PORT;

@BeforeClass
public static void beforeClass() throws Exception {
PORT = SecurityInstrumentationTestRunner.getIntrospector().getRandomPort();
server = HttpServer
.create(SecurityInstrumentationTestRunner.getIntrospector().getRandomPort())
.create(PORT)
.startRouter(r -> r
.post("/file/{path}", (req, res) -> res.send())
.get("/echo/{param}", (req, res) -> res.send())
.get("/check", (req, res) -> res.sendString(Mono.just("Check Response data sent...")))
.route(
httpServerRequest -> httpServerRequest.uri().equals("/test") && httpServerRequest.method().equals(HttpMethod.GET),
(req, res) -> res.send(req.receive().retain()))
.ws("/ws", (req, res) -> res.send(req.receive().retain()))
.get("/echo/{param}", (req, res) -> res.send())
);
.post("/file/{path}", (req, res) -> res.send())
.get("/echo/{param}", (req, res) -> res.send())
.get("/check", (req, res) -> res.sendString(Mono.just("Check Response data sent...")))
.route(
httpServerRequest -> httpServerRequest.uri().equals("/test") && httpServerRequest.method().equals(HttpMethod.GET),
(req, res) -> res.send(req.receive().retain()))
.ws("/ws", (req, res) -> res.send(req.receive().retain()))
.get("/echo/{param}", (req, res) -> res.send())
);
}

@AfterClass
public static void afterClass() throws Exception {
if (server != null){
if (server != null) {
server.shutdown();
}
}
Expand All @@ -62,7 +73,6 @@ public void apiEndpointTest() {
Assert.assertNotNull(actualMapping.getPath());
Assert.assertNotNull(actualMapping.getHandler());


Assert.assertEquals(expectedMappings.get(actualMapping.getPath()), actualMapping.getMethod());
if (!actualMapping.getPath().equals("/ws")) {
Assert.assertTrue(actualMapping.getHandler().startsWith(handler));
Expand All @@ -71,4 +81,40 @@ public void apiEndpointTest() {
}
}
}
}

@Test
public void routeTest() throws IOException, URISyntaxException {
service("test");
SecurityMetaData metaData = SecurityInstrumentationTestRunner.getIntrospector().getSecurityMetaData();
Assert.assertEquals("/*", metaData.getRequest().getRoute());
Assert.assertEquals(Framework.NETTY_REACTOR.name(), metaData.getMetaData().getFramework());
}

@Test
public void route1Test() throws IOException, URISyntaxException {
service("ws");
SecurityMetaData metaData = SecurityInstrumentationTestRunner.getIntrospector().getSecurityMetaData();
Assert.assertEquals("/ws", metaData.getRequest().getRoute());
Assert.assertEquals(Framework.NETTY_REACTOR.name(), metaData.getMetaData().getFramework());
}

@Test
public void route2Test() throws IOException, URISyntaxException {
service("echo/name");
SecurityMetaData metaData = SecurityInstrumentationTestRunner.getIntrospector().getSecurityMetaData();
Assert.assertEquals("/echo/{param}", metaData.getRequest().getRoute());
Assert.assertEquals(Framework.NETTY_REACTOR.name(), metaData.getMetaData().getFramework());
}

@Trace(dispatcher = true)
private void service(String path) throws IOException, URISyntaxException {
URL u = new URL(String.format("http://localhost:%s/%s", PORT, path));
HttpURLConnection conn = (HttpURLConnection) u.openConnection();

conn.setRequestProperty("content-type", "text/plain; charset=utf-8");
conn.setRequestMethod("GET");
conn.connect();
System.out.println(conn.getResponseCode());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.newrelic.agent.security.introspec.InstrumentationTestConfig;
import com.newrelic.agent.security.introspec.SecurityInstrumentationTestRunner;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.security.instrumentation.helpers.URLMappingsHelper;
import com.newrelic.api.agent.security.schema.ApplicationURLMapping;
import com.newrelic.api.agent.security.schema.Framework;
import com.newrelic.api.agent.security.schema.SecurityMetaData;
import io.netty.handler.codec.http.HttpMethod;
import org.junit.AfterClass;
import org.junit.Assert;
Expand All @@ -14,6 +17,10 @@
import reactor.netty.DisposableServer;
import reactor.netty.http.server.HttpServer;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -22,11 +29,15 @@
@InstrumentationTestConfig(includePrefixes = "reactor.netty.http.server")
public class APIEndpointTest {
private static DisposableServer server;

private static int PORT;
@BeforeClass
public static void beforeClass() {
PORT = SecurityInstrumentationTestRunner.getIntrospector().getRandomPort();
server = HttpServer
.create()
.port(SecurityInstrumentationTestRunner.getIntrospector().getRandomPort())
.host("localhost")
.port(PORT)
.route(r -> r
.post("/file/{path}", (req, res) -> res.send())
.get("/echo/{param}", (req, res) -> res.send())
Expand All @@ -36,7 +47,7 @@ public static void beforeClass() {
(req, res) -> res.send(req.receive().retain()))
.ws("/ws", (req, res) -> res.send(req.receive().retain()))
.get("/echo/{param}", (req, res) -> res.send())
).bindNow();
).bind().block();
}

@AfterClass
Expand Down Expand Up @@ -72,4 +83,40 @@ public void apiEndpointTest() {
}
}
}

@Test
public void routeTest() throws IOException, URISyntaxException {
service("test");
SecurityMetaData metaData = SecurityInstrumentationTestRunner.getIntrospector().getSecurityMetaData();
Assert.assertEquals("/*", metaData.getRequest().getRoute());
Assert.assertEquals(Framework.NETTY_REACTOR.name(), metaData.getMetaData().getFramework());
}

@Test
public void route1Test() throws IOException, URISyntaxException {
service("check");
SecurityMetaData metaData = SecurityInstrumentationTestRunner.getIntrospector().getSecurityMetaData();
Assert.assertEquals("/check", metaData.getRequest().getRoute());
Assert.assertEquals(Framework.NETTY_REACTOR.name(), metaData.getMetaData().getFramework());
}

@Test
public void route2Test() throws IOException, URISyntaxException {
service("echo/name");
SecurityMetaData metaData = SecurityInstrumentationTestRunner.getIntrospector().getSecurityMetaData();
Assert.assertEquals("/echo/{param}", metaData.getRequest().getRoute());
Assert.assertEquals(Framework.NETTY_REACTOR.name(), metaData.getMetaData().getFramework());
}

@Trace(dispatcher = true)
private void service(String path) throws IOException, URISyntaxException {
URL u = new URL(String.format("http://localhost:%s/%s", PORT, path));
HttpURLConnection conn = (HttpURLConnection) u.openConnection();

conn.setRequestProperty("content-type", "text/plain; charset=utf-8");
conn.setRequestMethod("GET");
conn.connect();
System.out.println(conn.getResponseCode());

}
}

0 comments on commit 18ee1c5

Please sign in to comment.