Skip to content

Commit

Permalink
softwaremill#1400: fix play server interpreter when routes are put in…
Browse files Browse the repository at this point in the history
… context

(cherry picked from commit 343995a)
  • Loading branch information
adamw authored and Sven Behrens committed Aug 10, 2021
1 parent 07403b5 commit b53b5b8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
)
}

0 comments on commit b53b5b8

Please sign in to comment.