2019-10-22 23:27:27 -04:00
|
|
|
// Copyright 2019 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xml // import "miniflux.app/reader/xml"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-12-19 21:31:52 -05:00
|
|
|
func TestUTF8WithIllegalCharacters(t *testing.T) {
|
2019-10-22 23:27:27 -04:00
|
|
|
type myxml struct {
|
|
|
|
XMLName xml.Name `xml:"rss"`
|
|
|
|
Version string `xml:"version,attr"`
|
|
|
|
Title string `xml:"title"`
|
|
|
|
}
|
|
|
|
|
2019-12-19 21:31:52 -05:00
|
|
|
expected := "Title & 中文标题"
|
|
|
|
data := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
|
|
|
|
reader := strings.NewReader(data)
|
|
|
|
|
|
|
|
var x myxml
|
|
|
|
|
|
|
|
decoder := NewDecoder(reader)
|
|
|
|
err := decoder.Decode(&x)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if x.Title != expected {
|
|
|
|
t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWindows251WithIllegalCharacters(t *testing.T) {
|
|
|
|
type myxml struct {
|
|
|
|
XMLName xml.Name `xml:"rss"`
|
|
|
|
Version string `xml:"version,attr"`
|
|
|
|
Title string `xml:"title"`
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "Title & 中文标题"
|
|
|
|
data := fmt.Sprintf(`<?xml version="1.0" encoding="windows-1251"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
|
|
|
|
reader := strings.NewReader(data)
|
|
|
|
|
2019-10-22 23:27:27 -04:00
|
|
|
var x myxml
|
|
|
|
|
2019-12-19 21:31:52 -05:00
|
|
|
decoder := NewDecoder(reader)
|
2019-10-22 23:27:27 -04:00
|
|
|
err := decoder.Decode(&x)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2019-12-19 21:31:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if x.Title != expected {
|
|
|
|
t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIllegalEncodingField(t *testing.T) {
|
|
|
|
type myxml struct {
|
|
|
|
XMLName xml.Name `xml:"rss"`
|
|
|
|
Version string `xml:"version,attr"`
|
|
|
|
Title string `xml:"title"`
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "Title & 中文标题"
|
|
|
|
data := fmt.Sprintf(`<?xml version="1.0" encoding="invalid"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
|
|
|
|
reader := strings.NewReader(data)
|
|
|
|
|
|
|
|
var x myxml
|
|
|
|
|
|
|
|
decoder := NewDecoder(reader)
|
|
|
|
err := decoder.Decode(&x)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if x.Title != expected {
|
|
|
|
t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
|
2019-10-22 23:27:27 -04:00
|
|
|
}
|
|
|
|
}
|