|
| 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.testing.junit.http.AbstractHttpServerTest |
| 9 | +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint._ |
| 10 | +import org.apache.pekko.actor.ActorSystem |
| 11 | +import org.apache.pekko.http.scaladsl.Http |
| 12 | +import org.apache.pekko.http.scaladsl.Http.ServerBinding |
| 13 | +import org.apache.pekko.http.scaladsl.model.StatusCodes.Found |
| 14 | +import org.apache.pekko.http.scaladsl.server.Directives._ |
| 15 | +import org.apache.pekko.stream.ActorMaterializer |
| 16 | +import org.apache.pekko.stream.scaladsl.Sink |
| 17 | + |
| 18 | +import java.util.function.Supplier |
| 19 | +import scala.concurrent.Await |
| 20 | + |
| 21 | +object PekkoHttpTestServerSourceWebServer { |
| 22 | + implicit val system = ActorSystem("my-system") |
| 23 | + implicit val materializer = ActorMaterializer() |
| 24 | + // needed for the future flatMap/onComplete in the end |
| 25 | + implicit val executionContext = system.dispatcher |
| 26 | + |
| 27 | + var route = get { |
| 28 | + concat( |
| 29 | + path(SUCCESS.rawPath()) { |
| 30 | + complete( |
| 31 | + AbstractHttpServerTest.controller(SUCCESS, supplier(SUCCESS.getBody)) |
| 32 | + ) |
| 33 | + }, |
| 34 | + path(INDEXED_CHILD.rawPath()) { |
| 35 | + parameterMap { map => |
| 36 | + val supplier = new Supplier[String] { |
| 37 | + def get(): String = { |
| 38 | + INDEXED_CHILD.collectSpanAttributes(new UrlParameterProvider { |
| 39 | + override def getParameter(name: String): String = |
| 40 | + map.get(name).orNull |
| 41 | + }) |
| 42 | + "" |
| 43 | + } |
| 44 | + } |
| 45 | + complete(AbstractHttpServerTest.controller(INDEXED_CHILD, supplier)) |
| 46 | + } |
| 47 | + }, |
| 48 | + path(QUERY_PARAM.rawPath()) { |
| 49 | + extractUri { uri => |
| 50 | + complete( |
| 51 | + AbstractHttpServerTest |
| 52 | + .controller(INDEXED_CHILD, supplier(uri.queryString().orNull)) |
| 53 | + ) |
| 54 | + } |
| 55 | + }, |
| 56 | + path(REDIRECT.rawPath()) { |
| 57 | + redirect( |
| 58 | + AbstractHttpServerTest |
| 59 | + .controller(REDIRECT, supplier(REDIRECT.getBody)), |
| 60 | + Found |
| 61 | + ) |
| 62 | + }, |
| 63 | + path(ERROR.rawPath()) { |
| 64 | + complete( |
| 65 | + 500 -> AbstractHttpServerTest |
| 66 | + .controller(ERROR, supplier(ERROR.getBody)) |
| 67 | + ) |
| 68 | + }, |
| 69 | + path("path" / LongNumber / "param") { id => |
| 70 | + complete( |
| 71 | + AbstractHttpServerTest.controller(PATH_PARAM, supplier(id.toString)) |
| 72 | + ) |
| 73 | + }, |
| 74 | + path( |
| 75 | + "test1" / IntNumber / HexIntNumber / LongNumber / HexLongNumber / |
| 76 | + DoubleNumber / JavaUUID / Remaining |
| 77 | + ) { (_, _, _, _, _, _, _) => |
| 78 | + complete(SUCCESS.getBody) |
| 79 | + }, |
| 80 | + pathPrefix("test2") { |
| 81 | + concat( |
| 82 | + path("first") { |
| 83 | + complete(SUCCESS.getBody) |
| 84 | + }, |
| 85 | + path("second") { |
| 86 | + complete(SUCCESS.getBody) |
| 87 | + } |
| 88 | + ) |
| 89 | + } |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + private var binding: ServerBinding = null |
| 94 | + |
| 95 | + def start(port: Int): Unit = synchronized { |
| 96 | + if (null == binding) { |
| 97 | + import scala.concurrent.duration._ |
| 98 | + binding = Await.result( |
| 99 | + Http() |
| 100 | + .bind("localhost", port) |
| 101 | + .map(_.handleWith(route)) |
| 102 | + .to(Sink.ignore) |
| 103 | + .run(), |
| 104 | + 10.seconds |
| 105 | + ) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + def stop(): Unit = synchronized { |
| 110 | + if (null != binding) { |
| 111 | + binding.unbind() |
| 112 | + system.terminate() |
| 113 | + binding = null |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + def supplier(string: String): Supplier[String] = { |
| 118 | + new Supplier[String] { |
| 119 | + def get(): String = { |
| 120 | + string |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments