Esempio n. 1
0
    def test_limited_stream_disconnection(self):
        io = StringIO('A bit of content')

        # disconnect detection on out of bytes
        stream = wsgi.LimitedStream(io, 255)
        with self.assert_raises(ClientDisconnected):
            stream.read()

        # disconnect detection because file close
        io = StringIO('x' * 255)
        io.close()
        stream = wsgi.LimitedStream(io, 255)
        with self.assert_raises(ClientDisconnected):
            stream.read()
Esempio n. 2
0
def test_limited_stream_disconnection():
    io = BytesIO(b'A bit of content')

    # disconnect detection on out of bytes
    stream = wsgi.LimitedStream(io, 255)
    with pytest.raises(ClientDisconnected):
        stream.read()

    # disconnect detection because file close
    io = BytesIO(b'x' * 255)
    io.close()
    stream = wsgi.LimitedStream(io, 255)
    with pytest.raises(ClientDisconnected):
        stream.read()
Esempio n. 3
0
def test_limited_stream_json_load():
    stream = wsgi.LimitedStream(BytesIO(b'{"hello": "test"}'), 17)
    # flask.json adapts bytes to text with TextIOWrapper
    # this expects stream.readable() to exist and return true
    stream = io.TextIOWrapper(io.BufferedReader(stream), 'UTF-8')
    data = json.load(stream)
    assert data == {'hello': 'test'}
Esempio n. 4
0
    def getEnvironForRequest(self, jsonByteString, httpRequestMethod):
        environ = self.environGetData
        if httpRequestMethod == "POST":
            environ = self.environPostData
        elif httpRequestMethod == "GET":
            environ = self.environGetData

        contentLength = len(jsonByteString)
        stream = io.BytesIO(jsonByteString)
        environ["wsgi.input"] = wsgi.LimitedStream(stream, contentLength)
        environ["CONTENT_LENGTH"] = contentLength
        return environ
Esempio n. 5
0
    def test_limited_stream(self):
        class RaisingLimitedStream(wsgi.LimitedStream):
            def on_exhausted(self):
                raise BadRequest('input stream exhausted')

        io = StringIO('123456')
        stream = RaisingLimitedStream(io, 3)
        self.assert_equal(stream.read(), '123')
        self.assert_raises(BadRequest, stream.read)

        io = StringIO('123456')
        stream = RaisingLimitedStream(io, 3)
        self.assert_equal(stream.tell(), 0)
        self.assert_equal(stream.read(1), '1')
        self.assert_equal(stream.tell(), 1)
        self.assert_equal(stream.read(1), '2')
        self.assert_equal(stream.tell(), 2)
        self.assert_equal(stream.read(1), '3')
        self.assert_equal(stream.tell(), 3)
        self.assert_raises(BadRequest, stream.read)

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        self.assert_equal(stream.readline(), '123456\n')
        self.assert_equal(stream.readline(), 'ab')

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        self.assert_equal(stream.readlines(), ['123456\n', 'ab'])

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        self.assert_equal(stream.readlines(2), ['12'])
        self.assert_equal(stream.readlines(2), ['34'])
        self.assert_equal(stream.readlines(), ['56\n', 'ab'])

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        self.assert_equal(stream.readline(100), '123456\n')

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        self.assert_equal(stream.readlines(100), ['123456\n', 'ab'])

        io = StringIO('123456')
        stream = wsgi.LimitedStream(io, 3)
        self.assert_equal(stream.read(1), '1')
        self.assert_equal(stream.read(1), '2')
        self.assert_equal(stream.read(), '3')
        self.assert_equal(stream.read(), '')

        io = StringIO('123456')
        stream = wsgi.LimitedStream(io, 3)
        self.assert_equal(stream.read(-1), '123')
