softwaremill / tapir

Rapid development of self-documenting APIs
https://tapir.softwaremill.com
Apache License 2.0
1.37k stars 420 forks source link

Testing Server Sent Events #2607

Open oltyant opened 1 year ago

oltyant commented 1 year ago

Hello,

I have a bunch of plain http server endpoints which works fine but one of them I needed to change to give back serverSentEventsBody so this is the only endpoint that gives back Stream[Throwable, ServerSentEvent]. I am using ZIO 2 and so I am using ZStream in the implementation. Now when we test the endpoints we use the entire list of routes: val wsRoutes = Router("/" -> allRoutes).orNotFound In a similar way:

type RouteTestType[A] = ZIO[ZServer.Env, Throwable, A]
for {
            req         <- ZIO.succeed(
                             getRequest[RouteTestType](
                               Method.PATCH,
                               s"/v1/projects/pause/$Project1",
                               Some(testNewAdminContext)
                             )
                           )
            result    <- wsRoutes.run(req)
} yield assert(result)(assertionFunctions)

I did not find a way to transform the result: Response[ZIO[Server,Throwable,_]] to a ZStream to test the server sent events. Is there a way to do it? I could not find a solution anywhere. If there is, sorry for raising a ticket for it. If not, will you add this feature to tapir?

adamw commented 1 year ago

As far as I can see, you are simulating a GET on the endpoint. If you want to test streaming behavior, I guess you'd need to simulate receiving a streaming response. Which should give you a Response[ZStream[...]] or sth similar