独立调用send_mail函数会由于主题中的换行符而导致BadHeaderError异常。
我预计test_newline_causes_exception也会失败,但它并没有。 这是在Django 1.3中发生的。 有什么想法吗?
from django.core.mail import send_mail
from django.utils import unittest
class EmailTestCase(unittest.TestCase):
def test_newline_causes_exception(self):
send_mail('Header\nInjection', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False)
编辑:这个新的测试显示,当在测试中使用send_mail时,头部检查代码(django.core.mail.message.forbid_multi_line_headers)不会被调用。
from django.core.mail import send_mail, BadHeaderError, outbox
from django.utils import unittest
class EmailTestCase(unittest.TestCase):
def test_newline_in_subject_should_raise_exception(self):
try:
send_mail('Subject\nhere', 'Here is the message.',
'from@example.com', ['to@example.com'], fail_silently=False)
except BadHeaderError:
raise Exception
self.assertEqual(len(outbox), 1)
self.assertEqual(outbox[0].subject, 'Subject here')
结果:
AssertionError: 'Subject\nhere' != 'Subject here'