Esempio n. 6
0
    def test_limited_stream(self):
        class RaisingLimitedStream(wsgi.LimitedStream):
            def on_exhausted(self):
                raise BadRequest('input stream exhausted')

        io = StringIO('123456')
        stream = RaisingLimitedStream(io, 3)
        assert stream.read() == '123'
        self.assert_raises(BadRequest, stream.read)

        io = StringIO('123456')
        stream = RaisingLimitedStream(io, 3)
        assert stream.read(1) == '1'
        assert stream.read(1) == '2'
        assert stream.read(1) == '3'
        self.assert_raises(BadRequest, stream.read)

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        assert stream.readline() == '123456\n'
        assert stream.readline() == 'ab'

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        assert stream.readlines() == ['123456\n', 'ab']

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        assert stream.readlines(2) == ['12']
        assert stream.readlines(2) == ['34']
        assert stream.readlines() == ['56\n', 'ab']

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        assert stream.readline(100) == '123456\n'

        io = StringIO('123456\nabcdefg')
        stream = wsgi.LimitedStream(io, 9)
        assert stream.readlines(100) == ['123456\n', 'ab']

        io = StringIO('123456')
        stream = wsgi.LimitedStream(io, 3)
        assert stream.read(1) == '1'
        assert stream.read(1) == '2'
        assert stream.read() == '3'
        assert stream.read() == ''

        io = StringIO('123456')
        stream = wsgi.LimitedStream(io, 3)
        assert stream.read(-1) == '123'
Esempio n. 7
0
def test_limited_stream():
    class RaisingLimitedStream(wsgi.LimitedStream):
        def on_exhausted(self):
            raise BadRequest('input stream exhausted')

    io = BytesIO(b'123456')
    stream = RaisingLimitedStream(io, 3)
    strict_eq(stream.read(), b'123')
    pytest.raises(BadRequest, stream.read)

    io = BytesIO(b'123456')
    stream = RaisingLimitedStream(io, 3)
    strict_eq(stream.tell(), 0)
    strict_eq(stream.read(1), b'1')
    strict_eq(stream.tell(), 1)
    strict_eq(stream.read(1), b'2')
    strict_eq(stream.tell(), 2)
    strict_eq(stream.read(1), b'3')
    strict_eq(stream.tell(), 3)
    pytest.raises(BadRequest, stream.read)

    io = BytesIO(b'123456\nabcdefg')
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readline(), b'123456\n')
    strict_eq(stream.readline(), b'ab')

    io = BytesIO(b'123456\nabcdefg')
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readlines(), [b'123456\n', b'ab'])

    io = BytesIO(b'123456\nabcdefg')
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readlines(2), [b'12'])
    strict_eq(stream.readlines(2), [b'34'])
    strict_eq(stream.readlines(), [b'56\n', b'ab'])

    io = BytesIO(b'123456\nabcdefg')
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readline(100), b'123456\n')

    io = BytesIO(b'123456\nabcdefg')
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readlines(100), [b'123456\n', b'ab'])

    io = BytesIO(b'123456')
    stream = wsgi.LimitedStream(io, 3)
    strict_eq(stream.read(1), b'1')
    strict_eq(stream.read(1), b'2')
    strict_eq(stream.read(), b'3')
    strict_eq(stream.read(), b'')

    io = BytesIO(b'123456')
    stream = wsgi.LimitedStream(io, 3)
    strict_eq(stream.read(-1), b'123')

    io = BytesIO(b'123456')
    stream = wsgi.LimitedStream(io, 0)
    strict_eq(stream.read(-1), b'')

    io = StringIO(u'123456')
    stream = wsgi.LimitedStream(io, 0)
    strict_eq(stream.read(-1), u'')

    io = StringIO(u'123\n456\n')
    stream = wsgi.LimitedStream(io, 8)
    strict_eq(list(stream), [u'123\n', u'456\n'])
