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
|
2022-06-10 11:30:51 +00:00
|
|
|
../../waku/v2/node/rest/serdes,
|
2022-08-29 14:54:11 +00:00
|
|
|
../../waku/v2/node/rest/base64,
|
2023-02-13 14:22:24 +00:00
|
|
|
../../waku/v2/node/rest/relay/types,
|
2022-11-09 14:00:11 +00:00
|
|
|
../../waku/v2/protocol/waku_message
|
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
|
2022-08-29 14:54:11 +00:00
|
|
|
let payload = Base64String.encode("MESSAGE")
|
|
|
|
let jsonBytes = toBytes("{\"payload\":\"" & $payload & "\"}")
|
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
|
|
|
|
value.contentTopic.isNone()
|
|
|
|
value.version.isNone()
|
|
|
|
value.timestamp.isNone()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
suite "RelayWakuMessage - encode":
|
|
|
|
test "optional fields are none":
|
|
|
|
# Given
|
2022-08-29 14:54:11 +00:00
|
|
|
let payload = Base64String.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),
|
|
|
|
timestamp: none(int64)
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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 & "\"}")
|