forked from thunderbird/thunderbird-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AddressHeaderBuilder to correctly fold recipient headers
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
mail/common/src/main/java/com/fsck/k9/mail/internet/AddressHeaderBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.fsck.k9.mail.internet | ||
|
||
import com.fsck.k9.mail.Address | ||
|
||
/** | ||
* Encode and fold email addresses for use in header values. | ||
*/ | ||
object AddressHeaderBuilder { | ||
@JvmStatic | ||
fun createHeaderValue(addresses: Array<Address>): String { | ||
require(addresses.isNotEmpty()) { "addresses must not be empty" } | ||
|
||
return buildString { | ||
var lineLength = 0 | ||
for ((index, address) in addresses.withIndex()) { | ||
val encodedAddress = address.toEncodedString() | ||
val encodedAddressLength = encodedAddress.length | ||
|
||
if (index > 0 && lineLength + 2 + encodedAddressLength + 1 > RECOMMENDED_MAX_LINE_LENGTH) { | ||
append(",$CRLF ") | ||
append(encodedAddress) | ||
lineLength = encodedAddressLength + 1 | ||
} else { | ||
if (index > 0) { | ||
append(", ") | ||
lineLength += 2 | ||
} | ||
|
||
append(encodedAddress) | ||
lineLength += encodedAddressLength | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
mail/common/src/main/java/com/fsck/k9/mail/internet/MimeExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
mail/common/src/test/java/com/fsck/k9/mail/internet/AddressHeaderBuilderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.fsck.k9.mail.internet | ||
|
||
import com.fsck.k9.mail.Address | ||
import com.fsck.k9.mail.K9LibRobolectricTestRunner | ||
import com.fsck.k9.mail.crlf | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
|
||
@RunWith(K9LibRobolectricTestRunner::class) | ||
class AddressHeaderBuilderTest { | ||
|
||
@Test | ||
fun createHeaderValue_withSingleAddress() { | ||
val addresses = arrayOf(Address("test@domain.example")) | ||
|
||
val headerValue = AddressHeaderBuilder.createHeaderValue(addresses) | ||
|
||
assertEquals("test@domain.example", headerValue) | ||
} | ||
|
||
@Test | ||
fun createHeaderValue_withTwoAddressesThatFitOnSingleLine() { | ||
val addresses = arrayOf( | ||
Address("one@domain.example"), | ||
Address("two@domain.example") | ||
) | ||
|
||
val headerValue = AddressHeaderBuilder.createHeaderValue(addresses) | ||
|
||
assertEquals("one@domain.example, two@domain.example", headerValue) | ||
} | ||
|
||
@Test | ||
fun createHeaderValue_withMultipleAddressesThatNeedWrapping() { | ||
val addresses = arrayOf( | ||
Address("one@domain.example", "Person One"), | ||
Address("two+because.i.can@this.is.quite.some.domain.example", "Person \"Long Email Address\" Two"), | ||
Address("three@domain.example", "Person Three"), | ||
Address("four@domain.example", "Person Four"), | ||
Address("five@domain.example", "Person Five") | ||
) | ||
|
||
val headerValue = AddressHeaderBuilder.createHeaderValue(addresses) | ||
|
||
assertEquals(""" | ||
|Person One <one@domain.example>, | ||
| "Person \"Long Email Address\" Two" <two+because.i.can@this.is.quite.some.domain.example>, | ||
| Person Three <three@domain.example>, Person Four <four@domain.example>, | ||
| Person Five <five@domain.example> | ||
""".trimMargin().crlf(), headerValue) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun createHeaderValue_withoutAddresses_shouldThrow() { | ||
AddressHeaderBuilder.createHeaderValue(emptyArray()) | ||
} | ||
} |