async def test_api_async_nego_accept(cli): res = await cli.post( "/path/to/identity", headers=[ ("Accept", "application/x-msgpack"), ("Content-Type", "application/x-msgpack"), ], data=msgpack_dumps({"data": "toto"}), ) assert res.status == 200 assert res.content_type == "application/x-msgpack" assert await res.read() == msgpack_dumps("toto")
async def test_api_async_rpc_server(cli): res = await cli.get( "/test_endpoint_url", headers=[ ("Content-Type", "application/x-msgpack"), ("Accept", "application/x-msgpack"), ], data=msgpack_dumps({"test_data": "spam"}), ) assert res.status == 200 assert res.content_type == "application/x-msgpack" assert await res.read() == msgpack_dumps("egg")
def test_complex_exception_serializer_round_trip_msgpack(): exception = ComplexExceptionType("NotFound", "the object is missing") data = msgpack_dumps({"exception": exception}) actual_data = msgpack_loads(data) assert "exception" in actual_data assert type(actual_data["exception"]) == Exception assert str(actual_data["exception"]) == str(exception)
def test_exception_serializer_round_trip_msgpack(): error_message = "unreachable host" data = msgpack_dumps({"exception": ConnectionError(error_message)}) actual_data = msgpack_loads(data) assert "exception" in actual_data assert type(actual_data["exception"]) == ConnectionError assert str(actual_data["exception"]) == error_message
def callback(request, context): assert request.headers["Content-Type"] == "application/x-msgpack" context.headers["Content-Type"] = "application/x-msgpack" exc_dict = exception_to_dict(exception) if old_exception_schema: exc_dict = {"exception": exc_dict} context.content = msgpack_dumps(exc_dict) context.status_code = status_code return context.content
async def test_post_struct_msgpack(cli) -> None: """Test that msgpack encoded posted struct data is returned as is""" # simple struct resp = await cli.post( "/echo", headers={"Content-Type": "application/x-msgpack"}, data=msgpack_dumps({"toto": 42}), ) assert resp.status == 200 check_mimetype(resp.headers["Content-Type"], "application/x-msgpack") assert (await decode_request(resp)) == {"toto": 42} # complex struct resp = await cli.post( "/echo", headers={"Content-Type": "application/x-msgpack"}, data=msgpack_dumps(STRUCT), ) assert resp.status == 200 check_mimetype(resp.headers["Content-Type"], "application/x-msgpack") assert (await decode_request(resp)) == STRUCT
def test_serializers_round_trip_msgpack(): expected_original_data = { **DATA, "none_dict_key": { None: 42 }, "long_int_is_loooong": 10000000000000000000000000000000, "long_negative_int_is_loooong": -10000000000000000000000000000000, } data = msgpack_dumps(expected_original_data) actual_data = msgpack_loads(data) assert actual_data == expected_original_data
def test_msgpack_extra_encoders_mutation(): data = msgpack_dumps({}, extra_encoders=extra_encoders) assert data is not None assert ENCODERS[-1][0] != ExtraType
def test_serializers_encode_native_datetime_msgpack(): dt = datetime.datetime(2015, 1, 1, 12, 4, 42, 231455) with pytest.raises(TypeError, match="datetime"): msgpack_dumps(dt)
def test_serializers_decode_datetime_compat_msgpack(): dt = datetime.datetime.now(tz=datetime.timezone.utc) encmsg = msgpack_dumps({b"swhtype": "datetime", b"d": dt.isoformat()}) decmsg = msgpack_loads(encmsg) assert decmsg == dt
def test_serializers_encode_datetime_msgpack(): dt = datetime.datetime.now(tz=datetime.timezone.utc) encmsg = msgpack_dumps(dt) decmsg = msgpack.loads(encmsg, timestamp=0) assert isinstance(decmsg, msgpack.Timestamp) assert decmsg.to_datetime() == dt
def test_serializers_generator_msgpack(): data = msgpack_dumps((i for i in range(5))) assert msgpack_loads(data) == [i for i in range(5)]
def test_serializers_round_trip_msgpack_extra_types(): original_data = [ExtraType("baz", DATA), "qux"] data = msgpack_dumps(original_data, extra_encoders=extra_encoders) actual_data = msgpack_loads(data, extra_decoders=extra_decoders) assert actual_data == original_data