Repository: getsentry/responses

Issue:
ability to pass a regex to add_passthru 
e.g:
`responses.add_passthru(re.compile(r'http(s?):\/\/(\w+)(:*)(\d*)\/api\/v1\/activities\/'))` 
so that calls to any domain[:port] with this request path may call the upstream server.

Discussion:

Patch:
diff --git a/README.rst b/README.rst
index 6ba7643..94c27b3 100644
--- a/README.rst
+++ b/README.rst
@@ -352,7 +352,7 @@ Passing thru real requests
 --------------------------
 
 In some cases you may wish to allow for certain requests to pass thru responses
-and hit a real server. This can be done with the 'passthru' methods:
+and hit a real server. This can be done with the ``add_passthru' methods:
 
 .. code-block:: python
 
@@ -365,6 +365,12 @@ and hit a real server. This can be done with the 'passthru' methods:
 This will allow any requests matching that prefix, that is otherwise not registered
 as a mock response, to passthru using the standard behavior.
 
+Regex can be used like:
+
+.. code-block:: python
+
+    responses.add_passthru(re.compile('https://percy.io/\\w+'))
+
 
 Viewing/Modifying registered responses
 --------------------------------------
diff --git a/responses.py b/responses.py
index bc54b78..3f32125 100644
--- a/responses.py
+++ b/responses.py
@@ -527,14 +527,18 @@ class RequestsMock(object):
 
     def add_passthru(self, prefix):
         """
-        Register a URL prefix to passthru any non-matching mock requests to.
+        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:
 
         >>> responses.add_passthru('https://example.com')
+
+        Regex can be used like:
+
+        >>> responses.add_passthru(re.compile('https://example.com/\\w+'))
         """
-        if _has_unicode(prefix):
+        if not isinstance(prefix, Pattern) and _has_unicode(prefix):
             prefix = _clean_unicode(prefix)
         self.passthru_prefixes += (prefix,)
 
@@ -624,7 +628,14 @@ class RequestsMock(object):
         resp_callback = self.response_callback
 
         if match is None:
-            if request.url.startswith(self.passthru_prefixes):
+            if any(
+                [
+                    p.match(request.url)
+                    if isinstance(p, Pattern)
+                    else request.url.startswith(p)
+                    for p in self.passthru_prefixes
+                ]
+            ):
                 logger.info("request.allowed-passthru", extra={"url": request.url})
                 return _real_send(adapter, request, **kwargs)
 


Test patch:
diff --git a/test_responses.py b/test_responses.py
index 223c0a7..41400ee 100644
--- a/test_responses.py
+++ b/test_responses.py
@@ -943,6 +943,28 @@ def test_passthru(httpserver):
     assert_reset()
 
 
+def test_passthru_regex(httpserver):
+    httpserver.serve_content("OK", headers={"Content-Type": "text/plain"})
+
+    @responses.activate
+    def run():
+        responses.add_passthru(re.compile("{}/\\w+".format(httpserver.url)))
+        responses.add(responses.GET, "{}/one".format(httpserver.url), body="one")
+        responses.add(responses.GET, "http://example.com/two", body="two")
+
+        resp = requests.get("http://example.com/two")
+        assert_response(resp, "two")
+        resp = requests.get("{}/one".format(httpserver.url))
+        assert_response(resp, "one")
+        resp = requests.get("{}/two".format(httpserver.url))
+        assert_response(resp, "OK")
+        resp = requests.get("{}/three".format(httpserver.url))
+        assert_response(resp, "OK")
+
+    run()
+    assert_reset()
+
+
 def test_method_named_param():
     @responses.activate
     def run():