2022-06-10 11:30:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
2022-08-29 14:54:11 +00:00
|
|
|
import
|
2022-06-10 11:30:51 +00:00
|
|
|
stew/[results, byteutils],
|
2022-08-29 14:54:11 +00:00
|
|
|
chronicles,
|
|
|
|
unittest2,
|
2022-06-10 11:30:51 +00:00
|
|
|
json_serialization
|
2023-02-13 10:43:49 +00:00
|
|
|
import
|
2023-02-23 08:31:06 +00:00
|
|
|
../../waku/common/base64,
|
2023-09-22 13:36:46 +00:00
|
|
|
../../waku/waku_api/rest/serdes,
|
|
|
|
../../waku/waku_api/rest/relay/types,
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/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")
|
2023-09-22 11:46:55 +00:00
|
|
|
let jsonBytes = toBytes("{\"payload\":\"" & $payload & "\",\"contentTopic\":\"some/topic\"}")
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# When
|
|
|
|
let res = decodeFromJsonBytes(RelayWakuMessage, jsonBytes, requireAllFields = true)
|
|
|
|
|
|
|
|
# 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),
|
|
|
|
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 & "\"}")
|