diff options
Diffstat (limited to 'reader/parser/parser_test.go')
-rw-r--r-- | reader/parser/parser_test.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/reader/parser/parser_test.go b/reader/parser/parser_test.go index 7e3f900..8f0f162 100644 --- a/reader/parser/parser_test.go +++ b/reader/parser/parser_test.go @@ -5,7 +5,11 @@ package parser // import "miniflux.app/reader/parser" import ( + "bytes" + "io/ioutil" "testing" + + "miniflux.app/http/client" ) func TestParseAtom(t *testing.T) { @@ -150,3 +154,47 @@ func TestParseEmptyFeed(t *testing.T) { t.Error("ParseFeed must returns an error") } } + +func TestDifferentEncodingWithResponse(t *testing.T) { + var unicodeTestCases = []struct { + filename, contentType string + index int + title string + }{ + // Arabic language encoded in UTF-8. + {"urdu_UTF8.xml", "text/xml; charset=utf-8", 0, "امریکی عسکری امداد کی بندش کی وجوہات: انڈیا سے جنگ، جوہری پروگرام اور اب دہشت گردوں کی پشت پناہی"}, + + // Windows-1251 encoding and not charset in HTTP header. + {"encoding_WINDOWS-1251.xml", "text/xml", 0, "Цитата #17703"}, + + // No encoding in XML, but defined in HTTP Content-Type header. + {"no_encoding_ISO-8859-1.xml", "application/xml; charset=ISO-8859-1", 2, "La criminalité liée surtout à... l'ennui ?"}, + + // ISO-8859-1 encoding defined in XML and HTTP header. + {"encoding_ISO-8859-1.xml", "application/rss+xml; charset=ISO-8859-1", 5, "Projekt Jedi: Microsoft will weiter mit US-Militär zusammenarbeiten"}, + + // UTF-8 encoding defined in RDF document and HTTP header. + {"rdf_UTF8.xml", "application/rss+xml; charset=utf-8", 1, "Mega-Deal: IBM übernimmt Red Hat"}, + + // UTF-8 encoding defined only in RDF document. + {"rdf_UTF8.xml", "application/rss+xml", 1, "Mega-Deal: IBM übernimmt Red Hat"}, + } + + for _, tc := range unicodeTestCases { + content, err := ioutil.ReadFile("testdata/" + tc.filename) + if err != nil { + t.Fatalf(`Unable to read file %q: %v`, tc.filename, err) + } + + r := &client.Response{Body: bytes.NewReader(content), ContentType: tc.contentType} + r.EnsureUnicodeBody() + feed, parseErr := ParseFeed(r.String()) + if parseErr != nil { + t.Errorf(`Parsing error for %q - %q: %v`, tc.filename, tc.contentType, parseErr) + } + + if feed.Entries[tc.index].Title != tc.title { + t.Errorf(`Unexpected title, got %q instead of %q`, feed.Entries[tc.index].Title, tc.title) + } + } +} |