1
1
import json
2
- from typing import Dict , List , Type
3
- from unittest .mock import patch
2
+ from typing import Dict , List , Optional , Type
3
+ from unittest .mock import Mock , patch
4
4
5
5
import pytest
6
6
from marshmallow import ValidationError
7
+ from requests .models import Response
7
8
8
9
from pygitguardian import GGClient
9
10
from pygitguardian .client import is_ok , load_detail
143
144
144
145
145
146
@pytest .mark .parametrize (
146
- "api_key, uri, user_agent, timeout, exception" ,
147
+ "api_key, uri, user_agent, timeout, exception " ,
147
148
[
148
149
pytest .param (
149
150
"validapi_keyforsure" ,
181
182
ValueError ,
182
183
id = "invalid prefix" ,
183
184
),
185
+ pytest .param (
186
+ "validapi_keyforsure" ,
187
+ "https://api.gitguardian.com/" ,
188
+ "custom" ,
189
+ 30.0 ,
190
+ None ,
191
+ id = "Custom headers" ,
192
+ ),
184
193
],
185
194
)
186
195
def test_client_creation (
187
- api_key : str , uri : str , user_agent : str , timeout : float , exception : Type [Exception ]
196
+ api_key : str ,
197
+ uri : str ,
198
+ user_agent : str ,
199
+ timeout : float ,
200
+ exception : Type [Exception ],
188
201
):
189
202
if exception is not None :
190
203
with pytest .raises (exception ):
191
204
client = GGClient (
192
- api_key = api_key , base_uri = uri , user_agent = user_agent , timeout = timeout
205
+ api_key = api_key ,
206
+ base_uri = uri ,
207
+ user_agent = user_agent ,
208
+ timeout = timeout ,
193
209
)
194
210
else :
195
211
client = GGClient (
196
- base_uri = uri , api_key = api_key , user_agent = user_agent , timeout = timeout
212
+ base_uri = uri ,
213
+ api_key = api_key ,
214
+ user_agent = user_agent ,
215
+ timeout = timeout ,
197
216
)
198
217
199
218
if exception is None :
@@ -202,8 +221,8 @@ def test_client_creation(
202
221
else :
203
222
assert client .base_uri == DEFAULT_BASE_URI
204
223
assert client .api_key == api_key
205
- assert user_agent in client .session .headers ["User-Agent" ]
206
224
assert client .timeout == timeout
225
+ assert user_agent in client .session .headers ["User-Agent" ]
207
226
assert client .session .headers ["Authorization" ] == "Token {0}" .format (api_key )
208
227
209
228
@@ -416,3 +435,66 @@ def test_assert_content_type(client: GGClient):
416
435
assert obj .status_code == 200
417
436
assert isinstance (obj , Detail )
418
437
assert str (obj ).startswith ("200:" ), str (obj )
438
+
439
+
440
+ @pytest .mark .parametrize (
441
+ "session_headers, extra_headers, expected_headers" ,
442
+ [
443
+ pytest .param (
444
+ {"session-header" : "value" },
445
+ None ,
446
+ {"session-header" : "value" },
447
+ id = "no-additional-headers" ,
448
+ ),
449
+ pytest .param (
450
+ {"session-header" : "value" },
451
+ {"additional-header" : "value" },
452
+ {"session-header" : "value" , "additional-header" : "value" },
453
+ id = "additional-headers" ,
454
+ ),
455
+ pytest .param (
456
+ {"session-header" : "value" , "common-header" : "session-value" },
457
+ {"additional-header" : "value" , "common-header" : "add-value" },
458
+ {
459
+ "session-header" : "value" ,
460
+ "additional-header" : "value" ,
461
+ "common-header" : "add-value" ,
462
+ },
463
+ id = "priority-additional-headers" ,
464
+ ),
465
+ ],
466
+ )
467
+ @patch ("requests.Session.request" )
468
+ @my_vcr .use_cassette
469
+ def test_extra_headers (
470
+ request_mock : Mock ,
471
+ client : GGClient ,
472
+ session_headers : Dict [str , str ],
473
+ extra_headers : Optional [Dict [str , str ]],
474
+ expected_headers : Dict [str , str ],
475
+ ):
476
+ """
477
+ GIVEN client's session headers
478
+ WHEN calling any client method with additional headers
479
+ THEN session/method headers should be merged with priority on method headers
480
+ """
481
+ client .session .headers = session_headers
482
+
483
+ mock_response = Mock (spec = Response )
484
+ mock_response .headers = {"content-type" : "text" }
485
+ mock_response .text = "some error"
486
+ mock_response .status_code = 400
487
+ request_mock .return_value = mock_response
488
+
489
+ client .multi_content_scan (
490
+ [{"filename" : FILENAME , "document" : DOCUMENT }],
491
+ extra_headers = extra_headers ,
492
+ )
493
+ assert request_mock .called
494
+ _ , kwargs = request_mock .call_args
495
+ assert expected_headers == kwargs ["headers" ]
496
+
497
+ client .content_scan ("some_string" , extra_headers = extra_headers )
498
+ assert request_mock .called
499
+ _ , kwargs = request_mock .call_args
500
+ assert expected_headers == kwargs ["headers" ]
0 commit comments