metainfo: Omit empty Metainfo.InfoBytes

This commit is contained in:
Matt Joiner 2017-11-08 19:57:05 +11:00
parent 163a63f9a7
commit da9cbc7cf0
2 changed files with 12 additions and 1 deletions

View File

@ -9,7 +9,7 @@ import (
)
type MetaInfo struct {
InfoBytes bencode.Bytes `bencode:"info"`
InfoBytes bencode.Bytes `bencode:"info,omitempty"`
Announce string `bencode:"announce,omitempty"`
AnnounceList AnnounceList `bencode:"announce-list,omitempty"`
Nodes []Node `bencode:"nodes,omitempty"`

View File

@ -1,6 +1,7 @@
package metainfo
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
@ -61,3 +62,13 @@ func TestUnmarshalBadMetainfoNodes(t *testing.T) {
err := bencode.Unmarshal([]byte("d5:nodesl1:ai42eee"), &mi)
require.Error(t, err)
}
func TestMetainfoEmptyInfoBytes(t *testing.T) {
var buf bytes.Buffer
require.NoError(t, (&MetaInfo{
// Include a non-empty field that comes after "info".
UrlList: []string{"hello"},
}).Write(&buf))
var mi MetaInfo
require.NoError(t, bencode.Unmarshal(buf.Bytes(), &mi))
}