Skip to content

Commit da5bdf1

Browse files
authored
Return 404 for unknown path prefixes (#41)
1 parent bd0b414 commit da5bdf1

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import io.grpc.*
77
import io.grpc.MethodDescriptor.MethodType
88
import io.grpc.stub.MetadataUtils
99
import org.http4s.dsl.Http4sDsl
10-
import org.http4s.{MediaType, MessageFailure, Method, Response}
10+
import org.http4s.{Header, MediaType, MessageFailure, Method, Response}
1111
import org.ivovk.connect_rpc_scala.Mappings.*
1212
import org.ivovk.connect_rpc_scala.grpc.{MethodName, MethodRegistry}
1313
import org.ivovk.connect_rpc_scala.http.Headers.`X-Test-Case-Name`

core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectRouteBuilder.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ case class ConnectRouteBuilder[F[_] : Async] private(
110110
)
111111

112112
HttpRoutes.of[F] {
113-
case req@Method.GET -> pathPrefix / serviceName / methodName :? EncodingQP(contentType) +& MessageQP(message) =>
113+
case req@Method.GET -> `pathPrefix` / serviceName / methodName :? EncodingQP(contentType) +& MessageQP(message) =>
114114
val grpcMethod = MethodName(serviceName, methodName)
115115
val entity = RequestEntity[F](message, req.headers)
116116

117117
handler.handle(Method.GET, contentType.some, entity, grpcMethod)
118-
case req@Method.POST -> pathPrefix / serviceName / methodName =>
118+
case req@Method.POST -> `pathPrefix` / serviceName / methodName =>
119119
val grpcMethod = MethodName(serviceName, methodName)
120120
val contentType = req.contentType.map(_.mediaType)
121121
val entity = RequestEntity[F](req)

core/src/test/scala/org/ivovk/connect_rpc_scala/HttpTest.scala

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.ivovk.connect_rpc_scala
22

33
import cats.effect.*
44
import cats.effect.unsafe.implicits.global
5-
import cats.syntax.all.*
65
import org.http4s.client.Client
76
import org.http4s.dsl.io.Root
87
import org.http4s.headers.`Content-Type`
@@ -46,12 +45,11 @@ class HttpTest extends AnyFunSuite, Matchers {
4645
)
4746
}
4847
.use { response =>
49-
for {
48+
for
5049
body <- response.as[String]
51-
status <- response.status.pure[IO]
52-
} yield {
50+
yield {
5351
assert(body == """{"sum":3}""")
54-
assert(status == Status.Ok)
52+
assert(response.status == Status.Ok)
5553
assert(response.headers.get[`Content-Type`].map(_.mediaType).contains(MediaTypes.`application/json`))
5654
}
5755
}
@@ -78,12 +76,11 @@ class HttpTest extends AnyFunSuite, Matchers {
7876
)
7977
}
8078
.use { response =>
81-
for {
79+
for
8280
body <- response.as[String]
83-
status <- response.status.pure[IO]
84-
} yield {
81+
yield {
8582
assert(body == """{"value":"Key is: 123"}""")
86-
assert(status == Status.Ok)
83+
assert(response.status == Status.Ok)
8784
assert(response.headers.get[`Content-Type`].map(_.mediaType).contains(MediaTypes.`application/json`))
8885
}
8986
}
@@ -104,13 +101,36 @@ class HttpTest extends AnyFunSuite, Matchers {
104101
.withEntity(""" { "a": 1, "b": 2} """)
105102
)
106103
}
104+
.use { response =>
105+
for
106+
body <- response.as[String]
107+
yield {
108+
assert(body == """{"sum":3}""")
109+
assert(response.status == Status.Ok)
110+
}
111+
}
112+
.unsafeRunSync()
113+
}
114+
115+
test("return 404 on unknown prefix") {
116+
val service = TestService.bindService(TestServiceImpl, ExecutionContext.global)
117+
118+
ConnectRouteBuilder.forService[IO](service)
119+
.withPathPrefix(Root / "connect")
120+
.build
121+
.flatMap { app =>
122+
val client = Client.fromHttpApp(app)
123+
124+
client.run(
125+
Request[IO](Method.POST, uri"/api/org.ivovk.connect_rpc_scala.test.TestService/Add")
126+
.withEntity(""" { "a": 1, "b": 2} """)
127+
)
128+
}
107129
.use { response =>
108130
for {
109131
body <- response.as[String]
110-
status <- response.status.pure[IO]
111132
} yield {
112-
assert(body == """{"sum":3}""")
113-
assert(status == Status.Ok)
133+
assert(response.status == Status.NotFound)
114134
}
115135
}
116136
.unsafeRunSync()

0 commit comments

Comments
 (0)