2015-09-17 06:53:28 +02:00
|
|
|
import urllib.request
|
2015-09-17 07:33:52 +02:00
|
|
|
import urllib.error
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('path, content, expected', [
|
|
|
|
('/', '<title>httpbin(1): HTTP Client Testing Service</title>', True),
|
|
|
|
# https://github.com/Runscope/httpbin/issues/245
|
|
|
|
('/', 'www.google-analytics.com', False),
|
2015-09-17 07:45:12 +02:00
|
|
|
('/data/hello.txt', 'Hello World!', True),
|
2015-09-17 06:53:28 +02:00
|
|
|
])
|
|
|
|
def test_httpbin(httpbin, qtbot, path, content, expected):
|
|
|
|
with qtbot.waitSignal(httpbin.got_new_url, raising=True, timeout=100):
|
|
|
|
url = 'http://localhost:{}{}'.format(httpbin.port, path)
|
2015-09-17 07:33:52 +02:00
|
|
|
try:
|
|
|
|
response = urllib.request.urlopen(url)
|
|
|
|
except urllib.error.HTTPError as e:
|
|
|
|
# "Though being an exception (a subclass of URLError), an HTTPError
|
|
|
|
# can also function as a non-exceptional file-like return value
|
|
|
|
# (the same thing that urlopen() returns)."
|
|
|
|
# ...wat
|
|
|
|
print(e.read().decode('utf-8'))
|
|
|
|
raise
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
data = response.read().decode('utf-8')
|
|
|
|
|
|
|
|
assert httpbin.get_visited() == ['GET {}'.format(path)]
|
|
|
|
assert (content in data) == expected
|