#!/usr/bin/env python
"""
open/DurusWorks/qp/hub/test/utest_web.py

"""
from durus.utils import BytesIO, as_bytes
from qp.hub.web import ns_read_size, ns_reads, read_env
from sancho.utest import UTest, raises

class TestNetstring(UTest):

    def a(self):
        input = BytesIO()
        size = ns_read_size(input)
        assert size == None

    def b(self):
        input = BytesIO(as_bytes('4:0000,'))
        size = ns_read_size(input)
        assert size == 4, size

    def b2(self):
        input = BytesIO(as_bytes('42:0000,'))
        size = ns_read_size(input)
        assert size == 42, size

    def c(self):
        input = BytesIO(as_bytes('40000,'))
        raises(IOError, ns_read_size, input)

    def c(self):
        input = BytesIO(as_bytes('x40000:,'))
        raises(ValueError, ns_read_size, input)

    def d(self):
        input = BytesIO()
        result = ns_reads(input)
        assert result == None, result

    def e(self):
        input = BytesIO(as_bytes('4:0000,'))
        result = ns_reads(input)
        assert result == as_bytes('0000'), result

    def f(self):
        input = BytesIO(as_bytes('4:000,'))
        raises(IOError, ns_reads, input)

    def g(self):
        input = BytesIO(as_bytes('4:'))
        raises(IOError, ns_reads, input)

    def h(self):
        input = BytesIO(as_bytes('4:0000'))
        raises(IOError, ns_reads, input)

    def k(self):
        input = BytesIO(as_bytes('4:0000000'))
        raises(IOError, ns_reads, input)

    def l(self):
        input = BytesIO(as_bytes('4:0000,000'))
        result = ns_reads(input)
        assert result == as_bytes('0000'), result

    def m(self):
        input = BytesIO(as_bytes('8:a\0b\0c\0d\0,asdfasdf'))
        result = read_env(input)
        assert result == dict(a='b', c='d')

if __name__ == '__main__':
    TestNetstring()
