-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
__init__.py
1221 lines (1004 loc) · 38.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import inspect
import json as json_module
import logging
from collections import namedtuple
from functools import partialmethod
from functools import wraps
from http import client
from itertools import groupby
from re import Pattern
from threading import Lock as _ThreadingLock
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from typing import Dict
from typing import Iterable
from typing import Iterator
from typing import List
from typing import Mapping
from typing import Optional
from typing import Sequence
from typing import Sized
from typing import Tuple
from typing import Type
from typing import Union
from typing import overload
from warnings import warn
import tomli as _toml
from requests.adapters import HTTPAdapter
from requests.adapters import MaxRetryError
from requests.exceptions import ConnectionError
from requests.exceptions import RetryError
from responses.matchers import json_params_matcher as _json_params_matcher
from responses.matchers import query_string_matcher as _query_string_matcher
from responses.matchers import urlencoded_params_matcher as _urlencoded_params_matcher
from responses.registries import FirstMatchRegistry
try:
from typing_extensions import Literal
except ImportError: # pragma: no cover
from typing import Literal # type: ignore # pragma: no cover
try:
from requests.packages.urllib3.response import HTTPResponse
except ImportError: # pragma: no cover
from urllib3.response import HTTPResponse # pragma: no cover
try:
from requests.packages.urllib3.connection import HTTPHeaderDict
except ImportError: # pragma: no cover
from urllib3.response import HTTPHeaderDict # type: ignore[attr-defined]
try:
from requests.packages.urllib3.util.url import parse_url
except ImportError: # pragma: no cover
from urllib3.util.url import parse_url # pragma: no cover
from io import BufferedReader
from io import BytesIO
from unittest import mock as std_mock
from urllib.parse import parse_qsl
from urllib.parse import quote
from urllib.parse import urlsplit
from urllib.parse import urlunparse
from urllib.parse import urlunsplit
if TYPE_CHECKING: # pragma: no cover
# import only for linter run
import os
from unittest.mock import _patch as _mock_patcher
from requests import PreparedRequest
from requests import models
from urllib3 import Retry as _Retry
# Block of type annotations
_Body = Union[str, BaseException, "Response", BufferedReader, bytes, None]
_F = Callable[..., Any]
_HeaderSet = Optional[Union[Mapping[str, str], List[Tuple[str, str]]]]
_MatcherIterable = Iterable[Callable[..., Tuple[bool, str]]]
_HTTPMethodOrResponse = Optional[Union[str, "BaseResponse"]]
_URLPatternType = Union["Pattern[str]", str]
Call = namedtuple("Call", ["request", "response"])
_real_send = HTTPAdapter.send
_UNSET = object()
logger = logging.getLogger("responses")
class FalseBool:
"""Class to mock up built-in False boolean.
Used for backwards compatibility, see
https://github.com/getsentry/responses/issues/464
"""
def __bool__(self) -> bool:
return False
__nonzero__ = __bool__
def urlencoded_params_matcher(params: Optional[Dict[str, str]]) -> Callable[..., Any]:
warn(
"Function is deprecated. Use 'from responses.matchers import urlencoded_params_matcher'",
DeprecationWarning,
)
return _urlencoded_params_matcher(params)
def json_params_matcher(params: Optional[Dict[str, Any]]) -> Callable[..., Any]:
warn(
"Function is deprecated. Use 'from responses.matchers import json_params_matcher'",
DeprecationWarning,
)
return _json_params_matcher(params)
def _has_unicode(s: str) -> bool:
return any(ord(char) > 128 for char in s)
def _clean_unicode(url: str) -> str:
"""Clean up URLs, which use punycode to handle unicode chars.
Applies percent encoding to URL path and query if required.
Parameters
----------
url : str
URL that should be cleaned from unicode
Returns
-------
str
Cleaned URL
"""
urllist = list(urlsplit(url))
netloc = urllist[1]
if _has_unicode(netloc):
domains = netloc.split(".")
for i, d in enumerate(domains):
if _has_unicode(d):
d = "xn--" + d.encode("punycode").decode("ascii")
domains[i] = d
urllist[1] = ".".join(domains)
url = urlunsplit(urllist)
# Clean up path/query/params, which use url-encoding to handle unicode chars
chars = list(url)
for i, x in enumerate(chars):
if ord(x) > 128:
chars[i] = quote(x)
return "".join(chars)
def get_wrapped(
func: Callable[..., Any],
responses: "RequestsMock",
*,
registry: Optional[Any] = None,
assert_all_requests_are_fired: Optional[bool] = None,
) -> Callable[..., Any]:
"""Wrap provided function inside ``responses`` context manager.
Provides a synchronous or asynchronous wrapper for the function.
Parameters
----------
func : Callable
Function to wrap.
responses : RequestsMock
Mock object that is used as context manager.
registry : FirstMatchRegistry, optional
Custom registry that should be applied. See ``responses.registries``
assert_all_requests_are_fired : bool
Raise an error if not all registered responses were executed.
Returns
-------
Callable
Wrapped function
"""
assert_mock = std_mock.patch.object(
target=responses,
attribute="assert_all_requests_are_fired",
new=assert_all_requests_are_fired,
)
if inspect.iscoroutinefunction(func):
# set asynchronous wrapper if requestor function is asynchronous
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]
if registry is not None:
responses._set_registry(registry)
with assert_mock, responses:
return await func(*args, **kwargs)
else:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]
if registry is not None:
responses._set_registry(registry)
with assert_mock, responses:
# set 'assert_all_requests_are_fired' temporarily for a single run.
# Mock automatically unsets to avoid leakage to another decorated
# function since we still apply the value on 'responses.mock' object
return func(*args, **kwargs)
return wrapper
class CallList(Sequence[Any], Sized):
def __init__(self) -> None:
self._calls: List[Call] = []
def __iter__(self) -> Iterator[Call]:
return iter(self._calls)
def __len__(self) -> int:
return len(self._calls)
def __getitem__(self, idx: Union[int, slice]) -> Union[Call, List[Call]]:
return self._calls[idx]
def add(self, request: "PreparedRequest", response: _Body) -> None:
self._calls.append(Call(request, response))
def reset(self) -> None:
self._calls = []
def _ensure_url_default_path(
url: _URLPatternType,
) -> _URLPatternType:
"""Add empty URL path '/' if doesn't exist.
Examples
--------
>>> _ensure_url_default_path("http://example.com")
"http://example.com/"
Parameters
----------
url : str or re.Pattern
URL to validate.
Returns
-------
url : str or re.Pattern
Modified URL if str or unchanged re.Pattern
"""
if isinstance(url, str):
url_parts = list(urlsplit(url))
if url_parts[2] == "":
url_parts[2] = "/"
url = urlunsplit(url_parts)
return url
def _get_url_and_path(url: str) -> str:
"""Construct URL only containing scheme, netloc and path by truncating other parts.
This method complies with RFC 3986.
Examples
--------
>>> _get_url_and_path("http://example.com/path;segment?ab=xy&zed=qwe#test=1&foo=bar")
"http://example.com/path;segment"
Parameters
----------
url : str
URL to parse.
Returns
-------
url : str
URL with scheme, netloc and path
"""
url_parsed = urlsplit(url)
url_and_path = urlunparse(
[url_parsed.scheme, url_parsed.netloc, url_parsed.path, None, None, None]
)
return parse_url(url_and_path).url
def _handle_body(
body: Optional[Union[bytes, BufferedReader, str]]
) -> Union[BufferedReader, BytesIO]:
"""Generates `Response` body.
Parameters
----------
body : str or bytes or BufferedReader
Input data to generate `Response` body.
Returns
-------
body : BufferedReader or BytesIO
`Response` body
"""
if isinstance(body, str):
body = body.encode("utf-8")
if isinstance(body, BufferedReader):
return body
data = BytesIO(body) # type: ignore[arg-type]
def is_closed() -> bool:
"""
Real Response uses HTTPResponse as body object.
Thus, when method is_closed is called first to check if there is any more
content to consume and the file-like object is still opened
This method ensures stability to work for both:
https://github.com/getsentry/responses/issues/438
https://github.com/getsentry/responses/issues/394
where file should be intentionally be left opened to continue consumption
"""
if not data.closed and data.read(1):
# if there is more bytes to read then keep open, but return pointer
data.seek(-1, 1)
return False
else:
if not data.closed:
# close but return False to mock like is still opened
data.close()
return False
# only if file really closed (by us) return True
return True
data.isclosed = is_closed # type: ignore[attr-defined]
return data
class BaseResponse(object):
passthrough: bool = False
content_type: Optional[str] = None
headers: Optional[Mapping[str, str]] = None
stream: Optional[bool] = False
def __init__(
self,
method: str,
url: _URLPatternType,
match_querystring: Union[bool, object] = None,
match: "_MatcherIterable" = (),
*,
passthrough: bool = False,
) -> None:
self.method: str = method
# ensure the url has a default path set if the url is a string
self.url: _URLPatternType = _ensure_url_default_path(url)
if self._should_match_querystring(match_querystring):
match = tuple(match) + (
_query_string_matcher(urlsplit(self.url).query), # type: ignore[arg-type]
)
self.match: "_MatcherIterable" = match
self.call_count: int = 0
self.passthrough = passthrough
def __eq__(self, other: Any) -> bool:
if not isinstance(other, BaseResponse):
return False
if self.method != other.method:
return False
# Can't simply do an equality check on the objects directly here since __eq__ isn't
# implemented for regex. It might seem to work as regex is using a cache to return
# the same regex instances, but it doesn't in all cases.
self_url = self.url.pattern if isinstance(self.url, Pattern) else self.url
other_url = other.url.pattern if isinstance(other.url, Pattern) else other.url
return self_url == other_url
def __ne__(self, other: Any) -> bool:
return not self.__eq__(other)
def _should_match_querystring(
self, match_querystring_argument: Union[bool, object]
) -> Union[bool, object]:
if isinstance(self.url, Pattern):
# the old default from <= 0.9.0
return False
if match_querystring_argument is not None:
if not isinstance(match_querystring_argument, FalseBool):
warn(
(
"Argument 'match_querystring' is deprecated. "
"Use 'responses.matchers.query_param_matcher' or "
"'responses.matchers.query_string_matcher'"
),
DeprecationWarning,
)
return match_querystring_argument
return bool(urlsplit(self.url).query)
def _url_matches(self, url: _URLPatternType, other: str) -> bool:
"""Compares two URLs.
Compares only scheme, netloc and path. If 'url' is a re.Pattern, then checks that
'other' matches the pattern.
Parameters
----------
url : Union["Pattern[str]", str]
Reference URL or Pattern to compare.
other : str
URl that should be compared.
Returns
-------
bool
True, if URLs are identical or 'other' matches the pattern.
"""
if isinstance(url, str):
if _has_unicode(url):
url = _clean_unicode(url)
return _get_url_and_path(url) == _get_url_and_path(other)
elif isinstance(url, Pattern) and url.match(other):
return True
else:
return False
@staticmethod
def _req_attr_matches(
match: "_MatcherIterable", request: "PreparedRequest"
) -> Tuple[bool, str]:
for matcher in match:
valid, reason = matcher(request)
if not valid:
return False, reason
return True, ""
def get_headers(self) -> HTTPHeaderDict:
headers = HTTPHeaderDict() # Duplicate headers are legal
if self.content_type is not None:
headers["Content-Type"] = self.content_type
if self.headers:
headers.extend(self.headers)
return headers
def get_response(self, request: "PreparedRequest") -> HTTPResponse:
raise NotImplementedError
def matches(self, request: "PreparedRequest") -> Tuple[bool, str]:
if request.method != self.method:
return False, "Method does not match"
if not self._url_matches(self.url, str(request.url)):
return False, "URL does not match"
valid, reason = self._req_attr_matches(self.match, request)
if not valid:
return False, reason
return True, ""
def _form_response(
body: Union[BufferedReader, BytesIO],
headers: Optional[Mapping[str, str]],
status: int,
) -> HTTPResponse:
# The requests library's cookie handling depends on the response object
# having an original response object with the headers as the `msg`, so
# we give it what it needs.
data = BytesIO()
data.close()
orig_response = HTTPResponse(
body=data, # required to avoid "ValueError: Unable to determine whether fp is closed."
msg=headers,
preload_content=False,
)
return HTTPResponse(
status=status,
reason=client.responses.get(status, None),
body=body,
headers=headers,
original_response=orig_response,
preload_content=False,
)
class Response(BaseResponse):
def __init__(
self,
method: str,
url: _URLPatternType,
body: _Body = "",
json: Optional[Any] = None,
status: int = 200,
headers: Optional[Mapping[str, str]] = None,
stream: Optional[bool] = None,
content_type: Union[str, object] = _UNSET,
auto_calculate_content_length: bool = False,
**kwargs: Any,
) -> None:
# if we were passed a `json` argument,
# override the body and content_type
if json is not None:
assert not body
body = json_module.dumps(json)
if content_type is _UNSET:
content_type = "application/json"
if content_type is _UNSET:
if isinstance(body, str) and _has_unicode(body):
content_type = "text/plain; charset=utf-8"
else:
content_type = "text/plain"
self.body: _Body = body
self.status: int = status
self.headers: Optional[Mapping[str, str]] = headers
if stream is not None:
warn(
"stream argument is deprecated. Use stream parameter in request directly",
DeprecationWarning,
)
self.stream: Optional[bool] = stream
self.content_type: str = content_type # type: ignore[assignment]
self.auto_calculate_content_length: bool = auto_calculate_content_length
super().__init__(method, url, **kwargs)
def get_response(self, request: "PreparedRequest") -> HTTPResponse:
if self.body and isinstance(self.body, Exception):
raise self.body
headers = self.get_headers()
status = self.status
assert not isinstance(self.body, (Response, BaseException))
body = _handle_body(self.body)
if (
self.auto_calculate_content_length
and isinstance(body, BytesIO)
and "Content-Length" not in headers
):
content_length = len(body.getvalue())
headers["Content-Length"] = str(content_length)
return _form_response(body, headers, status)
def __repr__(self) -> str:
return (
"<Response(url='{url}' status={status} "
"content_type='{content_type}' headers='{headers}')>".format(
url=self.url,
status=self.status,
content_type=self.content_type,
headers=json_module.dumps(self.headers),
)
)
class CallbackResponse(BaseResponse):
def __init__(
self,
method: str,
url: _URLPatternType,
callback: Callable[[Any], Any],
stream: Optional[bool] = None,
content_type: Optional[str] = "text/plain",
**kwargs: Any,
) -> None:
self.callback = callback
if stream is not None:
warn(
"stream argument is deprecated. Use stream parameter in request directly",
DeprecationWarning,
)
self.stream: Optional[bool] = stream
self.content_type: Optional[str] = content_type
super().__init__(method, url, **kwargs)
def get_response(self, request: "PreparedRequest") -> HTTPResponse:
headers = self.get_headers()
result = self.callback(request)
if isinstance(result, Exception):
raise result
status, r_headers, body = result
if isinstance(body, Exception):
raise body
# If the callback set a content-type remove the one
# set in add_callback() so that we don't have multiple
# content type values.
has_content_type = False
if isinstance(r_headers, dict) and "Content-Type" in r_headers:
has_content_type = True
elif isinstance(r_headers, list):
has_content_type = any(
[h for h in r_headers if h and h[0].lower() == "content-type"]
)
if has_content_type:
headers.pop("Content-Type", None)
body = _handle_body(body)
headers.extend(r_headers)
return _form_response(body, headers, status)
class PassthroughResponse(BaseResponse):
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, passthrough=True, **kwargs)
class RequestsMock(object):
DELETE: Literal["DELETE"] = "DELETE"
GET: Literal["GET"] = "GET"
HEAD: Literal["HEAD"] = "HEAD"
OPTIONS: Literal["OPTIONS"] = "OPTIONS"
PATCH: Literal["PATCH"] = "PATCH"
POST: Literal["POST"] = "POST"
PUT: Literal["PUT"] = "PUT"
response_callback: Optional[Callable[[Any], Any]] = None
def __init__(
self,
assert_all_requests_are_fired: bool = True,
response_callback: Optional[Callable[[Any], Any]] = None,
passthru_prefixes: Tuple[str, ...] = (),
target: str = "requests.adapters.HTTPAdapter.send",
registry: Type[FirstMatchRegistry] = FirstMatchRegistry,
):
self._calls: CallList = CallList()
self.reset()
self._registry: FirstMatchRegistry = registry() # call only after reset
self.assert_all_requests_are_fired: bool = assert_all_requests_are_fired
self.response_callback: Optional[Callable[[Any], Response]] = response_callback
self.passthru_prefixes: Tuple[_URLPatternType, ...] = tuple(passthru_prefixes)
self.target: str = target
self._patcher: Optional["_mock_patcher[Any]"] = None
self._thread_lock = _ThreadingLock()
def get_registry(self) -> FirstMatchRegistry:
"""Returns current registry instance with responses.
Returns
-------
FirstMatchRegistry
Current registry instance with responses.
"""
return self._registry
def _set_registry(self, new_registry: Type[FirstMatchRegistry]) -> None:
"""Replaces current registry with `new_registry`.
Parameters
----------
new_registry : Type[FirstMatchRegistry]
Class reference of the registry that should be set, eg OrderedRegistry
"""
if self.registered():
err_msg = (
"Cannot replace Registry, current registry has responses.\n"
"Run 'responses.registry.reset()' first"
)
raise AttributeError(err_msg)
self._registry = new_registry()
def reset(self) -> None:
"""Resets registry (including type), calls, passthru_prefixes to default values."""
self._registry = FirstMatchRegistry()
self._calls.reset()
self.passthru_prefixes = ()
def add(
self,
method: _HTTPMethodOrResponse = None,
url: "Optional[_URLPatternType]" = None,
body: _Body = "",
adding_headers: _HeaderSet = None,
*args: Any,
**kwargs: Any,
) -> BaseResponse:
"""
>>> import responses
A basic request:
>>> responses.add(responses.GET, 'http://example.com')
You can also directly pass an object which implements the
``BaseResponse`` interface:
>>> responses.add(Response(...))
A JSON payload:
>>> responses.add(
>>> method='GET',
>>> url='http://example.com',
>>> json={'foo': 'bar'},
>>> )
Custom headers:
>>> responses.add(
>>> method='GET',
>>> url='http://example.com',
>>> headers={'X-Header': 'foo'},
>>> )
"""
if isinstance(method, BaseResponse):
return self._registry.add(method)
if adding_headers is not None:
kwargs.setdefault("headers", adding_headers)
if "content_type" in kwargs and "headers" in kwargs:
header_keys = [header.lower() for header in kwargs["headers"]]
if "content-type" in header_keys:
raise RuntimeError(
"You cannot define both `content_type` and `headers[Content-Type]`."
" Using the `content_type` kwarg is recommended."
)
assert url is not None
assert isinstance(method, str)
response = Response(method=method, url=url, body=body, **kwargs)
return self._registry.add(response)
delete = partialmethod(add, DELETE)
get = partialmethod(add, GET)
head = partialmethod(add, HEAD)
options = partialmethod(add, OPTIONS)
patch = partialmethod(add, PATCH)
post = partialmethod(add, POST)
put = partialmethod(add, PUT)
def _add_from_file(self, file_path: "Union[str, bytes, os.PathLike[Any]]") -> None:
with open(file_path, "rb") as file:
data = _toml.load(file)
for rsp in data["responses"]:
rsp = rsp["response"]
self.add(
method=rsp["method"],
url=rsp["url"],
body=rsp["body"],
status=rsp["status"],
content_type=rsp["content_type"],
auto_calculate_content_length=rsp["auto_calculate_content_length"],
)
def add_passthru(self, prefix: _URLPatternType) -> None:
"""
Register a URL prefix or regex to passthru any non-matching mock requests to.
For example, to allow any request to 'https://example.com', but require
mocks for the remainder, you would add the prefix as so:
>>> import responses
>>> responses.add_passthru('https://example.com')
Regex can be used like:
>>> import re
>>> responses.add_passthru(re.compile('https://example.com/\\w+'))
"""
if not isinstance(prefix, Pattern) and _has_unicode(prefix):
prefix = _clean_unicode(prefix)
self.passthru_prefixes += (prefix,)
def remove(
self,
method_or_response: _HTTPMethodOrResponse = None,
url: "Optional[_URLPatternType]" = None,
) -> List[BaseResponse]:
"""
Removes a response previously added using ``add()``, identified
either by a response object inheriting ``BaseResponse`` or
``method`` and ``url``. Removes all matching responses.
>>> import responses
>>> responses.add(responses.GET, 'http://example.org')
>>> responses.remove(responses.GET, 'http://example.org')
"""
if isinstance(method_or_response, BaseResponse):
response = method_or_response
else:
assert url is not None
assert isinstance(method_or_response, str)
response = BaseResponse(method=method_or_response, url=url)
return self._registry.remove(response)
def replace(
self,
method_or_response: _HTTPMethodOrResponse = None,
url: "Optional[_URLPatternType]" = None,
body: _Body = "",
*args: Any,
**kwargs: Any,
) -> BaseResponse:
"""
Replaces a response previously added using ``add()``. The signature
is identical to ``add()``. The response is identified using ``method``
and ``url``, and the first matching response is replaced.
>>> import responses
>>> responses.add(responses.GET, 'http://example.org', json={'data': 1})
>>> responses.replace(responses.GET, 'http://example.org', json={'data': 2})
"""
if isinstance(method_or_response, BaseResponse):
response = method_or_response
else:
assert url is not None
assert isinstance(method_or_response, str)
response = Response(method=method_or_response, url=url, body=body, **kwargs)
return self._registry.replace(response)
def upsert(
self,
method_or_response: _HTTPMethodOrResponse = None,
url: "Optional[_URLPatternType]" = None,
body: _Body = "",
*args: Any,
**kwargs: Any,
) -> BaseResponse:
"""
Replaces a response previously added using ``add()``, or adds the response
if no response exists. Responses are matched using ``method``and ``url``.
The first matching response is replaced.
>>> import responses
>>> responses.add(responses.GET, 'http://example.org', json={'data': 1})
>>> responses.upsert(responses.GET, 'http://example.org', json={'data': 2})
"""
try:
return self.replace(method_or_response, url, body, *args, **kwargs)
except ValueError:
return self.add(method_or_response, url, body, *args, **kwargs)
def add_callback(
self,
method: str,
url: _URLPatternType,
callback: Callable[
["PreparedRequest"], Union[Exception, Tuple[int, Mapping[str, str], _Body]]
],
match_querystring: Union[bool, FalseBool] = FalseBool(),
content_type: Optional[str] = "text/plain",
match: "_MatcherIterable" = (),
) -> None:
self._registry.add(
CallbackResponse(
url=url,
method=method,
callback=callback,
content_type=content_type,
match_querystring=match_querystring,
match=match,
)
)
def registered(self) -> List["BaseResponse"]:
return self._registry.registered
@property
def calls(self) -> CallList:
return self._calls
def __enter__(self) -> "RequestsMock":
self.start()
return self
def __exit__(self, type: Any, value: Any, traceback: Any) -> bool:
success = type is None
self.stop(allow_assert=success)
self.reset()
return success
@overload
def activate(self, func: _F = ...) -> _F:
"""Overload for scenario when 'responses.activate' is used."""
... # pragma: no cover
@overload
def activate(
self,
*,
registry: Type[Any] = ...,
assert_all_requests_are_fired: bool = ...,
) -> Callable[["_F"], "_F"]:
"""Overload for scenario when
'responses.activate(registry=, assert_all_requests_are_fired=True)' is used.
See https://github.com/getsentry/responses/pull/469 for more details
"""
... # pragma: no cover
def activate(
self,
func: Optional[_F] = None,
*,
registry: Optional[Type[Any]] = None,
assert_all_requests_are_fired: bool = False,
) -> Union[Callable[["_F"], "_F"], _F]:
if func is not None:
return get_wrapped(func, self)
def deco_activate(function: _F) -> Callable[..., Any]:
return get_wrapped(
function,
self,
registry=registry,
assert_all_requests_are_fired=assert_all_requests_are_fired,
)
return deco_activate
def _find_match(
self, request: "PreparedRequest"
) -> Tuple[Optional["BaseResponse"], List[str]]:
"""
Iterates through all available matches and validates if any of them matches the request
:param request: (PreparedRequest), request object
:return:
(Response) found match. If multiple found, then remove & return the first match.
(list) list with reasons why other matches don't match
"""
with self._thread_lock:
return self._registry.find(request)
def _parse_request_params(
self, url: str
) -> Dict[str, Union[str, int, float, List[Optional[Union[str, int, float]]]]]:
params: Dict[str, Union[str, int, float, List[Any]]] = {}
for key, val in groupby(parse_qsl(urlsplit(url).query), lambda kv: kv[0]):
values = list(map(lambda x: x[1], val))
if len(values) == 1:
values = values[0] # type: ignore[assignment]
params[key] = values
return params
def _on_request(
self,
adapter: "HTTPAdapter",
request: "PreparedRequest",
*,
retries: Optional["_Retry"] = None,
**kwargs: Any,
) -> "models.Response":
# add attributes params and req_kwargs to 'request' object for further match comparison
# original request object does not have these attributes
request.params = self._parse_request_params(request.path_url) # type: ignore[attr-defined]
request.req_kwargs = kwargs # type: ignore[attr-defined]
request_url = str(request.url)
match, match_failed_reasons = self._find_match(request)
resp_callback = self.response_callback
if match is None: