2019-06-28 19:54:45 +02:00
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
import requests_mock
|
|
|
|
|
|
|
|
from moulinette.core import MoulinetteError
|
|
|
|
from moulinette.utils.network import download_json, download_text
|
|
|
|
|
|
|
|
|
|
|
|
def test_download(test_url):
|
|
|
|
with requests_mock.Mocker() as mock:
|
2019-11-25 17:21:13 +01:00
|
|
|
mock.register_uri("GET", test_url, text="some text")
|
2019-06-28 19:54:45 +02:00
|
|
|
fetched_text = download_text(test_url)
|
2019-11-25 17:21:13 +01:00
|
|
|
assert fetched_text == "some text"
|
2019-06-28 19:54:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_download_bad_url():
|
|
|
|
with pytest.raises(MoulinetteError):
|
2019-11-25 17:21:13 +01:00
|
|
|
download_text("Nowhere")
|
2019-06-28 19:54:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_download_404(test_url):
|
|
|
|
with requests_mock.Mocker() as mock:
|
2019-11-25 17:21:13 +01:00
|
|
|
mock.register_uri("GET", test_url, status_code=404)
|
2019-06-28 19:54:45 +02:00
|
|
|
with pytest.raises(MoulinetteError):
|
|
|
|
download_text(test_url)
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_ssl_error(test_url):
|
|
|
|
with requests_mock.Mocker() as mock:
|
|
|
|
exception = requests.exceptions.SSLError
|
2019-11-25 17:21:13 +01:00
|
|
|
mock.register_uri("GET", test_url, exc=exception)
|
2019-06-28 19:54:45 +02:00
|
|
|
with pytest.raises(MoulinetteError):
|
|
|
|
download_text(test_url)
|
|
|
|
|
|
|
|
|
2019-12-21 17:36:20 +01:00
|
|
|
def test_download_connection_error(test_url):
|
|
|
|
with requests_mock.Mocker() as mock:
|
|
|
|
exception = requests.exceptions.ConnectionError
|
|
|
|
mock.register_uri("GET", test_url, exc=exception)
|
|
|
|
with pytest.raises(MoulinetteError):
|
|
|
|
download_text(test_url)
|
|
|
|
|
|
|
|
|
2019-06-28 19:54:45 +02:00
|
|
|
def test_download_timeout(test_url):
|
|
|
|
with requests_mock.Mocker() as mock:
|
2019-12-21 17:36:20 +01:00
|
|
|
exception = requests.exceptions.Timeout
|
2019-11-25 17:21:13 +01:00
|
|
|
mock.register_uri("GET", test_url, exc=exception)
|
2019-06-28 19:54:45 +02:00
|
|
|
with pytest.raises(MoulinetteError):
|
|
|
|
download_text(test_url)
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_json(test_url):
|
|
|
|
with requests_mock.Mocker() as mock:
|
2019-11-25 17:21:13 +01:00
|
|
|
mock.register_uri("GET", test_url, text='{"foo":"bar"}')
|
2019-06-28 19:54:45 +02:00
|
|
|
fetched_json = download_json(test_url)
|
2019-11-25 17:21:13 +01:00
|
|
|
assert "foo" in fetched_json.keys()
|
|
|
|
assert fetched_json["foo"] == "bar"
|
2019-06-28 19:54:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_download_json_bad_json(test_url):
|
|
|
|
with requests_mock.Mocker() as mock:
|
2019-11-25 17:21:13 +01:00
|
|
|
mock.register_uri("GET", test_url, text="notjsonlol")
|
2019-06-28 19:54:45 +02:00
|
|
|
with pytest.raises(MoulinetteError):
|
|
|
|
download_json(test_url)
|