Commit c82f1cb2 authored by Robin Obůrka's avatar Robin Obůrka

Add some input checks for encode_msg and add some tests for it

parent 455c8d12
......@@ -31,6 +31,13 @@ def encode_msg(msg_type, data):
""" Gets string message type and its's string data. Then, both of them are
packed to be prepared for zmg.send_multipart().
"""
if not SN_MSG.match(msg_type or ""):
raise InvalidMsgTypeError("Bad message type definition")
if not data or type(data) != dict:
raise InvalidMsgError("Empty payload parameter")
b = bytes(msg_type, encoding="UTF-8")
msg = msgpack.packb(data)
......
import pytest
import sn
def test_encoding(good_type, good_payload, good_msg):
encoded = sn.encode_msg(good_type, good_payload)
assert encoded == good_msg
def test_empty_type(good_payload):
with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(None, good_payload)
def test_empty_payload(good_type):
with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(good_type, None)
def test_bad_payload(good_type):
with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(good_type, ("I", "am", "a", "tuple"))
def test_good_type(good_types, good_payload):
# I mean: "Not raise any exception"
(t, p) = sn.encode_msg(good_types, good_payload)
def test_invalid_type(bad_types, good_payload):
with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.encode_msg(bad_types, good_payload)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment