2022-06-10 11:30:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import stew/[results, byteutils], chronicles, unittest2, json_serialization
|
2024-07-05 22:03:38 +00:00
|
|
|
import waku/[common/base64, waku_api/rest/serdes, waku_api/rest/relay/types, waku_core]
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
suite "Waku v2 Rest API - Relay - serialization":
|
2022-06-10 11:30:51 +00:00
|
|
|
suite "RelayWakuMessage - decode":
|
|
|
|
test "optional fields are not provided":
|
|
|
|
# Given
|
2023-02-23 08:31:06 +00:00
|
|
|
let payload = base64.encode("MESSAGE")
|
2024-03-15 23:08:47 +00:00
|
|
|
let jsonBytes =
|
|
|
|
toBytes("{\"payload\":\"" & $payload & "\",\"contentTopic\":\"some/topic\"}")
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# When
|
2024-03-15 23:08:47 +00:00
|
|
|
let res =
|
|
|
|
decodeFromJsonBytes(RelayWakuMessage, jsonBytes, requireAllFields = true)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Then
|
2022-08-29 14:54:11 +00:00
|
|
|
require(res.isOk())
|
2022-06-10 11:30:51 +00:00
|
|
|
let value = res.get()
|
|
|
|
check:
|
2022-08-29 14:54:11 +00:00
|
|
|
value.payload == payload
|
2023-09-22 11:46:55 +00:00
|
|
|
value.contentTopic.isSome()
|
|
|
|
value.contentTopic.get() == "some/topic"
|
2022-08-29 14:54:11 +00:00
|
|
|
value.version.isNone()
|
|
|
|
value.timestamp.isNone()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
suite "RelayWakuMessage - encode":
|
|
|
|
test "optional fields are none":
|
|
|
|
# Given
|
2023-02-23 08:31:06 +00:00
|
|
|
let payload = base64.encode("MESSAGE")
|
2022-06-10 11:30:51 +00:00
|
|
|
let data = RelayWakuMessage(
|
2022-11-09 14:00:11 +00:00
|
|
|
payload: payload,
|
|
|
|
contentTopic: none(ContentTopic),
|
2022-06-10 11:30:51 +00:00
|
|
|
version: none(Natural),
|
2024-03-13 12:49:21 +00:00
|
|
|
timestamp: none(int64),
|
2024-03-15 23:08:47 +00:00
|
|
|
ephemeral: none(bool),
|
2022-06-10 11:30:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# When
|
|
|
|
let res = encodeIntoJsonBytes(data)
|
|
|
|
|
|
|
|
# Then
|
2022-08-29 14:54:11 +00:00
|
|
|
require(res.isOk())
|
2022-06-10 11:30:51 +00:00
|
|
|
let value = res.get()
|
|
|
|
check:
|
2022-08-29 14:54:11 +00:00
|
|
|
value == toBytes("{\"payload\":\"" & $payload & "\"}")
|