99
1010import pytest
1111import requests
12+ from charset_normalizer import from_bytes
1213
1314import openeo
1415import openeo .rest .job
@@ -78,7 +79,9 @@ def test_execute_batch(con100, requests_mock, tmpdir):
7879 }
7980 },
8081 )
81- _mock_get_head_content (requests_mock , API_URL + "/jobs/f00ba5/files/output.tiff" , "tiffdata" )
82+ requests_mock .head (API_URL + "/jobs/f00ba5/files/output.tiff" , headers = {"Content-Length" : f"{ len ("tiffdata" )} " })
83+ requests_mock .get (API_URL + "/jobs/f00ba5/files/output.tiff" , text = "tiffdata" )
84+
8285 requests_mock .get (API_URL + "/jobs/f00ba5/logs" , json = {'logs' : []})
8386
8487 path = tmpdir .join ("tmp.tiff" )
@@ -235,8 +238,8 @@ def test_execute_batch_with_soft_errors(con100, requests_mock, tmpdir, error_res
235238 }
236239 },
237240 )
238- _mock_get_head_content ( requests_mock , API_URL + "/jobs/f00ba5/files/output.tiff" , " tiffdata" )
239- # requests_mock.get(API_URL + "/jobs/f00ba5/files/output.tiff", text="tiffdata")
241+ requests_mock . head ( API_URL + "/jobs/f00ba5/files/output.tiff" , headers = { "Content-Length" : f" { len ( " tiffdata") } " } )
242+ requests_mock .get (API_URL + "/jobs/f00ba5/files/output.tiff" , text = "tiffdata" )
240243 requests_mock .get (API_URL + "/jobs/f00ba5/logs" , json = {'logs' : []})
241244
242245 path = tmpdir .join ("tmp.tiff" )
@@ -549,6 +552,34 @@ def job_with_1_asset(con100, requests_mock, tmp_path) -> BatchJob:
549552
550553@pytest .fixture
551554def job_with_chunked_asset_using_head (con100 , requests_mock , tmp_path ) -> BatchJob :
555+ def handle_content (request , context ):
556+ range = request .headers .get ("Range" )
557+ assert range
558+ search = re .search (r"bytes=(\d+)-(\d+)" , range )
559+ assert search
560+ from_bytes = int (search .group (1 ))
561+ to_bytes = int (search .group (2 ))
562+ assert from_bytes < to_bytes
563+ return TIFF_CONTENT [from_bytes : to_bytes + 1 ]
564+
565+ requests_mock .get (
566+ API_URL + "/jobs/jj1/results" ,
567+ json = {
568+ "assets" : {
569+ "1.tiff" : {"href" : API_URL + "/dl/jjr1.tiff" , "type" : "image/tiff; application=geotiff" },
570+ }
571+ },
572+ )
573+ requests_mock .head (
574+ API_URL + "/dl/jjr1.tiff" , headers = {"Content-Length" : f"{ len (TIFF_CONTENT )} " , "Accept-Ranges" : "bytes" }
575+ )
576+ requests_mock .get (API_URL + "/dl/jjr1.tiff" , content = handle_content )
577+ job = BatchJob ("jj1" , connection = con100 )
578+ return job
579+
580+
581+ @pytest .fixture
582+ def job_with_chunked_asset_using_head_old (con100 , requests_mock , tmp_path ) -> BatchJob :
552583 requests_mock .get (API_URL + "/jobs/jj1/results" , json = {"assets" : {
553584 "1.tiff" : {"href" : API_URL + "/dl/jjr1.tiff" , "type" : "image/tiff; application=geotiff" },
554585 }})
@@ -747,7 +778,8 @@ def test_get_results_download_files_include_stac_metadata(
747778
748779def test_result_asset_download_file (con100 , requests_mock , tmp_path ):
749780 href = API_URL + "/dl/jjr1.tiff"
750- _mock_get_head_content (requests_mock , href , TIFF_CONTENT )
781+ requests_mock .head (href , headers = {"Content-Length" : f"{ len (TIFF_CONTENT )} " })
782+ requests_mock .get (href , content = TIFF_CONTENT )
751783
752784 job = BatchJob ("jj" , connection = con100 )
753785 asset = ResultAsset (job , name = "1.tiff" , href = href , metadata = {'type' : 'image/tiff; application=geotiff' })
@@ -777,7 +809,8 @@ def test_result_asset_download_file_error(con100, requests_mock, tmp_path):
777809
778810def test_result_asset_download_folder (con100 , requests_mock , tmp_path ):
779811 href = API_URL + "/dl/jjr1.tiff"
780- _mock_get_head_content (requests_mock , href , TIFF_CONTENT )
812+ requests_mock .head (href , headers = {"Content-Length" : f"{ len (TIFF_CONTENT )} " })
813+ requests_mock .get (href , content = TIFF_CONTENT )
781814
782815 job = BatchJob ("jj" , connection = con100 )
783816 asset = ResultAsset (job , name = "1.tiff" , href = href , metadata = {"type" : "image/tiff; application=geotiff" })
@@ -804,7 +837,8 @@ def test_result_asset_load_json(con100, requests_mock):
804837
805838def test_result_asset_load_bytes (con100 , requests_mock ):
806839 href = API_URL + "/dl/jjr1.tiff"
807- _mock_get_head_content (requests_mock , href , TIFF_CONTENT )
840+ requests_mock .head (href , headers = {"Content-Length" : f"{ len (TIFF_CONTENT )} " })
841+ requests_mock .get (href , content = TIFF_CONTENT )
808842
809843 job = BatchJob ("jj" , connection = con100 )
810844 asset = ResultAsset (job , name = "out.tiff" , href = href , metadata = {"type" : "image/tiff; application=geotiff" })
@@ -915,15 +949,3 @@ def get_jobs(request, context):
915949 assert jobs .links == [Link (rel = "next" , href = "https://oeo.test/jobs?limit=2&offset=2" )]
916950 assert jobs .ext_federation_missing () == ["oeob" ]
917951 assert "Partial job listing: missing federation components: ['oeob']." in caplog .text
918-
919-
920- def _mock_get_head_content (requests_mock , url : str , content ):
921- if callable (content ):
922- requests_mock .head (url , headers = {"Content-Length" : "666" })
923- requests_mock .get (url , content = content )
924- elif type (content ) == str :
925- requests_mock .head (url , headers = {"Content-Length" : f"{ len (content )} " })
926- requests_mock .get (url , text = content )
927- else :
928- requests_mock .head (url , headers = {"Content-Length" : f"{ len (content )} " })
929- requests_mock .get (url , content = content )
0 commit comments