parser fixes

This commit is contained in:
Nazar Kanaev 2021-03-26 16:52:33 +00:00
parent cc51fe01c2
commit fafa6286d4
5 changed files with 39 additions and 10 deletions

View File

@ -23,7 +23,7 @@ type atomEntry struct {
Published string `xml:"published"`
Updated string `xml:"updated"`
Links atomLinks `xml:"link"`
Content atomText `xml:"content"`
Content atomText `xml:"http://www.w3.org/2005/Atom content"`
media
}

View File

@ -56,3 +56,24 @@ func TestAtom(t *testing.T) {
t.Fatal("invalid atom")
}
}
func TestAtomClashingNamespaces(t *testing.T) {
have, err := Parse(strings.NewReader(`
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<content>atom content</content>
<media:content xmlns:media="http://search.yahoo.com/mrss/" />
</entry>
</feed>
`))
want := &Feed{Items: []Item{{Content: "atom content"}}}
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, have) {
t.Logf("want: %#v", want)
t.Logf("have: %#v", have)
t.FailNow()
}
}

View File

@ -46,11 +46,11 @@ func Parse(r io.Reader) (*Feed, error) {
lookup := make([]byte, 1024)
n, err := io.ReadFull(r, lookup)
switch {
case err != nil:
return nil, err
case err == io.ErrUnexpectedEOF:
lookup = lookup[:n]
r = bytes.NewReader(lookup)
case err != nil:
return nil, err
default:
r = io.MultiReader(bytes.NewReader(lookup), r)
}

View File

@ -77,3 +77,18 @@ func TestParse(t *testing.T) {
t.Fatal("invalid content")
}
}
func TestParseShortFeed(t *testing.T) {
have, err := Parse(strings.NewReader(
`<?xml version="1.0"?><feed xmlns="http://www.w3.org/2005/Atom"></feed>`,
))
want := &Feed{}
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, have) {
t.Logf("want: %#v", want)
t.Logf("have: %#v", have)
t.FailNow()
}
}

View File

@ -12,13 +12,6 @@ type mediaGroup struct {
MediaDescriptions []mediaDescription `xml:"http://search.yahoo.com/mrss/ description"`
}
type mediaContent struct {
URL string `xml:"url,attr"`
Type string `xml:"type,attr"`
FileSize string `xml:"fileSize,attr"`
Medium string `xml:"medium,attr"`
}
type mediaThumbnail struct {
URL string `xml:"url,attr"`
}