Repository: kevin1024/vcrpy

Issue:
allow_playback_repeats
Ruby VCR has a Cassette option [allow_playback_repeats](https://relishapp.com/vcr/vcr/docs/request-matching/playback-repeats). Any chance this can be added to vcrpy?

Discussion:
tysonholub: Hi, this was a fairly simple feature to implement. Wondering if it will be merged in or not. Thanks!
jairhenrique: @tysonholub please rebase this code with master.
jairhenrique: @tysonholub can you add some docs about this option?
tysonholub: All set, thanks!
jairhenrique: @tysonholub great! Can you fix this lint error: https://travis-ci.org/github/kevin1024/vcrpy/jobs/708423571

@kevin1024 can you merge this PR after CI success?
tysonholub: Woops. Fixed
codecov-commenter: # [Codecov](https://codecov.io/gh/kevin1024/vcrpy/pull/541?src=pr&el=h1) Report
> Merging [#541](https://codecov.io/gh/kevin1024/vcrpy/pull/541?src=pr&el=desc) into [master](https://codecov.io/gh/kevin1024/vcrpy/commit/6e040030b8d58ef654c429f953a73e977ff0f594&el=desc) will **increase** coverage by `0.10%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/kevin1024/vcrpy/pull/541/graphs/tree.svg?width=650&height=150&src=pr&token=YOLqUk37fb)](https://codecov.io/gh/kevin1024/vcrpy/pull/541?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #541      +/-   ##
==========================================
+ Coverage   89.01%   89.12%   +0.10%     
==========================================
  Files          24       25       +1     
  Lines        1539     1600      +61     
  Branches      212      228      +16     
==========================================
+ Hits         1370     1426      +56     
- Misses        139      140       +1     
- Partials       30       34       +4     
```


| [Impacted Files](https://codecov.io/gh/kevin1024/vcrpy/pull/541?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [vcr/cassette.py](https://codecov.io/gh/kevin1024/vcrpy/pull/541/diff?src=pr&el=tree#diff-dmNyL2Nhc3NldHRlLnB5) | `96.20% <100.00%> (+0.03%)` | :arrow_up: |
| [vcr/config.py](https://codecov.io/gh/kevin1024/vcrpy/pull/541/diff?src=pr&el=tree#diff-dmNyL2NvbmZpZy5weQ==) | `95.00% <100.00%> (+0.03%)` | :arrow_up: |
| [vcr/record\_mode.py](https://codecov.io/gh/kevin1024/vcrpy/pull/541/diff?src=pr&el=tree#diff-dmNyL3JlY29yZF9tb2RlLnB5) | `100.00% <100.00%> (ø)` | |
| [vcr/stubs/aiohttp\_stubs/\_\_init\_\_.py](https://codecov.io/gh/kevin1024/vcrpy/pull/541/diff?src=pr&el=tree#diff-dmNyL3N0dWJzL2Fpb2h0dHBfc3R1YnMvX19pbml0X18ucHk=) | | |
| [vcr/stubs/aiohttp\_stubs.py](https://codecov.io/gh/kevin1024/vcrpy/pull/541/diff?src=pr&el=tree#diff-dmNyL3N0dWJzL2Fpb2h0dHBfc3R1YnMucHk=) | `93.03% <0.00%> (ø)` | |
| [vcr/patch.py](https://codecov.io/gh/kevin1024/vcrpy/pull/541/diff?src=pr&el=tree#diff-dmNyL3BhdGNoLnB5) | `84.71% <0.00%> (+0.34%)` | :arrow_up: |
| [vcr/stubs/\_\_init\_\_.py](https://codecov.io/gh/kevin1024/vcrpy/pull/541/diff?src=pr&el=tree#diff-dmNyL3N0dWJzL19faW5pdF9fLnB5) | `93.64% <0.00%> (+2.21%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kevin1024/vcrpy/pull/541?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kevin1024/vcrpy/pull/541?src=pr&el=footer). Last update [2f53776...2201f38](https://codecov.io/gh/kevin1024/vcrpy/pull/541?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/advanced.rst b/docs/advanced.rst
index 5536055..b16710b 100644
--- a/docs/advanced.rst
+++ b/docs/advanced.rst
@@ -33,6 +33,8 @@ consider part of the API. The fields are as follows:
    been played back.
 -  ``responses_of(request)``: Access the responses that match a given
    request
+-  ``allow_playback_repeats``: A boolean indicating whether responses
+   can be played back more than once.
 
 The ``Request`` object has the following properties:
 
@@ -386,3 +388,19 @@ VCR.py allows to rewind a cassette in order to replay it inside the same functio
         assert cass.all_played
         cass.rewind()
         assert not cass.all_played
+
+Playback Repeats
+----------------
+
+By default, each response in a cassette can only be matched and played back
+once while the cassette is in use, unless the cassette is rewound.
+
+If you want to allow playback repeats without rewinding the cassette, use
+the Cassette ``allow_playback_repeats`` option.
+
+.. code:: python
+
+    with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml', allow_playback_repeats=True) as cass:
+        for x in range(10):
+            response = urllib2.urlopen('http://www.zombo.com/').read()
+        assert cass.all_played
diff --git a/vcr/cassette.py b/vcr/cassette.py
index 826a68b..9122b4a 100644
--- a/vcr/cassette.py
+++ b/vcr/cassette.py
@@ -182,6 +182,7 @@ class Cassette:
         before_record_response=None,
         custom_patches=(),
         inject=False,
+        allow_playback_repeats=False,
     ):
         self._persister = persister or FilesystemPersister
         self._path = path
@@ -193,6 +194,7 @@ class Cassette:
         self.inject = inject
         self.record_mode = record_mode
         self.custom_patches = custom_patches
+        self.allow_playback_repeats = allow_playback_repeats
 
         # self.data is the list of (req, resp) tuples
         self.data = []
@@ -207,7 +209,7 @@ class Cassette:
     @property
     def all_played(self):
         """Returns True if all responses have been played, False otherwise."""
-        return self.play_count == len(self)
+        return len(self.play_counts.values()) == len(self)
 
     @property
     def requests(self):
@@ -259,7 +261,7 @@ class Cassette:
         hasn't been played back before, and mark it as played
         """
         for index, response in self._responses(request):
-            if self.play_counts[index] == 0:
+            if self.play_counts[index] == 0 or self.allow_playback_repeats:
                 self.play_counts[index] += 1
                 return response
         # The cassette doesn't contain the request asked for.
@@ -349,6 +351,6 @@ class Cassette:
     def __contains__(self, request):
         """Return whether or not a request has been stored"""
         for index, response in self._responses(request):
-            if self.play_counts[index] == 0:
+            if self.play_counts[index] == 0 or self.allow_playback_repeats:
                 return True
         return False
diff --git a/vcr/config.py b/vcr/config.py
index 7dfd1a8..e95908c 100644
--- a/vcr/config.py
+++ b/vcr/config.py
@@ -149,6 +149,7 @@ class VCR:
             "inject": kwargs.get("inject_cassette", self.inject_cassette),
             "path_transformer": path_transformer,
             "func_path_generator": func_path_generator,
+            "allow_playback_repeats": kwargs.get("allow_playback_repeats", False),
         }
         path = kwargs.get("path")
         if path:


Test patch:
diff --git a/tests/unit/test_cassettes.py b/tests/unit/test_cassettes.py
index 90a05c7..adbc77a 100644
--- a/tests/unit/test_cassettes.py
+++ b/tests/unit/test_cassettes.py
@@ -137,6 +137,31 @@ def test_cassette_all_played():
     assert a.all_played
 
 
+@mock.patch("vcr.cassette.requests_match", _mock_requests_match)
+def test_cassette_allow_playback_repeats():
+    a = Cassette("test", allow_playback_repeats=True)
+    a.append("foo", "bar")
+    a.append("other", "resp")
+    for x in range(10):
+        assert a.play_response("foo") == "bar"
+    assert a.play_count == 10
+    assert a.all_played is False
+    assert a.play_response("other") == "resp"
+    assert a.play_count == 11
+    assert a.all_played
+
+    a.allow_playback_repeats = False
+    with pytest.raises(UnhandledHTTPRequestError) as e:
+        a.play_response("foo")
+    assert str(e.value) == "\"The cassette ('test') doesn't contain the request ('foo') asked for\""
+    a.rewind()
+    assert a.all_played is False
+    assert a.play_response("foo") == "bar"
+    assert a.all_played is False
+    assert a.play_response("other") == "resp"
+    assert a.all_played
+
+
 @mock.patch("vcr.cassette.requests_match", _mock_requests_match)
 def test_cassette_rewound():
     a = Cassette("test")