CompositeSingleHostClientHttpSessionTest.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 "../CompositeSingleHostClientHttpSession.h"
00025 #include "generated/CompositeSingleHostClientHttpSessionDriver.h"
00026 #include "generated/ClientHttpSessionMock.h" // \todo for FileMock
00027 #include "generated/ClientHttpSessionStatusMock.h"
00028 #include "Imports.cpp"
00029 
00030 namespace Protocols {
00031 namespace Http {
00032 namespace Testing {
00033 
00035 
00039 class CompositeSingleHostClientHttpSessionTest : public CppUnit::TestFixture
00040 {
00041     CPPUNIT_TEST_SUITE (CompositeSingleHostClientHttpSessionTest);
00042     CPPUNIT_TEST (testSendGetRequest);
00043     CPPUNIT_TEST_SUITE_END();
00044 
00045     auto_ptr <FileMock>                                     outFile;
00046     auto_ptr <TransportStub>                                transport;
00047     auto_ptr <ClientHttpSessionStatusMock>                  status;
00048     auto_ptr <CompositeSingleHostClientHttpSession>         sessionReal;
00049     auto_ptr <CompositeSingleHostClientHttpSessionDriver>   session;
00050     auto_ptr <TransportStatusDriver>                        transportStatus;
00051 
00052 public:
00053     void setUp()
00054     {
00055         outFile.reset (new FileMock);
00056         transport.reset (new TransportStub);
00057         status.reset (new ClientHttpSessionStatusMock);
00058         sessionReal.reset (new CompositeSingleHostClientHttpSession (*transport,
00059                                                                      *status));
00060         session.reset (new CompositeSingleHostClientHttpSessionDriver
00061                                                                 (*sessionReal));
00062         transportStatus.reset (new TransportStatusDriver (*transport->status));
00063     }
00064 
00065     void tearDown()
00066     {
00067         transportStatus.reset();
00068         session.reset();
00069         sessionReal.reset();
00070         status.reset();
00071         transport.reset();
00072         outFile.reset();
00073     }
00074 
00075     void testSendGetRequest()
00076     {
00077         Uri uri = Uri::fromUnencoded ("http://www.calitko.org");
00078         RequestHeader request = SingleHostClientHttpSession::createRequestHeader ("GET", uri);
00079         request.setFieldValue ("Content-Length", "0");
00080 
00081         call (session->open())
00082         .returns();
00083 
00084         call (session->get (uri, *outFile))
00085         .returns();
00086 
00087         CPPUNIT_ASSERT (transport->writeBuffer == request.toRawBytes());
00088         transport->writeBuffer = QByteArray();
00089 
00090         // \todo mybe use a SingleHostClientHttpSession::createResponseHeader():
00091         QByteArray body (20, '3');
00092         ResponseHeader response (200, "OK", 1, 1);
00093         response.setFieldValue ("Content-Length", QByteArray::number (body.size()));
00094         transport->readBuffer = response.toRawBytes() + body;
00095 
00096         call (transportStatus->transportReadyRead (*transport))
00097             .willCall (status->clientHttpSessionResponseStarted (response))
00098             .returns()
00099             .willCall (outFile->write (body))
00100             .willReturn (true)
00101             .willCall (status->clientHttpSessionResponseFinished (HeaderBase()))
00102             .returns()
00103         .returns();
00104     }
00105 };
00106 
00107 CPPUNIT_TEST_SUITE_REGISTRATION (CompositeSingleHostClientHttpSessionTest);
00108 
00109 } // namespace Testing
00110 } // namespace Http
00111 } // namespace Protocols