-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1b3a2a
commit c9a9c39
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
effects/ox/src/main/scala/sttp/client4/impl/ox/sse/OxServerSentEvents.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sttp.client4.impl.ox.sse | ||
|
||
import ox.* | ||
import ox.channels.Source | ||
import sttp.model.sse.ServerSentEvent | ||
|
||
import java.io.InputStream | ||
|
||
object OxServerSentEvents: | ||
def parse(is: InputStream)(using Ox, IO): Source[ServerSentEvent] = | ||
Source | ||
.fromInputStream(is) | ||
.linesUtf8 | ||
.mapStatefulConcat(() => List.empty[String])( | ||
(lines, str) => if str.isEmpty then (Nil, List(lines)) else (lines :+ str, Nil), | ||
onComplete = { lines => | ||
if lines.nonEmpty then Some(lines) | ||
else None | ||
} | ||
) | ||
.filter(_.nonEmpty) | ||
.map(ServerSentEvent.parse) |
35 changes: 35 additions & 0 deletions
35
effects/ox/src/test/scala/sttp/client4/impl/ox/ws/OxSseTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sttp.client4.impl.ox.sse | ||
|
||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
import ox.IO.globalForTesting.given | ||
import sttp.client4.* | ||
import sttp.client4.testing.HttpTest.* | ||
import sttp.model.sse.ServerSentEvent | ||
|
||
class OxServerSentEventsTest extends AnyFlatSpec with Matchers with BeforeAndAfterAll: | ||
|
||
lazy val backend: WebSocketSyncBackend = DefaultSyncBackend() | ||
|
||
behavior of "OxServerSentEvents" | ||
|
||
it should "parse SSEs" in supervised { | ||
val sseData = "text1 in line1\ntext2 in line2" | ||
val expectedEvent = ServerSentEvent(data = Some(sseData), eventType = Some("test-event"), retry = Some(42000)) | ||
val expectedEvents = | ||
Seq(expectedEvent.copy(id = Some("1")), expectedEvent.copy(id = Some("2")), expectedEvent.copy(id = Some("3"))) | ||
basicRequest | ||
.post(uri"$endpoint/sse/echo3") | ||
.body(sseData) | ||
.response(asInputStreamAlways { is => | ||
OxServerSentEvents.parse(is).take(3).toList shouldBe(expectedEvents) | ||
() | ||
}) | ||
.send(backend) | ||
} | ||
|
||
override protected def afterAll(): Unit = | ||
backend.close() | ||
super.afterAll() |