diff options
-rw-r--r-- | mitmproxy/connections.py | 10 | ||||
-rw-r--r-- | mitmproxy/flow.py | 1 | ||||
-rw-r--r-- | mitmproxy/types/serializable.py | 6 | ||||
-rw-r--r-- | test/mitmproxy/test_connections.py | 1 | ||||
-rw-r--r-- | test/mitmproxy/types/test_serializable.py | 19 |
5 files changed, 21 insertions, 16 deletions
diff --git a/mitmproxy/connections.py b/mitmproxy/connections.py index 5a3cb69e..01721a71 100644 --- a/mitmproxy/connections.py +++ b/mitmproxy/connections.py @@ -83,11 +83,6 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject): def __hash__(self): return hash(self.id) - def copy(self): - f = super().copy() - f.id = str(uuid.uuid4()) - return f - @property def tls_established(self): return self.ssl_established @@ -222,11 +217,6 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject): def __hash__(self): return hash(self.id) - def copy(self): - f = super().copy() - f.id = str(uuid.uuid4()) - return f - @property def tls_established(self): return self.ssl_established diff --git a/mitmproxy/flow.py b/mitmproxy/flow.py index 73d6090e..7ad71516 100644 --- a/mitmproxy/flow.py +++ b/mitmproxy/flow.py @@ -112,7 +112,6 @@ class Flow(stateobject.StateObject): def copy(self): f = super().copy() - f.id = str(uuid.uuid4()) f.live = False return f diff --git a/mitmproxy/types/serializable.py b/mitmproxy/types/serializable.py index 49892ffc..cd8539b0 100644 --- a/mitmproxy/types/serializable.py +++ b/mitmproxy/types/serializable.py @@ -1,4 +1,5 @@ import abc +import uuid class Serializable(metaclass=abc.ABCMeta): @@ -29,4 +30,7 @@ class Serializable(metaclass=abc.ABCMeta): raise NotImplementedError() def copy(self): - return self.from_state(self.get_state()) + state = self.get_state() + if isinstance(state, dict) and "id" in state: + state["id"] = str(uuid.uuid4()) + return self.from_state(state) diff --git a/test/mitmproxy/test_connections.py b/test/mitmproxy/test_connections.py index 57fdd8c7..67a6552f 100644 --- a/test/mitmproxy/test_connections.py +++ b/test/mitmproxy/test_connections.py @@ -78,6 +78,7 @@ class TestClientConnection: assert c != 42 assert hash(c) != hash(c2) + class TestServerConnection: def test_send(self): diff --git a/test/mitmproxy/types/test_serializable.py b/test/mitmproxy/types/test_serializable.py index dd4a3778..390d17e1 100644 --- a/test/mitmproxy/types/test_serializable.py +++ b/test/mitmproxy/types/test_serializable.py @@ -1,3 +1,5 @@ +import copy + from mitmproxy.types import serializable @@ -6,17 +8,17 @@ class SerializableDummy(serializable.Serializable): self.i = i def get_state(self): - return self.i + return copy.copy(self.i) def set_state(self, i): self.i = i - def from_state(self, state): - return type(self)(state) + @classmethod + def from_state(cls, state): + return cls(state) class TestSerializable: - def test_copy(self): a = SerializableDummy(42) assert a.i == 42 @@ -26,3 +28,12 @@ class TestSerializable: a.set_state(1) assert a.i == 1 assert b.i == 42 + + def test_copy_id(self): + a = SerializableDummy({ + "id": "foo", + "foo": 42 + }) + b = a.copy() + assert a.get_state()["id"] != b.get_state()["id"] + assert a.get_state()["foo"] == b.get_state()["foo"] |