1
1
import pathlib
2
+ from requests import Session
2
3
import unittest
3
4
from unittest .mock import patch , MagicMock , mock_open , ANY
4
5
from urllib .error import HTTPError
5
6
6
7
from harmony import aws
7
8
from harmony import util
9
+ from harmony .http import request_context
8
10
from harmony .message import Variable
9
11
from tests .test_cli import MockAdapter , cli_test
10
12
from tests .util import mock_receive , config_fixture
@@ -19,7 +21,7 @@ def setUp(self):
19
21
@patch ('harmony.aws.Config' )
20
22
def test_s3_download_sets_minimal_user_agent_on_boto_client (self , boto_cfg , client , get_version ):
21
23
fake_lib_version = '0.1.0'
22
- get_version .return_value = fake_lib_version
24
+ get_version .return_value = fake_lib_version
23
25
cfg = config_fixture ()
24
26
boto_cfg_instance = MagicMock ()
25
27
boto_cfg .return_value = boto_cfg_instance
@@ -33,7 +35,7 @@ def test_s3_download_sets_minimal_user_agent_on_boto_client(self, boto_cfg, clie
33
35
@patch ('harmony.aws.Config' )
34
36
def test_s3_download_sets_harmony_user_agent_on_boto_client (self , boto_cfg , client , get_version ):
35
37
fake_lib_version = '0.1.0'
36
- get_version .return_value = fake_lib_version
38
+ get_version .return_value = fake_lib_version
37
39
harmony_user_agt = 'harmony/3.3.3 (harmony-test)'
38
40
cfg = config_fixture (user_agent = harmony_user_agt )
39
41
boto_cfg_instance = MagicMock ()
@@ -49,7 +51,7 @@ def test_s3_download_sets_harmony_user_agent_on_boto_client(self, boto_cfg, clie
49
51
def test_s3_download_sets_app_name_on_boto_client (self , boto_cfg , client , get_version ):
50
52
app_name = 'gdal-subsetter'
51
53
fake_lib_version = '0.1.0'
52
- get_version .return_value = fake_lib_version
54
+ get_version .return_value = fake_lib_version
53
55
cfg = config_fixture (app_name = app_name )
54
56
boto_cfg_instance = MagicMock ()
55
57
boto_cfg .return_value = boto_cfg_instance
@@ -58,6 +60,73 @@ def test_s3_download_sets_app_name_on_boto_client(self, boto_cfg, client, get_ve
58
60
boto_cfg .assert_called_with (user_agent_extra = f'harmony (unknown version) harmony-service-lib/{ fake_lib_version } ({ app_name } )' )
59
61
client .assert_called_with (service_name = 's3' , config = boto_cfg_instance , region_name = ANY )
60
62
63
+ @patch ('harmony.util.get_version' )
64
+ @patch ('harmony.aws.download' )
65
+ @patch ('harmony.aws.Config' )
66
+ def test_s3_download_does_not_set_api_request_uuid (self , boto_cfg , aws_download , get_version ):
67
+ request_context ['request_id' ] = 'abc123'
68
+ app_name = 'gdal-subsetter'
69
+ fake_lib_version = '0.1.0'
70
+ get_version .return_value = fake_lib_version
71
+ cfg = config_fixture (app_name = app_name )
72
+ boto_cfg_instance = MagicMock ()
73
+ boto_cfg .return_value = boto_cfg_instance
74
+ with patch ('builtins.open' , mock_open ()):
75
+ util .download ('s3://example/file.txt' , 'tmp' , access_token = '' , cfg = cfg )
76
+ aws_download .assert_called_with (ANY , 's3://example/file.txt' , ANY , ANY )
77
+
78
+ @patch ('harmony.util.get_version' )
79
+ @patch .object (Session , 'get' )
80
+ def test_http_download_sets_api_request_uuid (self , get , get_version ):
81
+ request_context ['request_id' ] = 'abc123'
82
+ app_name = 'gdal-subsetter'
83
+ fake_lib_version = '0.1.0'
84
+ get_version .return_value = fake_lib_version
85
+ cfg = config_fixture (app_name = app_name )
86
+ with patch ('builtins.open' , mock_open ()):
87
+ util .download ('http://example/file.txt' , 'tmp' , access_token = '' , cfg = cfg )
88
+ get .assert_called_with ('http://example/file.txt?A-api-request-uuid=abc123' , headers = {'user-agent' : f'harmony (unknown version) harmony-service-lib/{ fake_lib_version } (gdal-subsetter)' }, timeout = 60 , stream = True )
89
+
90
+ @patch ('harmony.util.get_version' )
91
+ @patch .object (Session , 'get' )
92
+ def test_https_download_sets_api_request_uuid (self , get , get_version ):
93
+ request_context ['request_id' ] = 'abc123'
94
+ app_name = 'gdal-subsetter'
95
+ fake_lib_version = '0.1.0'
96
+ get_version .return_value = fake_lib_version
97
+ cfg = config_fixture (app_name = app_name )
98
+ with patch ('builtins.open' , mock_open ()):
99
+ util .download ('https://example/file.txt' , 'tmp' , access_token = '' , cfg = cfg )
100
+ get .assert_called_with ('https://example/file.txt?A-api-request-uuid=abc123' , headers = {'user-agent' : f'harmony (unknown version) harmony-service-lib/{ fake_lib_version } (gdal-subsetter)' }, timeout = 60 , stream = True )
101
+
102
+ @patch ('harmony.util.get_version' )
103
+ @patch .object (Session , 'post' )
104
+ def test_http_download_with_post_sets_api_request_uuid (self , post , get_version ):
105
+ request_context ['request_id' ] = 'abc123'
106
+ app_name = 'gdal-subsetter'
107
+ fake_lib_version = '0.1.0'
108
+ get_version .return_value = fake_lib_version
109
+ data = { 'foo' : 'bar' }
110
+ cfg = config_fixture (app_name = app_name )
111
+ with patch ('builtins.open' , mock_open ()):
112
+ util .download ('http://example/file.txt' , 'tmp' , access_token = '' , data = data , cfg = cfg )
113
+ post .assert_called_with ('http://example/file.txt?A-api-request-uuid=abc123' , headers = {'user-agent' : f'harmony (unknown version) harmony-service-lib/{ fake_lib_version } (gdal-subsetter)' , 'Content-Type' : 'application/x-www-form-urlencoded' }, data = { 'foo' : 'bar' }, timeout = 60 , stream = True )
114
+
115
+
116
+ @patch ('harmony.util.get_version' )
117
+ @patch .object (Session , 'post' )
118
+ def test_https_download_with_post_sets_api_request_uuid (self , post , get_version ):
119
+ request_context ['request_id' ] = 'abc123'
120
+ app_name = 'gdal-subsetter'
121
+ fake_lib_version = '0.1.0'
122
+ get_version .return_value = fake_lib_version
123
+ data = { 'foo' : 'bar' }
124
+ cfg = config_fixture (app_name = app_name )
125
+ with patch ('builtins.open' , mock_open ()):
126
+ util .download ('https://example/file.txt' , 'tmp' , access_token = '' , data = data , cfg = cfg )
127
+ post .assert_called_with ('https://example/file.txt?A-api-request-uuid=abc123' , headers = {'user-agent' : f'harmony (unknown version) harmony-service-lib/{ fake_lib_version } (gdal-subsetter)' , 'Content-Type' : 'application/x-www-form-urlencoded' }, data = { 'foo' : 'bar' }, timeout = 60 , stream = True )
128
+
129
+
61
130
class TestStage (unittest .TestCase ):
62
131
def setUp (self ):
63
132
self .config = util .config (validate = False )
0 commit comments