Skip to content

Commit

Permalink
Return 404 for unknown path prefixes (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-vovk authored Dec 3, 2024
1 parent bd0b414 commit da5bdf1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 32 additions & 12 deletions core/src/test/scala/org/ivovk/connect_rpc_scala/HttpTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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`))
}
}
Expand All @@ -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`))
}
}
Expand All @@ -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()
Expand Down

0 comments on commit da5bdf1

Please sign in to comment.