Skip to content

Commit ac2461b

Browse files
committed
Add tests for location providing
-Testing filename deduction for http/https/ftp schemes Signed-off-by: Mateusz Perc <[email protected]>
1 parent 640e874 commit ac2461b

File tree

1 file changed

+69
-18
lines changed

1 file changed

+69
-18
lines changed

tests/test_fetch.py

+69-18
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,103 @@
1414
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations under the License.
1616

17+
from fetchcode import fetch
18+
from pathlib import Path
1719
from unittest import mock
1820

1921
import pytest
2022

21-
from fetchcode import fetch
2223

24+
FILENAMES = ["a.ext", "b.ext", "c.ext"]
25+
HTTP_URL = "http://example.com/"
26+
FTP_URL = "ftp://example.com/"
27+
28+
29+
@mock.patch("fetchcode.requests.get")
30+
def test_fetch_http_with_filename_provided(mock_get):
31+
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
32+
location = Path.home() / FILENAMES[0]
33+
response = fetch(HTTP_URL, location)
34+
assert response is not None
35+
assert response.location == location
36+
37+
38+
@pytest.mark.parametrize(
39+
"parameters, expected_filename",
40+
[
41+
pytest.param(f'filename*="{FILENAMES[0]}"; filename=""', FILENAMES[0]),
42+
pytest.param(f'filename*=""; filename="{FILENAMES[1]}"', FILENAMES[1]),
43+
pytest.param(f'filename*=""; filename=""', FILENAMES[2]),
44+
],
45+
)
46+
@mock.patch("fetchcode.requests.get")
47+
def test_fetch_http_with_filename_deduction(mock_get, parameters, expected_filename):
48+
mock_get.return_value.headers = {"content-disposition": f"attachment; {parameters}"}
49+
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
50+
location = Path.home()
51+
response = fetch(HTTP_URL + FILENAMES[2], location)
52+
assert response is not None
53+
assert response.location == (location / expected_filename)
2354

24-
@mock.patch('fetchcode.requests.get')
55+
56+
@mock.patch("fetchcode.tempfile.NamedTemporaryFile")
57+
@mock.patch("fetchcode.requests.get")
58+
def test_fetch_http_filename_deduction_failed(mock_get, mock_tmp):
59+
location = Path.home()
60+
mock_get.return_value.headers = {}
61+
mock_tmp.return_value.name = location / FILENAMES[0]
62+
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
63+
response = fetch(HTTP_URL, location)
64+
assert response is not None
65+
assert response.location == (location / FILENAMES[0])
66+
67+
68+
@mock.patch("fetchcode.requests.get")
2569
def test_fetch_http_with_tempfile(mock_get):
2670
mock_get.return_value.headers = {
27-
'content-type': 'image/png',
28-
'content-length': '1000999',
71+
"content-type": "image/png",
72+
"content-length": "1000999",
2973
}
30-
31-
with mock.patch('fetchcode.open', mock.mock_open()) as mocked_file:
32-
url = 'https://raw.githubusercontent.com/TG1999/converge/master/assets/Group%2022.png'
74+
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
75+
url = "https://raw.githubusercontent.com/TG1999/converge/master/assets/Group%2022.png"
3376
response = fetch(url=url)
3477
assert response is not None
3578
assert 1000999 == response.size
3679
assert url == response.url
37-
assert 'image/png' == response.content_type
80+
assert "image/png" == response.content_type
3881

3982

40-
@mock.patch('fetchcode.FTP')
83+
@mock.patch("fetchcode.FTP")
4184
def test_fetch_with_wrong_url(mock_get):
4285
with pytest.raises(Exception) as e_info:
43-
url = 'ftp://speedtest/1KB.zip'
86+
url = "ftp://speedtest/1KB.zip"
4487
response = fetch(url=url)
45-
assert 'Not a valid URL' == e_info
88+
assert "Not a valid URL" == e_info
4689

4790

48-
@mock.patch('fetchcode.FTP', autospec=True)
91+
@mock.patch("fetchcode.FTP", autospec=True)
4992
def test_fetch_ftp_with_tempfile(mock_ftp_constructor):
5093
mock_ftp = mock_ftp_constructor.return_value
5194
mock_ftp_constructor.return_value.size.return_value = 1024
52-
with mock.patch('fetchcode.open', mock.mock_open()) as mocked_file:
53-
response = fetch('ftp://speedtest.tele2.net/1KB.zip')
95+
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
96+
response = fetch("ftp://speedtest.tele2.net/1KB.zip")
5497
assert 1024 == response.size
55-
mock_ftp_constructor.assert_called_with('speedtest.tele2.net')
98+
mock_ftp_constructor.assert_called_with("speedtest.tele2.net")
5699
assert mock_ftp.login.called == True
57-
mock_ftp.cwd.assert_called_with('/')
100+
mock_ftp.cwd.assert_called_with("/")
58101
assert mock_ftp.retrbinary.called
59102

60103

104+
@mock.patch("fetchcode.FTP")
105+
def test_fetch_ftp_with_filename_deduction(mock_ftp):
106+
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
107+
location = Path.home()
108+
response = fetch(FTP_URL + FILENAMES[0], location)
109+
assert response.location == (location / FILENAMES[0])
110+
111+
61112
def test_fetch_with_scheme_not_present():
62113
with pytest.raises(Exception) as e_info:
63-
url = 'abc://speedtest/1KB.zip'
114+
url = "abc://speedtest/1KB.zip"
64115
response = fetch(url=url)
65-
assert 'Not a supported/known scheme.' == e_info
116+
assert "Not a supported/known scheme." == e_info

0 commit comments

Comments
 (0)