ferencd@0: // ferencd@0: // VMime library (http://www.vmime.org) ferencd@0: // Copyright (C) 2002-2013 Vincent Richard ferencd@0: // ferencd@0: // This program is free software; you can redistribute it and/or ferencd@0: // modify it under the terms of the GNU General Public License as ferencd@0: // published by the Free Software Foundation; either version 3 of ferencd@0: // the License, or (at your option) any later version. ferencd@0: // ferencd@0: // This program is distributed in the hope that it will be useful, ferencd@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ferencd@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ferencd@0: // General Public License for more details. ferencd@0: // ferencd@0: // You should have received a copy of the GNU General Public License along ferencd@0: // with this program; if not, write to the Free Software Foundation, Inc., ferencd@0: // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ferencd@0: // ferencd@0: // Linking this library statically or dynamically with other modules is making ferencd@0: // a combined work based on this library. Thus, the terms and conditions of ferencd@0: // the GNU General Public License cover the whole combination. ferencd@0: // ferencd@0: ferencd@0: #include "tests/testUtils.hpp" ferencd@0: ferencd@0: ferencd@0: VMIME_TEST_SUITE_BEGIN(headerFieldTest) ferencd@0: ferencd@0: VMIME_TEST_LIST_BEGIN ferencd@0: VMIME_TEST(testBadValueType) ferencd@0: VMIME_TEST(testValueOnNextLine) ferencd@0: VMIME_TEST(testStripSpacesAtEnd) ferencd@0: VMIME_TEST(testValueWithEmptyLine) ferencd@0: VMIME_TEST_LIST_END ferencd@0: ferencd@0: ferencd@0: void testBadValueType() ferencd@0: { ferencd@0: vmime::shared_ptr hfactory = ferencd@0: vmime::headerFieldFactory::getInstance(); ferencd@0: ferencd@0: // "To" header field accepts values of type "addressList" ferencd@0: vmime::shared_ptr to = hfactory->create(vmime::fields::TO); ferencd@0: VASSERT_THROW("to", ferencd@0: to->setValue(vmime::mailbox("email@vmime.org")), ferencd@0: vmime::exceptions::bad_field_value_type); ferencd@0: ferencd@0: // Unregistered header field accepts any value type ferencd@0: vmime::shared_ptr custom = hfactory->create("X-MyCustomHeader"); ferencd@0: VASSERT_NO_THROW("custom/1", ferencd@0: custom->setValue(vmime::mailbox("email@vmime.org"))); ferencd@0: VASSERT_NO_THROW("custom/2", ferencd@0: custom->setValue(vmime::text("field value text"))); ferencd@0: } ferencd@0: ferencd@0: void testValueOnNextLine() ferencd@0: { ferencd@0: vmime::parsingContext ctx; ferencd@0: ferencd@0: const vmime::string buffer = "Field: \r\n\tfield data"; ferencd@0: ferencd@0: vmime::shared_ptr hfield = ferencd@0: vmime::headerField::parseNext(ctx, buffer, 0, buffer.size()); ferencd@0: ferencd@0: vmime::shared_ptr hvalue = ferencd@0: hfield->getValue (); ferencd@0: ferencd@0: VASSERT_EQ("Field name", "Field", hfield->getName()); ferencd@0: VASSERT_EQ("Field value", "field data", hvalue->getWholeBuffer()); ferencd@0: } ferencd@0: ferencd@0: void testStripSpacesAtEnd() ferencd@0: { ferencd@0: vmime::parsingContext ctx; ferencd@0: ferencd@0: const vmime::string buffer = "Field: \r\n\tfield data "; ferencd@0: ferencd@0: vmime::shared_ptr hfield = ferencd@0: vmime::headerField::parseNext(ctx, buffer, 0, buffer.size()); ferencd@0: ferencd@0: vmime::shared_ptr hvalue = ferencd@0: hfield->getValue (); ferencd@0: ferencd@0: VASSERT_EQ("Field name", "Field", hfield->getName()); ferencd@0: VASSERT_EQ("Field value", toHex("field data"), toHex(hvalue->getWholeBuffer())); ferencd@0: } ferencd@0: ferencd@0: void testValueWithEmptyLine() ferencd@0: { ferencd@0: vmime::parsingContext ctx; ferencd@0: ferencd@0: const vmime::string buffer = "Field: \r\n\tdata1\r\n\tdata2\r\n\t\r\n\tdata3"; ferencd@0: ferencd@0: vmime::shared_ptr hfield = ferencd@0: vmime::headerField::parseNext(ctx, buffer, 0, buffer.size()); ferencd@0: ferencd@0: vmime::shared_ptr hvalue = ferencd@0: hfield->getValue (); ferencd@0: ferencd@0: VASSERT_EQ("Field name", "Field", hfield->getName()); ferencd@0: VASSERT_EQ("Field value", "data1 data2 data3", hvalue->getWholeBuffer()); ferencd@0: } ferencd@0: ferencd@0: VMIME_TEST_SUITE_END