Fix decoding with mix of decoded and encoded

This commit is contained in:
Philip Whitehouse 2017-08-31 22:01:07 +01:00
parent f88c3594fc
commit 0e9e4daa80
2 changed files with 21 additions and 15 deletions

View file

@ -158,9 +158,13 @@ class DecoderUtil {
if (previousWord != null) { if (previousWord != null) {
sb.append(decodeEncodedWord(previousWord)); sb.append(decodeEncodedWord(previousWord));
sb.append(sep); sb.append(sep);
sb.append(body.substring(begin, end));
previousWord = null; previousWord = null;
} else {
sb.append(sep);
} }
} else if (previousWord != null) { } else {
if (previousWord != null) {
if (previousWord.encoding.equals(word.encoding) && previousWord.charset.equals(word.charset)) { if (previousWord.encoding.equals(word.encoding) && previousWord.charset.equals(word.charset)) {
previousWord.encodedText += word.encodedText; previousWord.encodedText += word.encodedText;
} else { } else {
@ -169,8 +173,10 @@ class DecoderUtil {
previousWord = word; previousWord = word;
} }
} else { } else {
sb.append(sep);
previousWord = word; previousWord = word;
} }
}
if (previousWord == null) { if (previousWord == null) {
sb.append(sep); sb.append(sep);

View file

@ -25,7 +25,6 @@ public class DecoderUtilTest {
@Test @Test
public void decodeEncodedWords_withAsciiCharset_encoded_data_returns_text() { public void decodeEncodedWords_withAsciiCharset_encoded_data_returns_text() {
body = "=?us-ascii?q?abc?="; body = "=?us-ascii?q?abc?=";
expect = "abc"; expect = "abc";
message = null; message = null;
@ -76,7 +75,6 @@ public class DecoderUtilTest {
public void decodeEncodedWords_withMissingCharset_returnAsText() { public void decodeEncodedWords_withMissingCharset_returnAsText() {
body = "=??q??="; body = "=??q??=";
expect = "=??q??="; expect = "=??q??=";
;
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
} }
@ -92,7 +90,6 @@ public class DecoderUtilTest {
@Test @Test
public void decodeEncodedWords_withNoTextCharsetOrEncoding_returnAsText() { public void decodeEncodedWords_withNoTextCharsetOrEncoding_returnAsText() {
body = "=??="; body = "=??=";
expect = "=??="; expect = "=??=";
message = null; message = null;
@ -101,7 +98,6 @@ public class DecoderUtilTest {
@Test @Test
public void decodeEncodedWords_with_MissingEncodingAndData_returnAsText() { public void decodeEncodedWords_with_MissingEncodingAndData_returnAsText() {
body = "=?x?="; body = "=?x?=";
expect = "=?x?="; expect = "=?x?=";
message = null; message = null;
@ -110,7 +106,6 @@ public class DecoderUtilTest {
@Test @Test
public void decodeEncodedWords_withMissingEncoding_returnAsText() { public void decodeEncodedWords_withMissingEncoding_returnAsText() {
body = "=?x??="; body = "=?x??=";
expect = "=?x??="; expect = "=?x??=";
message = null; message = null;
@ -119,7 +114,6 @@ public class DecoderUtilTest {
@Test @Test
public void decodeEncodedWords_with_incompleteEncodingFormat_returnAsText() { public void decodeEncodedWords_with_incompleteEncodingFormat_returnAsText() {
body = "=?x?q?="; body = "=?x?q?=";
expect = "=?x?q?="; expect = "=?x?q?=";
message = null; message = null;
@ -128,7 +122,6 @@ public class DecoderUtilTest {
@Test @Test
public void decodeEncodedWords_with_unrecognisedEncoding_withEmptyEncodedData_returnAsText() { public void decodeEncodedWords_with_unrecognisedEncoding_withEmptyEncodedData_returnAsText() {
body = "=?x?q??="; body = "=?x?q??=";
expect = "=?x?q??="; expect = "=?x?q??=";
message = null; message = null;
@ -137,7 +130,6 @@ public class DecoderUtilTest {
@Test @Test
public void decodeEncodedWords_withUnrecognisedEncoding_withEncodedData_return_encoded_data() { public void decodeEncodedWords_withUnrecognisedEncoding_withEncodedData_return_encoded_data() {
body = "=?x?q?X?="; body = "=?x?q?X?=";
expect = "X"; expect = "X";
message = null; message = null;
@ -160,6 +152,14 @@ public class DecoderUtilTest {
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
} }
@Test
public void decodeEncodedWords_withPartiallyEncodedAfter_returnsBothSections() {
body = "def=?us-ascii?q?abc?=";
expect = "defabc";
message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test @Test
public void decodeEncodedWords_withUnrecognisedCharset_returnsEncodedData() { public void decodeEncodedWords_withUnrecognisedCharset_returnsEncodedData() {
body = "=?x?= =?"; body = "=?x?= =?";