Refactor peer dict creation in the announce test

This commit is contained in:
Justin Li 2014-07-15 20:50:33 -04:00
parent c36974d515
commit bec70e9759

View file

@ -28,52 +28,38 @@ func TestPublicAnnounce(t *testing.T) {
defer srv.Close() defer srv.Close()
// Add one seeder. // Add one seeder.
peer := basePeer("peer1", true) peer := makePeerParams("peer1", true)
expected := baseResponse(1, 0, bencode.List{}) expected := makeResponse(1, 0, bencode.List{})
checkResponse(peer, expected, srv, t) checkResponse(peer, expected, srv, t)
// Add another seeder. // Add another seeder.
peer = basePeer("peer2", true) peer = makePeerParams("peer2", true)
expected = baseResponse(2, 0, bencode.List{}) expected = makeResponse(2, 0, bencode.List{})
checkResponse(peer, expected, srv, t) checkResponse(peer, expected, srv, t)
// Add a leecher. // Add a leecher.
peer = basePeer("peer3", false) peer = makePeerParams("peer3", false)
expected = baseResponse(2, 1, bencode.List{ expected = makeResponse(2, 1, bencode.List{
bencode.Dict{ makePeerResponse("peer1"),
"ip": "127.0.0.1", makePeerResponse("peer2"),
"peer id": "peer1",
"port": int64(1234),
},
bencode.Dict{
"ip": "127.0.0.1",
"peer id": "peer2",
"port": int64(1234),
},
}) })
checkResponse(peer, expected, srv, t) checkResponse(peer, expected, srv, t)
// Remove seeder. // Remove seeder.
peer = basePeer("peer1", true) peer = makePeerParams("peer1", true)
peer["event"] = "stopped" peer["event"] = "stopped"
expected = baseResponse(1, 1, nil) expected = makeResponse(1, 1, nil)
checkResponse(peer, expected, srv, t) checkResponse(peer, expected, srv, t)
// Check seeders. // Check seeders.
peer = basePeer("peer3", false) peer = makePeerParams("peer3", false)
expected = baseResponse(1, 1, bencode.List{ expected = makeResponse(1, 1, bencode.List{
bencode.Dict{ makePeerResponse("peer2"),
"ip": "127.0.0.1",
"peer id": "peer2",
"port": int64(1234),
},
}) })
checkResponse(peer, expected, srv, t) checkResponse(peer, expected, srv, t)
} }
func basePeer(id string, seed bool) params { func makePeerParams(id string, seed bool) params {
left := "1" left := "1"
if seed { if seed {
left = "0" left = "0"
@ -91,7 +77,15 @@ func basePeer(id string, seed bool) params {
} }
} }
func baseResponse(seeders, leechers int64, peers bencode.List) bencode.Dict { func makePeerResponse(id string) bencode.Dict {
return bencode.Dict{
"ip": "127.0.0.1",
"peer id": id,
"port": int64(1234),
}
}
func makeResponse(seeders, leechers int64, peers bencode.List) bencode.Dict {
dict := bencode.Dict{ dict := bencode.Dict{
"complete": seeders, "complete": seeders,
"incomplete": leechers, "incomplete": leechers,