Files
kumomta/crates/message/test_header_wrap.lua
Wez Furlong 67bf14ca20 improve header wrapping functions
Augment the docs to clarify that msg:append_header and
msg:prepend_header do no modification of the value by default.

Adjust the wrapping used by Header:new_unstructured so that it
hard wraps before the SMTP line length limit.

Add optional encode parameter to msg:append_header and
msg:prepend_header so that those methods can opt-in to the
Header:new_unstructured formatting.

Add HeaderMap:append method to mirror the existing HeaderMap:prepend
method.

Add unit tests that demonstrate the effect of wrapping/encoding
for both the "dumb" msg:append_header and msg:prepend_header functions,
and the "smarter" HeaderMap:prepend and HeaderMap:append methods that
always encode their parameters.

Update the docs for the headermap flavor of these methods to indicate
that they will always encode the value.
2025-10-29 11:35:42 +00:00

56 lines
1.7 KiB
Lua

local kumo = require 'kumo'
local utils = require 'policy-extras.policy_utils'
local function new_msg(content)
return kumo.make_message('sender@example.com', 'recip@example.com', content)
end
local msg = new_msg 'Subject: hello\r\n\r\nHello'
local long_string = string.rep('A', 1500)
local expect_wrapped = string.rep('A', 900) .. ' ' .. string.rep('A', 600)
msg:prepend_header('X-Long', long_string)
utils.assert_eq(
tostring(msg:get_first_named_header_value 'X-Long'),
long_string
)
msg:prepend_header('X-Long-Wrap', long_string, true)
utils.assert_eq(
tostring(msg:get_first_named_header_value 'X-Long-Wrap'),
expect_wrapped
)
msg:prepend_header('X-Emoji-Unencoded', '👾')
-- The value is stored as UTF-8
utils.assert_eq(
tostring(msg:get_first_named_header_value 'X-Emoji-Unencoded'),
'👾'
)
utils.assert_eq(
msg:parse_mime().headers:get_first_named('X-Emoji-Unencoded').raw_value,
'👾'
)
msg:prepend_header('X-Emoji', '👾', true)
utils.assert_eq(tostring(msg:get_first_named_header_value 'X-Emoji'), '👾')
utils.assert_eq(
msg:parse_mime().headers:get_first_named('X-Emoji').raw_value,
'=?UTF-8?q?=F0=9F=91=BE?='
)
-- Soft wrap lines with spaces
local long_string = string.rep(' hello there', 10)
msg:prepend_header('X-Long-Space', long_string)
utils.assert_eq(
tostring(msg:get_first_named_header_value 'X-Long-Space'),
'hello there hello there hello there hello there hello there hello there '
.. 'hello there hello there hello there hello there'
)
msg:append_header('X-Long-Space-Wrap', long_string, true)
utils.assert_eq(
tostring(msg:get_first_named_header_value 'X-Long-Space-Wrap'),
'hello there hello there hello there hello there hello there hello there '
.. 'hello there hello there hello there hello there'
)