From f41d521ce51084f37c094a7a5368b77a7d0cd225 Mon Sep 17 00:00:00 2001
From: Arushit Mudgal <rshtmudgal@gmail.com>
Date: Thu, 25 Jan 2018 20:36:19 +0530
Subject: [PATCH] fix #2800

---
 mitmproxy/contentviews/base.py           |  5 +++--
 test/mitmproxy/contentviews/test_base.py | 18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/mitmproxy/contentviews/base.py b/mitmproxy/contentviews/base.py
index bdab1e995..dbaa6cccf 100644
--- a/mitmproxy/contentviews/base.py
+++ b/mitmproxy/contentviews/base.py
@@ -49,8 +49,9 @@ def format_dict(
     ]
     entries, where key is padded to a uniform width.
     """
-    max_key_len = max(len(k) for k in d.keys())
-    max_key_len = min(max_key_len, KEY_MAX)
+
+    max_key_len = max((len(k) for k in d.keys()), default=0)
+    max_key_len = min((max_key_len, KEY_MAX), default=0)
     for key, value in d.items():
         if isinstance(key, bytes):
             key += b":"
diff --git a/test/mitmproxy/contentviews/test_base.py b/test/mitmproxy/contentviews/test_base.py
index 777ab4dd1..c94d8be2a 100644
--- a/test/mitmproxy/contentviews/test_base.py
+++ b/test/mitmproxy/contentviews/test_base.py
@@ -1 +1,17 @@
-# TODO: write tests
+import pytest
+from mitmproxy.contentviews import base
+
+
+def test_format_dict():
+    d = {"one": "two", "three": "four"}
+    f_d = base.format_dict(d)
+    assert next(f_d)
+
+    d = {"adsfa": ""}
+    f_d = base.format_dict(d)
+    assert next(f_d)
+
+    d = {}
+    f_d = base.format_dict(d)
+    with pytest.raises(StopIteration):
+        next(f_d)