2014-04-03 12:44:00 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-23 13:26:53 -08:00
|
|
|
"""
|
2014-04-03 12:44:00 -07:00
|
|
|
# full seq
|
2016-02-23 13:26:53 -08:00
|
|
|
#>>> print RewriteContent._decode_buff(b'\xce\xb4\xce\xbf\xce\xba', BytesIO(b''), 'utf-8')
|
2014-04-03 12:44:00 -07:00
|
|
|
δοκ
|
|
|
|
|
|
|
|
# read split bytes, read rest
|
2014-12-26 13:08:35 -08:00
|
|
|
#>>> b = BytesIO('\xbf\xce\xba')
|
2016-02-23 13:26:53 -08:00
|
|
|
#>>> sys.stdout.write(RewriteContent._decode_buff(b'\xce\xb4\xce', b, 'utf-8')); sys.stdout.write(RewriteContent._decode_buff(b.read(), b, 'utf-8'))
|
2014-04-03 12:44:00 -07:00
|
|
|
δοκ
|
|
|
|
|
|
|
|
# invalid seq
|
2016-02-23 13:26:53 -08:00
|
|
|
#>>> print RewriteContent._decode_buff(b'\xce\xb4\xce', BytesIO(b'\xfe'), 'utf-8')
|
2014-04-03 12:44:00 -07:00
|
|
|
Traceback (most recent call last):
|
2014-12-26 13:08:35 -08:00
|
|
|
"UnicodeDecodeError: 'utf8' codec can't decode byte 0xce in position 2: invalid continuation byte"
|
|
|
|
|
|
|
|
|
2014-04-03 12:44:00 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
from pywb.rewrite.rewrite_content import RewriteContent
|
|
|
|
from io import BytesIO
|
|
|
|
import sys
|
|
|
|
|
2016-02-23 13:26:53 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_type_detect_1():
|
|
|
|
text_type, stream = RewriteContent._resolve_text_type('js', 'html', BytesIO(b' <html></html>'))
|
|
|
|
assert(text_type == 'html')
|
|
|
|
assert(stream.read() == b' <html></html>')
|
|
|
|
|
|
|
|
|
|
|
|
def test_type_detect_2():
|
|
|
|
text_type, stream = RewriteContent._resolve_text_type('js', 'html', BytesIO(b' function() { return 0; }'))
|
|
|
|
assert(text_type == 'js')
|
|
|
|
assert(stream.read() == b' function() { return 0; }')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-03 12:44:00 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|