|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0 |
| 7 | + |
| 8 | +import io.opentelemetry.instrumentation.test.utils.PortUtils |
| 9 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension |
| 10 | +import io.opentelemetry.sdk.testing.assertj.{SpanDataAssert, TraceAssert} |
| 11 | +import io.opentelemetry.testing.internal.armeria.client.WebClient |
| 12 | +import io.opentelemetry.testing.internal.armeria.common.{ |
| 13 | + AggregatedHttpRequest, |
| 14 | + HttpMethod |
| 15 | +} |
| 16 | +import org.apache.pekko.actor.ActorSystem |
| 17 | +import org.apache.pekko.http.scaladsl.Http |
| 18 | +import org.apache.pekko.http.scaladsl.server.Directives.{ |
| 19 | + IntNumber, |
| 20 | + complete, |
| 21 | + concat, |
| 22 | + path, |
| 23 | + pathEndOrSingleSlash, |
| 24 | + pathPrefix, |
| 25 | + pathSingleSlash |
| 26 | +} |
| 27 | +import org.apache.pekko.http.scaladsl.server.Route |
| 28 | +import org.assertj.core.api.Assertions.assertThat |
| 29 | +import org.junit.jupiter.api.extension.RegisterExtension |
| 30 | +import org.junit.jupiter.api.{AfterAll, Test, TestInstance} |
| 31 | +import sttp.tapir._ |
| 32 | +import sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter |
| 33 | + |
| 34 | +import java.net.{URI, URISyntaxException} |
| 35 | +import java.util.function.Consumer |
| 36 | +import scala.concurrent.duration.DurationInt |
| 37 | +import scala.concurrent.{Await, Future} |
| 38 | + |
| 39 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 40 | +class TapirHttpServerRouteTest { |
| 41 | + @RegisterExtension private val testing: AgentInstrumentationExtension = |
| 42 | + AgentInstrumentationExtension.create |
| 43 | + private val client: WebClient = WebClient.of() |
| 44 | + |
| 45 | + implicit val system: ActorSystem = ActorSystem("my-system") |
| 46 | + |
| 47 | + private def buildAddress(port: Int): URI = try |
| 48 | + new URI("http://localhost:" + port + "/") |
| 49 | + catch { |
| 50 | + case exception: URISyntaxException => |
| 51 | + throw new IllegalStateException(exception) |
| 52 | + } |
| 53 | + |
| 54 | + @Test def testSimple(): Unit = { |
| 55 | + val route = path("test") { |
| 56 | + complete("ok") |
| 57 | + } |
| 58 | + |
| 59 | + test(route, "/test", "GET /test") |
| 60 | + } |
| 61 | + |
| 62 | + @Test def testRoute(): Unit = { |
| 63 | + val route = concat( |
| 64 | + pathEndOrSingleSlash { |
| 65 | + complete("root") |
| 66 | + }, |
| 67 | + pathPrefix("test") { |
| 68 | + concat( |
| 69 | + pathSingleSlash { |
| 70 | + complete("test") |
| 71 | + }, |
| 72 | + path(IntNumber) { _ => |
| 73 | + complete("ok") |
| 74 | + } |
| 75 | + ) |
| 76 | + } |
| 77 | + ) |
| 78 | + |
| 79 | + test(route, "/test/1", "GET /test/*") |
| 80 | + } |
| 81 | + |
| 82 | + @Test def testTapirRoutes(): Unit = { |
| 83 | + val interpreter = PekkoHttpServerInterpreter()(system.dispatcher) |
| 84 | + def makeRoute(input: EndpointInput[Unit]) = { |
| 85 | + interpreter.toRoute( |
| 86 | + endpoint.get |
| 87 | + .in(input) |
| 88 | + .errorOut(stringBody) |
| 89 | + .out(stringBody) |
| 90 | + .serverLogicPure[Future](_ => Right("ok")) |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + val routes = concat( |
| 95 | + concat(makeRoute("test" / "1"), makeRoute("test" / "2")), |
| 96 | + concat(makeRoute("test" / "3"), makeRoute("test" / "4")) |
| 97 | + ) |
| 98 | + |
| 99 | + test(routes, "/test/4", "GET /test/4") |
| 100 | + } |
| 101 | + |
| 102 | + def test(route: Route, path: String, spanName: String): Unit = { |
| 103 | + val port = PortUtils.findOpenPort |
| 104 | + val address: URI = buildAddress(port) |
| 105 | + val binding = |
| 106 | + Await.result(Http().bindAndHandle(route, "localhost", port), 10.seconds) |
| 107 | + try { |
| 108 | + val request = AggregatedHttpRequest.of( |
| 109 | + HttpMethod.GET, |
| 110 | + address.resolve(path).toString |
| 111 | + ) |
| 112 | + val response = client.execute(request).aggregate.join |
| 113 | + assertThat(response.status.code).isEqualTo(200) |
| 114 | + assertThat(response.contentUtf8).isEqualTo("ok") |
| 115 | + |
| 116 | + testing.waitAndAssertTraces(new Consumer[TraceAssert] { |
| 117 | + override def accept(trace: TraceAssert): Unit = |
| 118 | + trace.hasSpansSatisfyingExactly(new Consumer[SpanDataAssert] { |
| 119 | + override def accept(span: SpanDataAssert): Unit = { |
| 120 | + span.hasName(spanName) |
| 121 | + } |
| 122 | + }) |
| 123 | + }) |
| 124 | + } finally { |
| 125 | + binding.unbind() |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @AfterAll |
| 130 | + def cleanUp(): Unit = { |
| 131 | + system.terminate() |
| 132 | + } |
| 133 | +} |
0 commit comments