diff --git a/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectHandler.scala b/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectHandler.scala index 3abdafe..bf59e41 100644 --- a/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectHandler.scala +++ b/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectHandler.scala @@ -7,7 +7,7 @@ import io.grpc.* import io.grpc.MethodDescriptor.MethodType import io.grpc.stub.MetadataUtils import org.http4s.dsl.Http4sDsl -import org.http4s.{MediaType, MessageFailure, Method, Response} +import org.http4s.{Header, MediaType, MessageFailure, Method, Response} import org.ivovk.connect_rpc_scala.Mappings.* import org.ivovk.connect_rpc_scala.grpc.{MethodName, MethodRegistry} import org.ivovk.connect_rpc_scala.http.Headers.`X-Test-Case-Name` diff --git a/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectRouteBuilder.scala b/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectRouteBuilder.scala index 3aff336..9a726f2 100644 --- a/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectRouteBuilder.scala +++ b/core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectRouteBuilder.scala @@ -110,12 +110,12 @@ case class ConnectRouteBuilder[F[_] : Async] private( ) HttpRoutes.of[F] { - case req@Method.GET -> pathPrefix / serviceName / methodName :? EncodingQP(contentType) +& MessageQP(message) => + case req@Method.GET -> `pathPrefix` / serviceName / methodName :? EncodingQP(contentType) +& MessageQP(message) => val grpcMethod = MethodName(serviceName, methodName) val entity = RequestEntity[F](message, req.headers) handler.handle(Method.GET, contentType.some, entity, grpcMethod) - case req@Method.POST -> pathPrefix / serviceName / methodName => + case req@Method.POST -> `pathPrefix` / serviceName / methodName => val grpcMethod = MethodName(serviceName, methodName) val contentType = req.contentType.map(_.mediaType) val entity = RequestEntity[F](req) diff --git a/core/src/test/scala/org/ivovk/connect_rpc_scala/HttpTest.scala b/core/src/test/scala/org/ivovk/connect_rpc_scala/HttpTest.scala index c587e79..cee5fc5 100644 --- a/core/src/test/scala/org/ivovk/connect_rpc_scala/HttpTest.scala +++ b/core/src/test/scala/org/ivovk/connect_rpc_scala/HttpTest.scala @@ -2,7 +2,6 @@ package org.ivovk.connect_rpc_scala import cats.effect.* import cats.effect.unsafe.implicits.global -import cats.syntax.all.* import org.http4s.client.Client import org.http4s.dsl.io.Root import org.http4s.headers.`Content-Type` @@ -46,12 +45,11 @@ class HttpTest extends AnyFunSuite, Matchers { ) } .use { response => - for { + for body <- response.as[String] - status <- response.status.pure[IO] - } yield { + yield { assert(body == """{"sum":3}""") - assert(status == Status.Ok) + assert(response.status == Status.Ok) assert(response.headers.get[`Content-Type`].map(_.mediaType).contains(MediaTypes.`application/json`)) } } @@ -78,12 +76,11 @@ class HttpTest extends AnyFunSuite, Matchers { ) } .use { response => - for { + for body <- response.as[String] - status <- response.status.pure[IO] - } yield { + yield { assert(body == """{"value":"Key is: 123"}""") - assert(status == Status.Ok) + assert(response.status == Status.Ok) assert(response.headers.get[`Content-Type`].map(_.mediaType).contains(MediaTypes.`application/json`)) } } @@ -104,13 +101,36 @@ class HttpTest extends AnyFunSuite, Matchers { .withEntity(""" { "a": 1, "b": 2} """) ) } + .use { response => + for + body <- response.as[String] + yield { + assert(body == """{"sum":3}""") + assert(response.status == Status.Ok) + } + } + .unsafeRunSync() + } + + test("return 404 on unknown prefix") { + val service = TestService.bindService(TestServiceImpl, ExecutionContext.global) + + ConnectRouteBuilder.forService[IO](service) + .withPathPrefix(Root / "connect") + .build + .flatMap { app => + val client = Client.fromHttpApp(app) + + client.run( + Request[IO](Method.POST, uri"/api/org.ivovk.connect_rpc_scala.test.TestService/Add") + .withEntity(""" { "a": 1, "b": 2} """) + ) + } .use { response => for { body <- response.as[String] - status <- response.status.pure[IO] } yield { - assert(body == """{"sum":3}""") - assert(status == Status.Ok) + assert(response.status == Status.NotFound) } } .unsafeRunSync()