Skip to content

Commit ac525b0

Browse files
committed
tests: add tests for extract_post_query()
add test for HttpsUrlRewriter, remove unnecessary check in bufferedreader
1 parent 8449647 commit ac525b0

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

pywb/rewrite/test/test_url_rewriter.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,17 @@
103103
'http://example.com/file.html?param=https://example.com/filename.html&other=value&a=b&param2=http://test.example.com'
104104
105105
# HttpsUrlRewriter tests
106-
>>> HttpsUrlRewriter('http://example.com/', None).rewrite('https://example.com/abc')
106+
>>> httpsrewriter = HttpsUrlRewriter('http://example.com/', None)
107+
>>> httpsrewriter.rewrite('https://example.com/abc')
107108
'http://example.com/abc'
108109
109-
>>> HttpsUrlRewriter('http://example.com/', None).rewrite('http://example.com/abc')
110+
>>> httpsrewriter.rewrite('http://example.com/abc')
110111
'http://example.com/abc'
111112
113+
# rebase is identity
114+
>>> httpsrewriter.rebase_rewriter('https://example.com/') == httpsrewriter
115+
True
116+
112117
"""
113118

114119

pywb/utils/bufferedreaders.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ def __init__(self, stream, block_size=1024,
4646
self.buff_size = 0
4747

4848
def set_decomp(self, decomp_type):
49-
if self.num_read > 0:
50-
raise Exception('Attempting to change decompression mid-stream')
51-
5249
self._init_decomp(decomp_type)
5350

5451
def _init_decomp(self, decomp_type):

pywb/utils/loaders.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def extract_post_query(method, mime, length, stream):
4949
not mime.lower().startswith('application/x-www-form-urlencoded'))):
5050
return None
5151

52-
if not length or length == '0':
53-
return None
54-
5552
try:
5653
length = int(length)
57-
except ValueError:
54+
except (ValueError, TypeError):
55+
return None
56+
57+
if length <= 0:
5858
return None
5959

6060
#todo: encoding issues?

pywb/utils/test/test_loaders.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@
5656
>>> extract_client_cookie(dict(HTTP_COOKIE='x'), 'x')
5757
5858
>>> extract_client_cookie({}, 'y')
59+
60+
61+
# extract_post_query tests
62+
63+
# correct POST data
64+
>>> post_data = 'foo=bar&dir=%2Fbaz'
65+
>>> extract_post_query('POST', 'application/x-www-form-urlencoded', len(post_data), BytesIO(post_data))
66+
'foo=bar&dir=/baz'
67+
68+
# unsupported method
69+
>>> extract_post_query('PUT', 'application/x-www-form-urlencoded', len(post_data), BytesIO(post_data))
70+
71+
# unsupported type
72+
>>> extract_post_query('POST', 'text/plain', len(post_data), BytesIO(post_data))
73+
74+
# invalid length
75+
>>> extract_post_query('POST', 'application/x-www-form-urlencoded', 'abc', BytesIO(post_data))
76+
>>> extract_post_query('POST', 'application/x-www-form-urlencoded', 0, BytesIO(post_data))
77+
78+
# length too short
79+
>>> extract_post_query('POST', 'application/x-www-form-urlencoded', len(post_data) - 4, BytesIO(post_data))
80+
'foo=bar&dir=%2'
81+
82+
# length too long
83+
>>> extract_post_query('POST', 'application/x-www-form-urlencoded', len(post_data) + 4, BytesIO(post_data))
84+
'foo=bar&dir=/baz'
5985
"""
6086

6187

@@ -64,7 +90,7 @@
6490
import os
6591
from io import BytesIO
6692
from pywb.utils.loaders import BlockLoader, HMACCookieMaker, to_file_url
67-
from pywb.utils.loaders import LimitReader, extract_client_cookie
93+
from pywb.utils.loaders import LimitReader, extract_client_cookie, extract_post_query
6894

6995
from pywb import get_test_dir
7096

0 commit comments

Comments
 (0)