Skip to content

Commit

Permalink
Fix places where timeout was being passed through
Browse files Browse the repository at this point in the history
  • Loading branch information
jimwins committed Mar 7, 2024
1 parent bbfff5a commit d3ba34b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions frozen_soup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def freeze_to_string(
# Turn rel="stylesheet" into <style>
elif 'stylesheet' in link.get_attribute_list('rel'):
stylesheet_url = urljoin(url, link['href'])
response = session.get(stylesheet_url)
response = session.get(stylesheet_url, timeout=timeout)
if response.status_code == 200:
style = soup.new_tag('style')
style.string = expand_urls_in_css(response.text, stylesheet_url, session, timeout)
Expand All @@ -57,7 +57,7 @@ def freeze_to_string(
# Inline <script src="">
for script in soup.find_all('script'):
if script.get('src'):
response = session.get(urljoin(url, script['src']))
response = session.get(urljoin(url, script['src']), timeout=timeout)
if response.status_code == 200:
script.string = response.text
# TODO what other attributes do we care about?
Expand Down
60 changes: 60 additions & 0 deletions tests/test_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ def session() -> requests.Session:
s.mount("http://test/img-contains-long-timeout", TestAdapter(b'<img src="long-timeout">'))
s.mount("http://test/img-contains-short-timeout", TestAdapter(b'<img src="short-timeout">'))

s.mount(
"http://test/link-contains-long-timeout",
TestAdapter(b'<link rel="icon" href="long-timeout">')
)
s.mount(
"http://test/link-contains-short-timeout",
TestAdapter(b'<link rel="icon" href="short-timeout">')
)

s.mount(
"http://test/script-contains-long-timeout",
TestAdapter(b'<script src="long-timeout"></script>')
)
s.mount(
"http://test/script-contains-short-timeout",
TestAdapter(b'<script src="short-timeout"></script>')
)

s.mount(
"http://test/long-timeout-css",
TimeoutTestAdapter(b'* { background: white; }', timeout= 1200)
)
s.mount(
"http://test/short-timeout-css",
TimeoutTestAdapter(b'* { background: white; }', timeout= 3)
)

s.mount(
"http://test/link-stylesheet-contains-long-timeout",
TestAdapter(b'<link rel="stylesheet" href="long-timeout-css">')
)
s.mount(
"http://test/link-stylesheet-contains-short-timeout",
TestAdapter(b'<link rel="stylesheet" href="short-timeout-css">')
)

return s

def test_timeout_okay(session):
Expand Down Expand Up @@ -68,3 +104,27 @@ def test_timeout_tuple_connect_raised(session):
def test_timeout_tuple_read_raised(session):
with pytest.raises(ReadTimeoutError):
out = freeze_to_string('http://test/long-timeout', session, timeout= (1200, 100))

def test_timeout_okay_on_link(session):
out = freeze_to_string('http://test/link-contains-short-timeout', session, timeout= 100)
assert out == '<link href="data:None;base64,REFUQQ==" rel="icon">'

def test_timeout_raised_on_link(session):
with pytest.raises(ConnectTimeoutError):
out = freeze_to_string('http://test/link-contains-long-timeout', session, timeout= 100)

def test_timeout_okay_on_script(session):
out = freeze_to_string('http://test/script-contains-short-timeout', session, timeout= 100)
assert out == '<script>DATA</script>'

def test_timeout_raised_on_script(session):
with pytest.raises(ConnectTimeoutError):
out = freeze_to_string('http://test/script-contains-long-timeout', session, timeout= 100)

def test_timeout_okay_on_link_stylesheet(session):
out = freeze_to_string('http://test/link-stylesheet-contains-short-timeout', session, timeout= 100)
assert out == '<style>* { background: white; }</style>'

def test_timeout_raised_on_link_stylesheet(session):
with pytest.raises(ConnectTimeoutError):
out = freeze_to_string('http://test/link-stylesheet-contains-long-timeout', session, timeout= 100)

0 comments on commit d3ba34b

Please sign in to comment.