Skip to content

Commit

Permalink
Merge pull request #75 from softwaremill/ComposeSSE
Browse files Browse the repository at this point in the history
Add composeSSE to ServerSentEvent model
  • Loading branch information
adamw authored Mar 16, 2021
2 parents cebf276 + f72cb19 commit 3cf084d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
22 changes: 15 additions & 7 deletions core/src/main/scala/sttp/model/sse/ServerSentEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package sttp.model.sse
import scala.util.Try

case class ServerSentEvent(
data: Option[String] = None,
eventType: Option[String] = None,
id: Option[String] = None,
retry: Option[Int] = None
)
data: Option[String] = None,
eventType: Option[String] = None,
id: Option[String] = None,
retry: Option[Int] = None
)

object ServerSentEvent {
// https://html.spec.whatwg.org/multipage/server-sent-events.html
Expand All @@ -24,10 +24,18 @@ object ServerSentEvent {
}
}

def composeSSE(sse: ServerSentEvent): String = {
val data = sse.data.map(_.split("\n")).map(_.map(line => Some(s"data: $line"))).getOrElse(Array.empty[Option[String]])
val event = sse.eventType.map(event => s"event: $event")
val id = sse.id.map(id => s"id: $id")
val retry = sse.retry.map(retryCount => s"retry: $retryCount")
(data :+ event :+ id :+ retry).flatten.mkString("\n")
}

private def combineData(event: ServerSentEvent, newData: String): ServerSentEvent = {
event match {
case e @ ServerSentEvent(Some(oldData), _, _, _) => e.copy(data = Some(s"$oldData\n$newData"))
case e @ ServerSentEvent(None, _, _, _) => e.copy(data = Some(newData))
case e@ServerSentEvent(Some(oldData), _, _, _) => e.copy(data = Some(s"$oldData\n$newData"))
case e@ServerSentEvent(None, _, _, _) => e.copy(data = Some(newData))
}
}

Expand Down
34 changes: 34 additions & 0 deletions core/src/test/scala/sttp/model/sse/ServerSentEventTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,38 @@ class ServerSentEventTest extends AnyFlatSpec with Matchers {
ServerSentEvent.parse(lines) shouldBe expected
}
}


"composeSSE" should "successfully serialise Server Sent Event with all fields set" in {
val sse = ServerSentEvent(Some("data"), Some("event"), Some("id1"), Some(10))
ServerSentEvent.composeSSE(sse) shouldBe
s"""data: data
|event: event
|id: id1
|retry: 10""".stripMargin
}

"composeSSE" should "omit fields that are not set" in {
val sse = ServerSentEvent(Some("data"), None, Some("id1"), None)
ServerSentEvent.composeSSE(sse) shouldBe
s"""data: data
|id: id1""".stripMargin
}

"composeSSE" should "successfully serialise multiline data event" in {
val sse = ServerSentEvent(
Some(
"""some data info 1
|some data info 2
|some data info 3""".stripMargin),
None,
None,
None
)

ServerSentEvent.composeSSE(sse) shouldBe
s"""data: some data info 1
|data: some data info 2
|data: some data info 3""".stripMargin
}
}

0 comments on commit 3cf084d

Please sign in to comment.