mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2026-02-06 00:58:33 +00:00
feat: handle foreign encodings
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@ -19,6 +20,30 @@ func newTestParser(t *testing.T, msg string) *Parser {
|
||||
return p
|
||||
}
|
||||
|
||||
func TestParserSpecifiedLatin1Charset(t *testing.T) {
|
||||
p := newTestParser(t, "text_plain_latin1.eml")
|
||||
|
||||
checkBodies(t, p, "ééééééé")
|
||||
}
|
||||
|
||||
func TestParserUnspecifiedLatin1Charset(t *testing.T) {
|
||||
p := newTestParser(t, "text_plain_unknown_latin1.eml")
|
||||
|
||||
checkBodies(t, p, "ééééééé")
|
||||
}
|
||||
|
||||
func TestParserSpecifiedLatin2Charset(t *testing.T) {
|
||||
p := newTestParser(t, "text_plain_latin2.eml")
|
||||
|
||||
checkBodies(t, p, "řšřšřš")
|
||||
}
|
||||
|
||||
func TestParserEmbeddedLatin2Charset(t *testing.T) {
|
||||
p := newTestParser(t, "text_html_embedded_latin2_encoding.eml")
|
||||
|
||||
checkBodies(t, p, `<html><head><meta charset="ISO-8859-2"></head><body>latin2 řšřš</body></html>`)
|
||||
}
|
||||
|
||||
func f(filename string) io.ReadCloser {
|
||||
f, err := os.Open(filepath.Join("testdata", filename))
|
||||
|
||||
@ -37,3 +62,21 @@ func s(filename string) string {
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func checkBodies(t *testing.T, p *Parser, wantBodies ...string) {
|
||||
var partBodies, expectedBodies [][]byte
|
||||
|
||||
require.NoError(t, p.NewWalker().RegisterDefaultHandler(func(p *Part) (err error) {
|
||||
if p.Body != nil {
|
||||
partBodies = append(partBodies, p.Body)
|
||||
}
|
||||
|
||||
return
|
||||
}).Walk())
|
||||
|
||||
for _, body := range wantBodies {
|
||||
expectedBodies = append(expectedBodies, []byte(body))
|
||||
}
|
||||
|
||||
assert.ElementsMatch(t, expectedBodies, partBodies)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user