1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
From: Matthieu Moy <git@matthieu-moy.fr>
Date: Fri, 22 Jun 2018 17:45:36 +0200
Subject: [PATCH] Allow the testsuite to run on new httpbin versions
The new httpbin version has a JavaScript-enabled welcome page, not
suited for MechanicalSoup. Fortunately, the old version is still
available with a URL /legacy.
Use this URL, if available, for tests which need it.
Fixes #213.
---
tests/test_stateful_browser.py | 18 ++++++++++++------
tests/utils.py | 17 +++++++++++++++++
2 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/tests/test_stateful_browser.py b/tests/test_stateful_browser.py
index fba91c9..9fc1140 100644
--- a/tests/test_stateful_browser.py
+++ b/tests/test_stateful_browser.py
@@ -6,7 +6,12 @@ import mechanicalsoup
import sys
import re
from bs4 import BeautifulSoup
-from utils import setup_mock_browser, prepare_mock_browser, mock_get
+from utils import (
+ setup_mock_browser,
+ prepare_mock_browser,
+ mock_get,
+ open_legacy_httpbin
+)
import pytest
import webbrowser
@@ -367,11 +372,12 @@ def test_select_form_tag_object():
def test_referer_follow_link(httpbin):
browser = mechanicalsoup.StatefulBrowser()
- browser.open(httpbin.url)
+ open_legacy_httpbin(browser, httpbin)
+ start_url = browser.get_url()
response = browser.follow_link("/headers")
referer = response.json()["headers"]["Referer"]
actual_ref = re.sub('/*$', '', referer)
- expected_ref = re.sub('/*$', '', httpbin.url)
+ expected_ref = re.sub('/*$', '', start_url)
assert actual_ref == expected_ref
@@ -445,7 +451,7 @@ def file_get_contents(filename):
def test_download_link(httpbin):
"""Test downloading the contents of a link to file."""
browser = mechanicalsoup.StatefulBrowser()
- browser.open(httpbin.url)
+ open_legacy_httpbin(browser, httpbin)
tmpdir = tempfile.mkdtemp()
tmpfile = tmpdir + '/nosuchfile.png'
current_url = browser.get_url()
@@ -466,7 +472,7 @@ def test_download_link(httpbin):
def test_download_link_nofile(httpbin):
"""Test downloading the contents of a link without saving it."""
browser = mechanicalsoup.StatefulBrowser()
- browser.open(httpbin.url)
+ open_legacy_httpbin(browser, httpbin)
current_url = browser.get_url()
current_page = browser.get_current_page()
response = browser.download_link(link='image/png')
@@ -482,7 +488,7 @@ def test_download_link_nofile(httpbin):
def test_download_link_to_existing_file(httpbin):
"""Test downloading the contents of a link to an existing file."""
browser = mechanicalsoup.StatefulBrowser()
- browser.open(httpbin.url)
+ open_legacy_httpbin(browser, httpbin)
tmpdir = tempfile.mkdtemp()
tmpfile = tmpdir + '/existing.png'
with open(tmpfile, "w") as f:
diff --git a/tests/utils.py b/tests/utils.py
index a6a4c1a..7db0a1a 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -76,3 +76,20 @@ class HttpbinRemote:
def __add__(self, x):
return self.url + x
+
+
+def open_legacy_httpbin(browser, httpbin):
+ """Opens the start page of httpbin (given as a fixture). Tries the
+ legacy page (available only on recent versions of httpbin), and if
+ it fails fall back to the main page (which if JavaScript-only in
+ recent versions of httpbin hence usable for us only on old
+ versions.
+ """
+ try:
+ response = browser.open(httpbin + "/legacy")
+ if response.status_code == 404:
+ # The line above may or may not have raised the expection
+ # depending on raise_on_404. Raise it unconditionally now.
+ raise mechanicalsoup.LinkNotFoundError()
+ except mechanicalsoup.LinkNotFoundError:
+ browser.open(httpbin.url)
|