From 48a27182eee3c7bf78a891164af651f9c98559b5 Mon Sep 17 00:00:00 2001 From: dsrees Date: Mon, 12 Jun 2023 14:23:42 -0400 Subject: [PATCH] Do not lose payloadJson in trigger --- .../kotlin/org/phoenixframework/Channel.kt | 12 ++++++---- .../org/phoenixframework/DefaultsTest.kt | 24 ++++++++++++++++++- .../org/phoenixframework/MessageTest.kt | 4 ++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/org/phoenixframework/Channel.kt b/src/main/kotlin/org/phoenixframework/Channel.kt index dd594f3..0794cfb 100644 --- a/src/main/kotlin/org/phoenixframework/Channel.kt +++ b/src/main/kotlin/org/phoenixframework/Channel.kt @@ -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) } } @@ -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) { diff --git a/src/test/kotlin/org/phoenixframework/DefaultsTest.kt b/src/test/kotlin/org/phoenixframework/DefaultsTest.kt index f26290c..bf2da9b 100644 --- a/src/test/kotlin/org/phoenixframework/DefaultsTest.kt +++ b/src/test/kotlin/org/phoenixframework/DefaultsTest.kt @@ -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) diff --git a/src/test/kotlin/org/phoenixframework/MessageTest.kt b/src/test/kotlin/org/phoenixframework/MessageTest.kt index 0a555d0..2dcd321 100644 --- a/src/test/kotlin/org/phoenixframework/MessageTest.kt +++ b/src/test/kotlin/org/phoenixframework/MessageTest.kt @@ -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) @@ -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)