Esempio n. 8
0
def test_limited_stream():
    class RaisingLimitedStream(wsgi.LimitedStream):
        def on_exhausted(self):
            raise BadRequest("input stream exhausted")

    io_ = io.BytesIO(b"123456")
    stream = RaisingLimitedStream(io_, 3)
    assert stream.read() == b"123"
    pytest.raises(BadRequest, stream.read)

    io_ = io.BytesIO(b"123456")
    stream = RaisingLimitedStream(io_, 3)
    assert stream.tell() == 0
    assert stream.read(1) == b"1"
    assert stream.tell() == 1
    assert stream.read(1) == b"2"
    assert stream.tell() == 2
    assert stream.read(1) == b"3"
    assert stream.tell() == 3
    pytest.raises(BadRequest, stream.read)

    io_ = io.BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io_, 9)
    assert stream.readline() == b"123456\n"
    assert stream.readline() == b"ab"

    io_ = io.BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io_, 9)
    assert stream.readlines() == [b"123456\n", b"ab"]

    io_ = io.BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io_, 9)
    assert stream.readlines(2) == [b"12"]
    assert stream.readlines(2) == [b"34"]
    assert stream.readlines() == [b"56\n", b"ab"]

    io_ = io.BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io_, 9)
    assert stream.readline(100) == b"123456\n"

    io_ = io.BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io_, 9)
    assert stream.readlines(100) == [b"123456\n", b"ab"]

    io_ = io.BytesIO(b"123456")
    stream = wsgi.LimitedStream(io_, 3)
    assert stream.read(1) == b"1"
    assert stream.read(1) == b"2"
    assert stream.read() == b"3"
    assert stream.read() == b""

    io_ = io.BytesIO(b"123456")
    stream = wsgi.LimitedStream(io_, 3)
    assert stream.read(-1) == b"123"

    io_ = io.BytesIO(b"123456")
    stream = wsgi.LimitedStream(io_, 0)
    assert stream.read(-1) == b""

    io_ = io.StringIO("123456")
    stream = wsgi.LimitedStream(io_, 0)
    assert stream.read(-1) == ""

    io_ = io.StringIO("123\n456\n")
    stream = wsgi.LimitedStream(io_, 8)
    assert list(stream) == ["123\n", "456\n"]
Esempio n. 9
0
def test_limited_stream():
    class RaisingLimitedStream(wsgi.LimitedStream):
        def on_exhausted(self):
            raise BadRequest("input stream exhausted")

    io = BytesIO(b"123456")
    stream = RaisingLimitedStream(io, 3)
    strict_eq(stream.read(), b"123")
    pytest.raises(BadRequest, stream.read)

    io = BytesIO(b"123456")
    stream = RaisingLimitedStream(io, 3)
    strict_eq(stream.tell(), 0)
    strict_eq(stream.read(1), b"1")
    strict_eq(stream.tell(), 1)
    strict_eq(stream.read(1), b"2")
    strict_eq(stream.tell(), 2)
    strict_eq(stream.read(1), b"3")
    strict_eq(stream.tell(), 3)
    pytest.raises(BadRequest, stream.read)

    io = BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readline(), b"123456\n")
    strict_eq(stream.readline(), b"ab")

    io = BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readlines(), [b"123456\n", b"ab"])

    io = BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readlines(2), [b"12"])
    strict_eq(stream.readlines(2), [b"34"])
    strict_eq(stream.readlines(), [b"56\n", b"ab"])

    io = BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readline(100), b"123456\n")

    io = BytesIO(b"123456\nabcdefg")
    stream = wsgi.LimitedStream(io, 9)
    strict_eq(stream.readlines(100), [b"123456\n", b"ab"])

    io = BytesIO(b"123456")
    stream = wsgi.LimitedStream(io, 3)
    strict_eq(stream.read(1), b"1")
    strict_eq(stream.read(1), b"2")
    strict_eq(stream.read(), b"3")
    strict_eq(stream.read(), b"")

    io = BytesIO(b"123456")
    stream = wsgi.LimitedStream(io, 3)
    strict_eq(stream.read(-1), b"123")

    io = BytesIO(b"123456")
    stream = wsgi.LimitedStream(io, 0)
    strict_eq(stream.read(-1), b"")

    io = StringIO(u"123456")
    stream = wsgi.LimitedStream(io, 0)
    strict_eq(stream.read(-1), u"")

    io = StringIO(u"123\n456\n")
    stream = wsgi.LimitedStream(io, 8)
    strict_eq(list(stream), [u"123\n", u"456\n"])