From b53b5b8b768246b733c04b6c94148aaf26ab3102 Mon Sep 17 00:00:00 2001 From: adamw Date: Thu, 29 Jul 2021 16:13:33 +0200 Subject: [PATCH] #1400: fix play server interpreter when routes are put in context (cherry picked from commit 343995a5680fb79527897d1f5cef9fdd674149b4) --- .../server/play/PlayServerInterpreter.scala | 4 +- .../tapir/server/play/PlayServerTest.scala | 10 ++--- .../play/PlayServerWithContextTest.scala | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerWithContextTest.scala diff --git a/server/play-server/src/main/scala/sttp/tapir/server/play/PlayServerInterpreter.scala b/server/play-server/src/main/scala/sttp/tapir/server/play/PlayServerInterpreter.scala index 4a91a11e8d..ca794d6a2e 100644 --- a/server/play-server/src/main/scala/sttp/tapir/server/play/PlayServerInterpreter.scala +++ b/server/play-server/src/main/scala/sttp/tapir/server/play/PlayServerInterpreter.scala @@ -57,10 +57,10 @@ trait PlayServerInterpreter { } } - override def apply(v1: RequestHeader): Handler = { + override def apply(header: RequestHeader): Handler = { playServerOptions.defaultActionBuilder.async(playServerOptions.playBodyParsers.raw) { request => implicit val bodyListener: BodyListener[Future, HttpEntity] = new PlayBodyListener - val serverRequest = new PlayServerRequest(request) + val serverRequest = new PlayServerRequest(header) val interpreter = new ServerInterpreter[Any, Future, HttpEntity, NoStreams]( new PlayRequestBody(request, playServerOptions), new PlayToResponseBody, diff --git a/server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerTest.scala b/server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerTest.scala index c031778cd5..7587f39fdc 100644 --- a/server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerTest.scala +++ b/server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerTest.scala @@ -25,15 +25,11 @@ class PlayServerTest extends TestSuite { val interpreter = new PlayTestServerInterpreter()(actorSystem) val createServerTest = new DefaultCreateServerTest(backend, interpreter) - new ServerBasicTests( - createServerTest, - interpreter, - multipleValueHeaderSupport = false, - inputStreamSupport = false - ).tests() ++ + new ServerBasicTests(createServerTest, interpreter, multipleValueHeaderSupport = false, inputStreamSupport = false).tests() ++ new ServerFileMultipartTests(createServerTest, multipartInlineHeaderSupport = false).tests() new ServerAuthenticationTests(createServerTest).tests() ++ - new ServerMetricsTest(createServerTest).tests() + new ServerMetricsTest(createServerTest).tests() ++ + new PlayServerWithContextTest(backend).tests() } } } diff --git a/server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerWithContextTest.scala b/server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerWithContextTest.scala new file mode 100644 index 0000000000..444f1fa377 --- /dev/null +++ b/server/play-server/src/test/scala/sttp/tapir/server/play/PlayServerWithContextTest.scala @@ -0,0 +1,38 @@ +package sttp.tapir.server.play + +import akka.actor.ActorSystem +import cats.effect.IO +import org.scalatest.matchers.should.Matchers._ +import play.api.Mode +import play.api.routing.Router +import play.core.server.{DefaultAkkaHttpServerComponents, ServerConfig} +import sttp.client3._ +import sttp.tapir._ +import sttp.tapir.tests.Test + +import scala.concurrent.Future + +class PlayServerWithContextTest(backend: SttpBackend[IO, Any])(implicit _actorSystem: ActorSystem) { + import _actorSystem.dispatcher + + def tests(): List[Test] = List( + Test("server with play.http.context set") { + val e = endpoint.get.in("hello").out(stringBody).serverLogic[Future](_ => Future.successful(Right("world"))) + val components = new DefaultAkkaHttpServerComponents { + override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test) + override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher)) + override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test") + } + val s = components.server + val r = Future.successful(()).flatMap { _ => + basicRequest + .get(uri"http://localhost:${s.mainAddress.getPort}/test/hello") + .send(backend) + .map(_.body shouldBe Right("world")) + .unsafeToFuture() + } + r.onComplete(_ => s.stop()) + r + } + ) +}