2016-03-20 18:40:03 +00:00
|
|
|
from six.moves import cStringIO as StringIO
|
2016-02-16 19:59:33 +00:00
|
|
|
from pathod import language
|
|
|
|
from pathod.language import writer
|
2015-05-02 05:10:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_send_chunk():
|
|
|
|
v = "foobarfoobar"
|
|
|
|
for bs in range(1, len(v) + 2):
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.send_chunk(s, v, bs, 0, len(v))
|
|
|
|
assert s.getvalue() == v
|
|
|
|
for start in range(len(v)):
|
|
|
|
for end in range(len(v)):
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.send_chunk(s, v, bs, start, end)
|
|
|
|
assert s.getvalue() == v[start:end]
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_values_inject():
|
|
|
|
tst = "foo"
|
|
|
|
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(s, [tst], [(0, "inject", "aaa")], blocksize=5)
|
|
|
|
assert s.getvalue() == "aaafoo"
|
|
|
|
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(s, [tst], [(1, "inject", "aaa")], blocksize=5)
|
|
|
|
assert s.getvalue() == "faaaoo"
|
|
|
|
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(s, [tst], [(1, "inject", "aaa")], blocksize=5)
|
|
|
|
assert s.getvalue() == "faaaoo"
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_values_disconnects():
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
tst = "foo" * 100
|
|
|
|
writer.write_values(s, [tst], [(0, "disconnect")], blocksize=5)
|
|
|
|
assert not s.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_values():
|
|
|
|
tst = "foobarvoing"
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(s, [tst], [])
|
|
|
|
assert s.getvalue() == tst
|
|
|
|
|
|
|
|
for bs in range(1, len(tst) + 2):
|
|
|
|
for off in range(len(tst)):
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(
|
|
|
|
s, [tst], [(off, "disconnect")], blocksize=bs
|
|
|
|
)
|
|
|
|
assert s.getvalue() == tst[:off]
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_values_pauses():
|
|
|
|
tst = "".join(str(i) for i in range(10))
|
|
|
|
for i in range(2, 10):
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(
|
|
|
|
s, [tst], [(2, "pause", 0), (1, "pause", 0)], blocksize=i
|
|
|
|
)
|
|
|
|
assert s.getvalue() == tst
|
|
|
|
|
|
|
|
for i in range(2, 10):
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(s, [tst], [(1, "pause", 0)], blocksize=i)
|
|
|
|
assert s.getvalue() == tst
|
|
|
|
|
|
|
|
tst = ["".join(str(i) for i in range(10))] * 5
|
|
|
|
for i in range(2, 10):
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-05-02 05:10:25 +00:00
|
|
|
writer.write_values(s, tst[:], [(1, "pause", 0)], blocksize=i)
|
|
|
|
assert s.getvalue() == "".join(tst)
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_values_after():
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-06-07 04:11:32 +00:00
|
|
|
r = language.parse_pathod("400:da").next()
|
2015-05-02 05:10:25 +00:00
|
|
|
language.serve(r, s, {})
|
|
|
|
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-06-07 04:11:32 +00:00
|
|
|
r = language.parse_pathod("400:pa,0").next()
|
2015-05-02 05:10:25 +00:00
|
|
|
language.serve(r, s, {})
|
|
|
|
|
2016-03-20 18:40:03 +00:00
|
|
|
s = StringIO()
|
2015-06-07 04:11:32 +00:00
|
|
|
r = language.parse_pathod("400:ia,'xx'").next()
|
2015-05-02 05:10:25 +00:00
|
|
|
language.serve(r, s, {})
|
|
|
|
assert s.getvalue().endswith('xx')
|