Always make sure we have even the optional attributes of PingResponse

This commit is contained in:
Nathan Adams
2014-09-26 00:02:02 +02:00
parent a8d9c482e7
commit 18af499c62
2 changed files with 13 additions and 0 deletions

View File

@@ -103,6 +103,8 @@ class PingResponse:
if type(raw["sample"]) is not list: if type(raw["sample"]) is not list:
raise ValueError("Invalid players object (expected 'sample' to be list, was %s)" % type(raw["max"])) raise ValueError("Invalid players object (expected 'sample' to be list, was %s)" % type(raw["max"]))
self.sample = [PingResponse.Players.Player(p) for p in raw["sample"]] self.sample = [PingResponse.Players.Player(p) for p in raw["sample"]]
else:
self.sample = None
class Version: class Version:
def __init__(self, raw): def __init__(self, raw):
@@ -138,5 +140,7 @@ class PingResponse:
if "favicon" in raw: if "favicon" in raw:
self.favicon = raw["favicon"] self.favicon = raw["favicon"]
else:
self.favicon = None
self.latency = None self.latency = None

View File

@@ -97,6 +97,11 @@ class TestPingResponse(TestCase):
self.assertEqual(response.favicon, "data:image/png;base64,foo") self.assertEqual(response.favicon, "data:image/png;base64,foo")
def test_favicon_missing(self):
response = PingResponse({"description":"A Minecraft Server","players":{"max":20,"online":0},"version":{"name":"1.8-pre1","protocol":44}})
self.assertIsNone(response.favicon)
class TestPingResponsePlayers(TestCase): class TestPingResponsePlayers(TestCase):
def test_invalid(self): def test_invalid(self):
self.assertRaises(ValueError, PingResponse.Players, "foo") self.assertRaises(ValueError, PingResponse.Players, "foo")
@@ -128,6 +133,10 @@ class TestPingResponsePlayers(TestCase):
def test_sample_invalid(self): def test_sample_invalid(self):
self.assertRaises(ValueError, PingResponse.Players, {"max":20,"online":1,"sample":"foo"}) self.assertRaises(ValueError, PingResponse.Players, {"max":20,"online":1,"sample":"foo"})
def test_sample_missing(self):
players = PingResponse.Players({"max":20,"online":1})
self.assertIsNone(players.sample)
class TestPingResponsePlayersPlayer(TestCase): class TestPingResponsePlayersPlayer(TestCase):
def test_invalid(self): def test_invalid(self):