ConcreteProtocolTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "../ConcreteProtocol.h"
00025 #include "../IdentityContentLengthBodyReader.h"
00026 #include "../IdentityContentLengthBodyWriter.h"
00027 #include "../IdentityReadAllBodyReader.h"
00028 #include "../RequestHeader.h"
00029 #include "../ResponseHeader.h"
00030 #include "Imports.cpp"
00031 
00032 namespace Protocols {
00033 namespace Http {
00034 namespace Testing {
00035 
00037 
00039 class ConcreteProtocolTest : public CppUnit::TestFixture
00040 {
00041     CPPUNIT_TEST_SUITE (ConcreteProtocolTest);
00042     CPPUNIT_TEST (testCreateHeaderInvalidHeader);
00043     CPPUNIT_TEST (testCreateRequestHeader);
00044     CPPUNIT_TEST (testCreateResponseHeader);
00045     CPPUNIT_TEST (testCreateBodyReaderWhenNoTransferEncodingAndNoContentLength);
00046     CPPUNIT_TEST (testCreateBodyReaderWhenIdentityEncodingAndNoContentLength);
00047     CPPUNIT_TEST (testCreateBodyReaderWhenNoTransferEncodingAndContentLength);
00048     CPPUNIT_TEST (testCreateBodyReaderWhenIdentityEncodingAndContentLength);
00049     CPPUNIT_TEST (testCreateBodyWriterWhenNoTransferEncodingAndContentLength);
00050     CPPUNIT_TEST (testCreateBodyWriterWhenIdentityEncodingAndContentLength);
00051     CPPUNIT_TEST_SUITE_END();
00052 
00053     auto_ptr <ConcreteProtocol>     protocol;
00054 
00055 public:
00056     void setUp()
00057     {
00058         protocol.reset (new ConcreteProtocol);
00059     }
00060 
00061     void tearDown()
00062     {
00063         protocol.reset();
00064     }
00065 
00066     void testCreateHeaderInvalidHeader()
00067     {
00068         QByteArray invalidRawHeader ("non HTTP header string");
00069         Header header = protocol->createHeader (invalidRawHeader);
00070         CPPUNIT_ASSERT (header == Header());
00071     }
00072 
00073     void refCreateHeader (const QByteArray &rawHeader, const Header &header)
00074     {
00075         Header createdHeader = protocol->createHeader (rawHeader);
00076         CPPUNIT_ASSERT (header == createdHeader);
00077     }
00078 
00079     void testCreateRequestHeader()
00080     {
00081         RequestHeader requestHeader ("GET", "/", 1, 1);
00082         requestHeader.setFieldValue ("header-name", "value");
00083         refCreateHeader (requestHeader.toRawBytes(), requestHeader);
00084     }
00085 
00086     void testCreateResponseHeader()
00087     {
00088         ResponseHeader responseHeader (200, "OK", 1, 1);
00089         responseHeader.setFieldValue ("header-name", "value");
00090         refCreateHeader (responseHeader.toRawBytes(), responseHeader);
00091     }
00092 
00093     template <typename BodyReaderType>
00094     void refCreateBodyReader (const QByteArray &rawHeader)
00095     {
00096         HeaderBase header;
00097         CPPUNIT_ASSERT (header.parse (rawHeader) == true);
00098         BodyReader *bodyReader = protocol->createBodyReader (header);
00099         CPPUNIT_ASSERT (bodyReader != 0);
00100         CPPUNIT_ASSERT (typeid (*bodyReader) == typeid (BodyReaderType));
00101         protocol->destroyBodyReader (bodyReader);
00102     }
00103 
00104     void testCreateBodyReaderWhenNoTransferEncodingAndNoContentLength()
00105     {
00106         refCreateBodyReader <IdentityReadAllBodyReader> ("");
00107     }
00108 
00109     void testCreateBodyReaderWhenIdentityEncodingAndNoContentLength()
00110     {
00111         refCreateBodyReader <IdentityReadAllBodyReader>
00112             ("Transfer-Encoding: identity\r\n");
00113     }
00114 
00115     void testCreateBodyReaderWhenNoTransferEncodingAndContentLength()
00116     {
00117         refCreateBodyReader <IdentityContentLengthBodyReader>
00118             ("Content-Length: 30\r\n");
00119     }
00120 
00121     void testCreateBodyReaderWhenIdentityEncodingAndContentLength()
00122     {
00123         refCreateBodyReader <IdentityContentLengthBodyReader>
00124             ("Transfer-Encoding: identity\r\n"
00125              "Content-Length: 30\r\n");
00126     }
00127 
00128     template <typename BodyWriterType>
00129     void refCreateBodyWriter (const QByteArray &rawHeader)
00130     {
00131         HeaderBase header;
00132         CPPUNIT_ASSERT (header.parse (rawHeader) == true);
00133         BodyWriter *bodyWriter = protocol->createBodyWriter (header);
00134         CPPUNIT_ASSERT (bodyWriter != 0);
00135         CPPUNIT_ASSERT (typeid (*bodyWriter) == typeid (BodyWriterType));
00136         protocol->destroyBodyWriter (bodyWriter);
00137     }
00138 
00139     void testCreateBodyWriterWhenNoTransferEncodingAndContentLength()
00140     {
00141         refCreateBodyWriter <IdentityContentLengthBodyWriter>
00142             ("Content-Length: 30\r\n");
00143     }
00144 
00145     void testCreateBodyWriterWhenIdentityEncodingAndContentLength()
00146     {
00147         refCreateBodyWriter <IdentityContentLengthBodyWriter>
00148             ("Transfer-Encoding: identity\r\n"
00149              "Content-Length: 30\r\n");
00150     }
00151 };
00152 
00153 CPPUNIT_TEST_SUITE_REGISTRATION (ConcreteProtocolTest);
00154 
00155 } // namespace Testing
00156 } // namespace Http
00157 } // namespace Protocols