Skip to content

Commit

Permalink
add tests for HTTP requests parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
aperrin66 committed Oct 6, 2021
1 parent 70dc8a4 commit 2167e33
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/test_downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ def test_get_basic_auth(self):
('username', 'password')
)

def test_get_request_parameters(self):
"""get_request_parameters() should return the
'request_parameters' key from kwargs if it is present and
contains a dictionary
"""
self.assertDictEqual(
downloaders.HTTPDownloader.get_request_parameters({
'foo': 'bar',
'request_parameters': {'baz': 'qux'}
}),
{'baz': 'qux'})

self.assertDictEqual(downloaders.HTTPDownloader.get_request_parameters({'foo': 'bar'}), {})

def test_get_request_parameters_invalid(self):
"""get_request_parameters() should raise an exception if the
'request_parameters' key's contents are invalid
"""
with self.assertRaises(ValueError):
downloaders.HTTPDownloader.get_request_parameters({'request_parameters': 'foo'})

def test_get_file_name(self):
"""Test the correct extraction of a file name from a standard
Content-Disposition header
Expand Down Expand Up @@ -288,6 +309,36 @@ def test_get_file_name_head_error(self):
with self.assertLogs(downloaders.LOGGER, level=logging.ERROR):
self.assertEqual(downloaders.HTTPDownloader.get_file_name('url', None), '')

def test_get_file_name_with_parameters(self):
"""Test getting a file name with parameters in the HEAD request
"""
file_name = "test_file.txt"
response = requests.Response()
response.status_code = 200
response.headers['Content-Disposition'] = f'inline;filename="{file_name}"'
with mock.patch(
'geospaas_processing.utils.http_request',
return_value=response) as mock_http_request:
self.assertEqual(
downloaders.HTTPDownloader.get_file_name(
'url', None, request_parameters={'foo': 'bar'}),
file_name)
mock_http_request.assert_called_once_with('HEAD', 'url', auth=None, params={'foo': 'bar'})

def test_get_file_name_without_parameters(self):
"""Test getting a file name without parameters in the HEAD
request
"""
file_name = "test_file.txt"
response = requests.Response()
response.status_code = 200
response.headers['Content-Disposition'] = f'inline;filename="{file_name}"'
with mock.patch(
'geospaas_processing.utils.http_request',
return_value=response) as mock_http_request:
self.assertEqual(downloaders.HTTPDownloader.get_file_name('url', None), file_name)
mock_http_request.assert_called_once_with('HEAD', 'url', auth=None, params={})

def test_connect(self):
"""Connect should return a Response object"""
response = requests.Response()
Expand All @@ -296,6 +347,20 @@ def test_connect(self):
connect_result = downloaders.HTTPDownloader.connect('url')
self.assertEqual(connect_result, response)

def test_connect_with_parameters(self):
"""Test connecting with parameters to the GET request"""
with mock.patch('geospaas_processing.utils.http_request') as mock_http_request:
downloaders.HTTPDownloader.connect('url', request_parameters={'appkey': 'foo'})
mock_http_request.assert_called_once_with(
'GET', 'url', stream=True, auth=(None, None), params={'appkey': 'foo'})

def test_connect_without_parameters(self):
"""Test connecting with parameters to the GET request"""
with mock.patch('geospaas_processing.utils.http_request') as mock_http_request:
downloaders.HTTPDownloader.connect('url')
mock_http_request.assert_called_once_with(
'GET', 'url', stream=True, auth=(None, None), params={})

def test_connect_error_code(self):
"""An exception should be raised when an error code is received
"""
Expand Down

0 comments on commit 2167e33

Please sign in to comment.