SingleHostClientHttpSessionTest.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "../SingleHostClientHttpSession.h"
00025 #include "../BodyEnd.h"
00026 #include "../RequestHeader.h"
00027 #include "../ResponseHeader.h"
00028 #include "generated/SingleHostClientHttpSessionDriver.h"
00029 #include "generated/ClientHttpSessionMock.h"
00030 #include "generated/ClientHttpSessionStatusMock.h"
00031 #include "Imports.cpp"
00032
00033 namespace Protocols {
00034 namespace Http {
00035 namespace Testing {
00036
00038
00042 class SingleHostClientHttpSessionTest : public CppUnit::TestFixture
00043 {
00044 CPPUNIT_TEST_SUITE (SingleHostClientHttpSessionTest);
00045 CPPUNIT_TEST (testCreateRequestHeaderFromUriWithPathWithoutQuery);
00046 CPPUNIT_TEST (testCreateRequestHeaderFromUriWithPathWithQuery);
00047 CPPUNIT_TEST (testCreateRequestHeaderFromUriWithoutPathWithoutQuery);
00048 CPPUNIT_TEST (testCreateRequestHeaderFromUriWithoutPathWithQueryWithPort);
00049 CPPUNIT_TEST (testOpenHttpSessionOpensTheUnderlyingSession);
00050 CPPUNIT_TEST (testCloseHttpSessionClosesTheUnderlyingSession);
00051 CPPUNIT_TEST (testAbortHttpSessionAbortsTheUnderlyingSession);
00052 CPPUNIT_TEST (testSendGetRequestHeader);
00053 CPPUNIT_TEST (testReceiveResponseHeaderForGetRequest);
00054 CPPUNIT_TEST (testReceiveResponseBodyChunk);
00055 CPPUNIT_TEST (testReceiveBodyEnd);
00056 CPPUNIT_TEST_SUITE_END();
00057
00058 auto_ptr <FileMock> outFile;
00059 auto_ptr <SessionMock> session;
00060 auto_ptr <ClientHttpSessionStatusMock> status;
00061 auto_ptr <SingleHostClientHttpSession> httpSessionReal;
00062 auto_ptr <SingleHostClientHttpSessionDriver> httpSession;
00063
00064 const Uri uri;
00065
00066 public:
00067 SingleHostClientHttpSessionTest()
00068 : uri (Uri::fromUnencoded ("http://www.calitko.org"))
00069 {
00070 }
00071
00072 void setUp()
00073 {
00074 outFile.reset (new FileMock);
00075 session.reset (new SessionMock);
00076 status.reset (new ClientHttpSessionStatusMock);
00077 httpSessionReal.reset (new SingleHostClientHttpSession
00078 (*status, *session));
00079 httpSession.reset (new SingleHostClientHttpSessionDriver
00080 (*httpSessionReal));
00081 }
00082
00083 void tearDown()
00084 {
00085 httpSession.reset();
00086 httpSessionReal.reset();
00087 status.reset();
00088 session.reset();
00089 outFile.reset();
00090 }
00091
00092 void refCreateRequestHeader (const QByteArray &unencodedUri,
00093 const QByteArray &httpMethod,
00094 const QByteArray &path,
00095 const QByteArray &host)
00096 {
00097 RequestHeader request = SingleHostClientHttpSession::createRequestHeader
00098 (httpMethod, Uri::fromUnencoded (unencodedUri));
00099
00100 RequestHeader referenceRequest (httpMethod, path, 1, 1);
00101 referenceRequest.addFieldValue ("Host", host);
00102
00103 CPPUNIT_ASSERT (request == referenceRequest);
00104 }
00105
00106 void testCreateRequestHeaderFromUriWithPathWithoutQuery()
00107 {
00108 refCreateRequestHeader ("http://www.calitko.org/test me",
00109 "GET", "/test%20me", "www.calitko.org");
00110 }
00111
00112 void testCreateRequestHeaderFromUriWithPathWithQuery()
00113 {
00114 refCreateRequestHeader ("http://www.calitko.org/test me?s=p&k",
00115 "POST", "/test%20me?s=p&k", "www.calitko.org");
00116 }
00117
00118 void testCreateRequestHeaderFromUriWithoutPathWithoutQuery()
00119 {
00120 refCreateRequestHeader ("http://www.calitko.org",
00121 "PUT", "/", "www.calitko.org");
00122 }
00123
00124 void testCreateRequestHeaderFromUriWithoutPathWithQueryWithPort()
00125 {
00126 refCreateRequestHeader ("http://www.calitko.org:8080?myquery",
00127 "HEAD", "/?myquery", "www.calitko.org:8080");
00128 }
00129
00130 void testOpenHttpSessionOpensTheUnderlyingSession()
00131 {
00132 call (httpSession->open())
00133 .willCall (session->open())
00134 .returns()
00135 .returns();
00136 }
00137
00138 void testCloseHttpSessionClosesTheUnderlyingSession()
00139 {
00140 call (httpSession->close())
00141 .willCall (session->close())
00142 .willCall (httpSession->sessionClosing (*session))
00143 .willCall (status->clientHttpSessionClosing())
00144 .returns()
00145 .returns()
00146 .returns()
00147 .returns();
00148
00149 session->calls (httpSession->sessionClosed (*session))
00150 .willCall (status->clientHttpSessionClosed())
00151 .returns()
00152 .returns();
00153 }
00154
00155 void testAbortHttpSessionAbortsTheUnderlyingSession()
00156 {
00157 call (httpSession->abort())
00158 .willCall (session->abort())
00159 .willCall (httpSession->sessionClosed (*session))
00160 .willCall (status->clientHttpSessionClosed())
00161 .returns()
00162 .returns()
00163 .returns()
00164 .returns();
00165 }
00166
00167 void refSendGetRequest (const Uri &uri)
00168 {
00169 RequestHeader getRequest =
00170 SingleHostClientHttpSession::createRequestHeader ("GET", uri);
00171 getRequest.setFieldValue ("Content-Length", "0");
00172
00173 call (httpSession->get (uri, *outFile))
00174 .willCall (session->send (getRequest))
00175 .returns()
00176 .returns();
00177 }
00178
00179 void testSendGetRequestHeader()
00180 {
00181 refSendGetRequest (uri);
00182 }
00183
00184 void refReceiveResponseHeader (const ResponseHeader &response)
00185 {
00186 session->calls (httpSession->sessionReceivedData (*session, response))
00187 .willCall (status->clientHttpSessionResponseStarted (response))
00188 .returns()
00189 .returns();
00190 }
00191
00192 void testReceiveResponseHeaderForGetRequest()
00193 {
00194 ResponseHeader getResponse (200, "OK", 1, 1);
00195 getResponse.setFieldValue ("Content-Length", "0");
00196
00197 refSendGetRequest (uri);
00198 refReceiveResponseHeader (getResponse);
00199 }
00200
00201 void refReceiveResponseBodyChunk (const RawData &chunk)
00202 {
00203 session->calls (httpSession->sessionReceivedData (*session, chunk))
00204 .willCall (outFile->write (chunk.toRawBytes()))
00205 .willReturn (true)
00206 .returns();
00207 }
00208
00209 void testReceiveResponseBodyChunk()
00210 {
00211 RawData chunk (QByteArray (20, '1'));
00212 refSendGetRequest (uri);
00213 refReceiveResponseBodyChunk (chunk);
00214 }
00215
00216 void refReceiveBodyEnd (const BodyEnd &bodyEnd)
00217 {
00218 session->calls (httpSession->sessionReceivedData (*session, bodyEnd))
00219 .willCall (status->clientHttpSessionResponseFinished (bodyEnd))
00220 .returns()
00221 .returns();
00222 }
00223
00224 void testReceiveBodyEnd()
00225 {
00226 BodyEnd bodyEnd;
00227 bodyEnd.setFieldValue ("header-name", "value");
00228
00229 refSendGetRequest (uri);
00230 refReceiveBodyEnd (bodyEnd);
00231 }
00232 };
00233
00234 CPPUNIT_TEST_SUITE_REGISTRATION (SingleHostClientHttpSessionTest);
00235
00236 }
00237 }
00238 }