Skip to content

Commit b9b5777

Browse files
ilyabreevKrusen
authored andcommitted
Store torrent 'announce' field correctly as BString instead of BList
Fixes #30
1 parent ee19a5f commit b9b5777

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

BencodeNET.Tests/Torrents/TorrentTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,46 @@ public void ToBDictionary_Announce_SingleTracker_AddsAnnounceButNotAddAnnounceLi
276276
result.Should().NotContainKey(TorrentFields.AnnounceList);
277277
}
278278

279+
[Fact]
280+
public void ToBDictionary_Announce_SingleTracker_AnnounceMustBeAString()
281+
{
282+
var torrent = new Torrent
283+
{
284+
Trackers = new List<IList<string>>
285+
{
286+
new List<string>
287+
{
288+
"http://sometracker.com"
289+
}
290+
}
291+
};
292+
293+
var result = torrent.ToBDictionary();
294+
result[TorrentFields.Announce].Should().BeAssignableTo<BString>();
295+
}
296+
297+
[Fact]
298+
public void ToBDictionary_Announce_MultipleTrackers_AddAnnounceAndAnnounceList()
299+
{
300+
const string firstTracker = "http://sometracker.com";
301+
var torrent = new Torrent
302+
{
303+
Trackers = new List<IList<string>>
304+
{
305+
new List<string>
306+
{
307+
firstTracker,
308+
"http://someothertracker.com"
309+
}
310+
}
311+
};
312+
313+
var result = torrent.ToBDictionary();
314+
result.Should().ContainKey(TorrentFields.Announce);
315+
result.Should().ContainKey(TorrentFields.AnnounceList);
316+
result[TorrentFields.Announce].ToString().Should().Be(firstTracker);
317+
}
318+
279319
[Theory]
280320
[AutoMockedData]
281321
public void ToBDictionary_Comment_AddsToCorrectField(string comment)

BencodeNET/Torrents/Torrent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public virtual BDictionary ToBDictionary()
228228
var trackerCount = Trackers.Flatten().Count();
229229

230230
if (trackerCount > 0)
231-
torrent[TorrentFields.Announce] = new BList(Trackers.First().Select(x => new BString(x, Encoding)));
231+
torrent[TorrentFields.Announce] = new BString(Trackers.Flatten().First(), Encoding);
232232

233233
if (trackerCount > 1)
234234
torrent[TorrentFields.AnnounceList] = new BList(Trackers.Select(x => new BList(x, Encoding)));

0 commit comments

Comments
 (0)