Skip to content

Commit

Permalink
HARMONY-1929: Test that POST body matches opendap query
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnyinverso committed Nov 21, 2024
1 parent 3c27921 commit 9d6f685
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions harmony/harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,7 @@ def _download_file(self, url: str, directory: str = '', overwrite: bool = False)
chunksize = int(self.config.DOWNLOAD_CHUNK_SIZE)
session = self._session()
filename = self.get_download_filename_from_url(url)
new_url = url

if directory:
filename = os.path.join(directory, filename)
Expand All @@ -1317,12 +1318,12 @@ def _download_file(self, url: str, directory: str = '', overwrite: bool = False)
is_opendap = parse_result.netloc.startswith('opendap')
method = 'post' if is_opendap else 'get'
if is_opendap: # remove the query params from the URL and convert to dict
url = parse.urlunparse(parse_result._replace(query=""))
new_url = parse.urlunparse(parse_result._replace(query=""))
data_dict = dict(parse.parse_qsl(parse.urlsplit(url).query))
headers = {
"Accept-Encoding": "identity"
}
with getattr(session, method)(url, data=data_dict, stream=True, headers=headers) as r:
with getattr(session, method)(new_url, data=data_dict, stream=True, headers=headers) as r:
with open(filename, 'wb') as f:
shutil.copyfileobj(r.raw, f, length=chunksize)
if verbose and verbose.upper() == 'TRUE':
Expand Down
6 changes: 3 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,16 +1019,16 @@ def test_download_opendap_file():
file_obj.write(expected_data)
file_obj.seek(0)
with responses.RequestsMock() as resp_mock:
resp_mock.add(responses.POST, path + expected_filename, body=file_obj.read(), stream=True)
resp_mock.add(responses.POST, path + expected_filename, body=file_obj.read(), stream=True,
match=[responses.matchers.urlencoded_params_matcher({"dap4.ce": "/ds_surf_type[0:1:4]"})])
client = Client(should_validate_auth=False)
actual_output = client._download_file(url, overwrite=False)

# TODO assert POST body params are as expected

assert actual_output == expected_filename
with open(expected_filename, 'rb') as temp_file:
data = temp_file.read()
assert data == expected_data

os.unlink(actual_output)

def test_download_all(mocker):
Expand Down

0 comments on commit 9d6f685

Please sign in to comment.