I have some tapir endpoints defined as part of a zio-http server. Everything works for real, including those POST endpoints with JSON bodies.
However, I've been unable to get a unit test using SttpBackendStub & TapirStubInterpreter to work for endpoints with a JSON body - if I remove the body from the endpoint and test request, it works fine. With the body in place, I get a 404, with no other error info.
The endpoint is defined like:
case class Payload(one: String, two: String)
@endpointInput("accounts/{id}/test-request")
final case class RequestInput(
@path
id: String,
@header("Source-Event-Timestamp")
sourceEventTimestamp: LocalDateTime,
@header("Accept")
accept: String,
@jsonbody
payload: Payload
)
val input = EndpointInput.derived[RequestInput]
baseEndpoint.post.in(input)
I also experimented with using
.in(jsonBody[Input])
along with corresponding zio-json encoder & decoder, which surprisingly results in a different failure - a 400 with the message "Invalid value for: body."
Here is the relevant test code snippet (this is a ZIO spec):
stub = TapirStubInterpreter(SttpBackendStub(new RIOMonadAsyncError[Any]))
.whenServerEndpoint(endpoint)
.thenRunLogic()
.backend()
body =
"""
{
"one": "",
"two": ""
}
""".stripMargin
response <- basicRequest
.contentType("application/json")
.body(body)
.post(uri"http://test.com/test-request")
.send(stub)
I've tried every permutation of calls/methods I can think of and just can't get the test to work for an endpoint with a body - again, bear in mind the real server does work with the exact same JSON body input.