Skip to content

Commit

Permalink
Do not lose payloadJson in trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrees committed Jun 12, 2023
1 parent 0a932c6 commit 48a2718
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/main/kotlin/org/phoenixframework/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Channel(

// Perform when the join reply is received
this.on(Event.REPLY) { message ->
this.trigger(replyEventName(message.ref), message.rawPayload, message.ref, message.joinRef)
this.trigger(replyEventName(message.ref), message.rawPayload, message.ref, message.joinRef, message.payloadJson)
}
}

Expand Down Expand Up @@ -383,18 +383,20 @@ class Channel(
event: Event,
payload: Payload = hashMapOf(),
ref: String = "",
joinRef: String? = null
joinRef: String? = null,
payloadJson: String = ""
) {
this.trigger(event.value, payload, ref, joinRef)
this.trigger(event.value, payload, ref, joinRef, payloadJson)
}

internal fun trigger(
event: String,
payload: Payload = hashMapOf(),
ref: String = "",
joinRef: String? = null
joinRef: String? = null,
payloadJson: String = ""
) {
this.trigger(Message(joinRef, ref, topic, event, payload))
this.trigger(Message(joinRef, ref, topic, event, payload, payloadJson))
}

internal fun trigger(message: Message) {
Expand Down
24 changes: 23 additions & 1 deletion src/test/kotlin/org/phoenixframework/DefaultsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,32 @@ internal class DefaultsTest {
))
}



@Test
internal fun `decoder decodes an error`() {
val v2Json = """
["6","8","drivers:self","phx_reply",{"response":{"details":"invalid code specified"},"status":"error"}]
""".trimIndent()

val message = Defaults.decode(v2Json)
assertThat(message.payloadJson).isEqualTo("{\"details\":\"invalid code specified\"}")
assertThat(message.rawPayload).isEqualTo(mapOf(
"response" to mapOf(
"details" to "invalid code specified"
),
"status" to "error"
))
assertThat(message.payload).isEqualTo(mapOf(
"details" to "invalid code specified"
))

}

@Test
internal fun `decoder decodes a non-json payload`() {
val v2Json = """
[1,2,"room:lobby","phx_reply",{"response":"hello","status":"ok"}]
["1","2","room:lobby","phx_reply",{"response":"hello","status":"ok"}]
""".trimIndent()

val message = Defaults.decode(v2Json)
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/org/phoenixframework/MessageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MessageTest {
@Test
internal fun `jsonParsing parses normal message`() {
val json = """
[null, "6", "my-topic", "update", {"user": "James S.", "message": "This is a test"}]
[null,"6","my-topic","update",{"user":"James S.","message":"This is a test"}]
""".trimIndent()

val message = Defaults.decode.invoke(json)
Expand All @@ -30,7 +30,7 @@ class MessageTest {
@Test
internal fun `jsonParsing parses a reply`() {
val json = """
[null, "6", "my-topic", "phx_reply", {"response": {"user": "James S.","message": "This is a test"},"status": "ok"}]
[null,"6","my-topic","phx_reply",{"response":{"user":"James S.","message":"This is a test"},"status": "ok"}]
""".trimIndent()

val message = Defaults.decode.invoke(json)
Expand Down

0 comments on commit 48a2718

Please sign in to comment.