applayerprotocols/httptransportfw/Test/T_HttpOnline/T_PersistentConn.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // defines test class: CTextModeTestPersistentConn
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "T_PersistentConn.h"
       
    19 
       
    20 
       
    21 // Define this to do tests using real SyncML data
       
    22 // #define _TEST_WITH_SYNCML_DATA_
       
    23 
       
    24 
       
    25 // Constants for use in this file
       
    26 _LIT8(KTestUrlFormat, "http://%S%S"); // <hostname><path> (where path must start with a / character)
       
    27 
       
    28 #if defined (_TEST_WITH_SYNCML_DATA_)
       
    29 
       
    30 	_LIT8(KHttpPostBodyChunk1a, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SyncML xmlns='SYNCML:SYNCML1.0'><SyncHdr><VerDTD>1.0</VerDTD><VerProto>SyncML/1.0</VerProto><SessionID>1</SessionID>");
       
    31 	_LIT8(KHttpPostBodyChunk1b, "<MsgID>1</MsgID><Target><LocURI>/servlet/fde.sync.SyncML</LocURI></Target><Source><LocURI>ABCDEFG</LocURI></Source><Cred><Meta>Basic</Meta><Data>c3ltYmlhbj");
       
    32 	_LIT8(KHttpPostBodyChunk1c, "pmMXN5bmNtbAA=</Data></Cred></SyncHdr><SyncBody><Alert><CmdID>1</CmdID><Data>200</Data><Item><Target><LocURI>CONTACTS</LocURI></Target><Source><LocURI>C:\\D");
       
    33 	_LIT8(KHttpPostBodyChunk1d, "ocuments\\contacts</LocURI></Source></Item></Alert><Final/></SyncBody></SyncML>");
       
    34 
       
    35 	_LIT8(KHttpPostBodyChunk2a, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SyncML xmlns='SYNCML:SYNCML1.0'><SyncHdr><VerDTD>1.0</VerDTD><VerProto>SyncML/1.0</VerProto><SessionID>1</SessionID>");
       
    36 	_LIT8(KHttpPostBodyChunk2b, "<MsgID>2</MsgID><Target><LocURI>/servlet/fde.sync.SyncML</LocURI></Target><Source><LocURI>ABCDEFG</LocURI></Source><Cred><Meta>Basic</Meta><Data>c3ltYmlhbj");
       
    37 	_LIT8(KHttpPostBodyChunk2c, "pmMXN5bmNtbAA=</Data></Cred></SyncHdr><SyncBody><Status><CmdID>2</CmdID><MsgRef>1</MsgRef><CmdRef>0</CmdRef><Cmd>SyncHdr</Cmd><Data>200</Data><Item><Target");
       
    38 	_LIT8(KHttpPostBodyChunk2d, "><LocURI>ABCDEFG</LocURI></Target><Source><LocURI>/servlet/fde.sync.SyncML</LocURI></Source></Item></Status><Status><CmdID>3</CmdID><MsgRef>1</MsgRef><CmdR");
       
    39 	_LIT8(KHttpPostBodyChunk2e, "ef>3</CmdRef><Cmd>Alert</Cmd><Data>200</Data><Item><Target><LocURI>C:\\Documents\\contacts</LocURI></Target><Source><LocURI>CONTACTS</LocURI></Source></Item>");
       
    40 	_LIT8(KHttpPostBodyChunk2f, "</Status><Sync><CmdID>1</CmdID><Target><LocURI>CONTACTS</LocURI></Target><Source><LocURI>C:\\Documents\\contacts</LocURI></Source><Meta></Meta><Add><CmdID>2<");
       
    41 	_LIT8(KHttpPostBodyChunk2g, "/CmdID><Item><Source><LocURI>3</LocURI></Source><Data>BEGIN:VCARD\r\nVERSION:2.1\r\nREV:20010117T160038Z\r\nUID:a1d1c81d196960-e058c63d190a18-3\r\nN:;test;");
       
    42 	_LIT8(KHttpPostBodyChunk2h, ";;\r\nEND:VCARD\r\n</Data></Item></Add></Sync><Final/></SyncBody></SyncML>");
       
    43 
       
    44 #else
       
    45 
       
    46 	_LIT8(KHttpPostBodyChunk1, "abc=def&");
       
    47 	_LIT8(KHttpPostBodyChunk2, "ghi=jkl&");
       
    48 	_LIT8(KHttpPostBodyChunk3, "mno=pqr&");
       
    49 	_LIT8(KHttpPostBodyChunk4, "stu=vwx&");
       
    50 	_LIT8(KHttpPostBodyChunk5, "y=z");
       
    51 
       
    52 
       
    53 #endif
       
    54 
       
    55 // CTextModeTestPersistentConn-----------------------------------
       
    56 
       
    57 CTextModeTestPersistentConn* CTextModeTestPersistentConn::NewL(const TDesC& aHostName, const TDesC& aPath, TInt aNumTrans)
       
    58 	{
       
    59 	CTextModeTestPersistentConn* me = new(ELeave)CTextModeTestPersistentConn(aNumTrans);
       
    60 	CleanupStack::PushL(me);
       
    61 	me->ConstructL(aHostName, aPath);
       
    62 	CleanupStack::Pop();
       
    63 	return me;
       
    64 	}
       
    65 
       
    66 CTextModeTestPersistentConn::~CTextModeTestPersistentConn()
       
    67 	{
       
    68 	delete iTestUrl;
       
    69 	}
       
    70 
       
    71 void CTextModeTestPersistentConn::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
    72 	{
       
    73 	switch (aEvent.iStatus)
       
    74 		{
       
    75 	case THTTPEvent::EGotResponseHeaders:
       
    76 		{
       
    77 		// HTTP response headers have been received
       
    78 		//iEngine->Utils().LogIt(_L("<Got response headers>"));
       
    79 		DumpRespHeaders(aTransaction);
       
    80 		} break;
       
    81 	case THTTPEvent::ESubmit:
       
    82 		{
       
    83 		} break;
       
    84 	case THTTPEvent::EGotResponseBodyData:
       
    85 		{
       
    86 		// Some (more) body data has been received (in the HTTP response)
       
    87 		//iEngine->Utils().LogIt(_L("<Got a chunk of data>"));
       
    88 		DumpResponseBody(aTransaction);
       
    89 		} break;
       
    90 	case THTTPEvent::EResponseComplete:
       
    91 		{
       
    92 		// The transaction's response is complete
       
    93 		iEngine->Utils().LogIt(_L("<Transaction Complete>"));
       
    94 		} break;
       
    95 	case THTTPEvent::ESucceeded:
       
    96 		{
       
    97 		iEngine->Utils().LogIt(_L("<Transaction validated successfully>"));
       
    98 		CActiveScheduler::Stop();
       
    99 		} break;
       
   100 	case THTTPEvent::EFailed:
       
   101 		{
       
   102 		iEngine->Utils().LogIt(_L("<Transaction NOT validated successfully>"));
       
   103 		iFailureError = KErrTestFailed;
       
   104 		CActiveScheduler::Stop();
       
   105 		} break;
       
   106 	default:
       
   107 		{
       
   108 		iEngine->Utils().LogIt(_L("<unrecognised event> %d"),aEvent.iStatus);
       
   109 		if (aEvent.iStatus < 0)
       
   110 			{
       
   111 			iFailureError = aEvent.iStatus;
       
   112 			CActiveScheduler::Stop();
       
   113 			}
       
   114 		} 
       
   115 		break;
       
   116 		}
       
   117 	}
       
   118 
       
   119 TInt CTextModeTestPersistentConn::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   120 	{
       
   121 	iEngine->Utils().LogIt(_L("\nMHFRunL left: error code %d on transaction ID=%d for event %d\n"), aError, aTransaction.Id(), aEvent.iStatus);
       
   122 	return KErrNone;
       
   123 	}
       
   124 
       
   125 TBool CTextModeTestPersistentConn::GetNextDataPart(TPtrC8& aDataChunk)
       
   126 	{
       
   127 	iRetVal = EFalse;
       
   128 #if defined (_TEST_WITH_SYNCML_DATA_)
       
   129 	switch (iTestIdx)
       
   130 		{
       
   131 	case 0:
       
   132 		{
       
   133 		switch (iReqBodyChunkCount)
       
   134 			{
       
   135 		case 0:
       
   136 			{
       
   137 			aDataChunk.Set(KHttpPostBodyChunk1a());
       
   138 			} break;
       
   139 		case 1:
       
   140 			{
       
   141 			aDataChunk.Set(KHttpPostBodyChunk1b());
       
   142 			} break;
       
   143 		case 2:
       
   144 			{
       
   145 			aDataChunk.Set(KHttpPostBodyChunk1c());
       
   146 			} break;
       
   147 		case 3:
       
   148 			{
       
   149 			aDataChunk.Set(KHttpPostBodyChunk1d());
       
   150 			iRetVal = ETrue;
       
   151 			} break;
       
   152 			}
       
   153 		} break;
       
   154 	case 1:
       
   155 		{
       
   156 		switch (iReqBodyChunkCount)
       
   157 			{
       
   158 		case 0:
       
   159 			{
       
   160 			aDataChunk.Set(KHttpPostBodyChunk2a());
       
   161 			} break;
       
   162 		case 1:
       
   163 			{
       
   164 			aDataChunk.Set(KHttpPostBodyChunk2b());
       
   165 			} break;
       
   166 		case 2:
       
   167 			{
       
   168 			aDataChunk.Set(KHttpPostBodyChunk2c());
       
   169 			} break;
       
   170 		case 3:
       
   171 			{
       
   172 			aDataChunk.Set(KHttpPostBodyChunk2d());
       
   173 			} break;
       
   174 		case 4:
       
   175 			{
       
   176 			aDataChunk.Set(KHttpPostBodyChunk2e());
       
   177 			} break;
       
   178 		case 5:
       
   179 			{
       
   180 			aDataChunk.Set(KHttpPostBodyChunk2f());
       
   181 			} break;
       
   182 		case 6:
       
   183 			{
       
   184 			aDataChunk.Set(KHttpPostBodyChunk2g());
       
   185 			} break;
       
   186 		case 7:
       
   187 			{
       
   188 			aDataChunk.Set(KHttpPostBodyChunk2h());
       
   189 			iRetVal = ETrue;
       
   190 			} break;
       
   191 			}
       
   192 		} break;
       
   193 		}
       
   194 #else
       
   195 	switch (iReqBodyChunkCount)
       
   196 		{
       
   197 	case 0:
       
   198 		{
       
   199 		aDataChunk.Set(KHttpPostBodyChunk1());
       
   200 		} break;
       
   201 	case 1:
       
   202 		{
       
   203 		aDataChunk.Set(KHttpPostBodyChunk2());
       
   204 		} break;
       
   205 	case 2:
       
   206 		{
       
   207 		aDataChunk.Set(KHttpPostBodyChunk3());
       
   208 		} break;
       
   209 	case 3:
       
   210 		{
       
   211 		aDataChunk.Set(KHttpPostBodyChunk4());
       
   212 		} break;
       
   213 	case 4:
       
   214 		{
       
   215 		aDataChunk.Set(KHttpPostBodyChunk5());
       
   216 		iRetVal = ETrue;
       
   217 		} break;
       
   218 		}
       
   219 #endif
       
   220 
       
   221 	iReqBodyChunkCount++;
       
   222 	return iRetVal;
       
   223 	}
       
   224 
       
   225 void CTextModeTestPersistentConn::ReleaseData()
       
   226 	{
       
   227 	if( !iRetVal )
       
   228 		{
       
   229 		// More data to send - notify framework.
       
   230 		TRAPD(err,iTransaction.NotifyNewRequestBodyPartL());
       
   231 		
       
   232 		if (err != KErrNone)
       
   233 			iTransaction.Fail();
       
   234 		}
       
   235 	}
       
   236 
       
   237 TInt CTextModeTestPersistentConn::OverallDataSize()
       
   238 	{
       
   239 #if defined (_TEST_WITH_SYNCML_DATA_)
       
   240 	switch (iTestIdx)
       
   241 		{
       
   242 	case 0:
       
   243 		{
       
   244 		return (KHttpPostBodyChunk1a().Length() + KHttpPostBodyChunk1b().Length() + KHttpPostBodyChunk1c().Length() + KHttpPostBodyChunk1d().Length());
       
   245 		} break;
       
   246 	case 1:
       
   247 		{
       
   248 		return (KHttpPostBodyChunk2a().Length() + KHttpPostBodyChunk2b().Length() + KHttpPostBodyChunk2c().Length() + KHttpPostBodyChunk2d().Length() +
       
   249 			    KHttpPostBodyChunk2e().Length() + KHttpPostBodyChunk2f().Length() + KHttpPostBodyChunk2g().Length() + KHttpPostBodyChunk2h().Length());
       
   250 		} break;
       
   251 	default:
       
   252 		return 0;
       
   253 		}
       
   254 #else
       
   255 	return (KHttpPostBodyChunk1().Length() + KHttpPostBodyChunk2().Length() + KHttpPostBodyChunk3().Length() + KHttpPostBodyChunk4().Length() + KHttpPostBodyChunk5().Length());
       
   256 #endif
       
   257 	}
       
   258 
       
   259 const TDesC& CTextModeTestPersistentConn::TestName()
       
   260 	{
       
   261 	_LIT(KHeaderTestName,"CTextModeTestPersistentConn");
       
   262 	return KHeaderTestName;
       
   263 	}
       
   264 
       
   265 void CTextModeTestPersistentConn::DoRunL()
       
   266 	{
       
   267 	// Open the HTTP session
       
   268 	iSession.OpenL();
       
   269 	CleanupClosePushL(iSession);
       
   270 	RStringPool strP = iSession.StringPool();
       
   271 
       
   272 	// Loop for the number of transactions for this test
       
   273 	for (iTestIdx = 0; iTestIdx < iNumTrans; iTestIdx++)
       
   274 		{
       
   275 		// Reset the body chunk counter for the new transaction
       
   276 		iReqBodyChunkCount = 0;
       
   277 
       
   278 		// Open a POST transactions, specifying this object as the request body data supplier
       
   279 		iTransaction = iSession.OpenTransactionL(iTestUrl->Uri(), *this, strP.StringF(HTTP::EPOST,RHTTPSession::GetTable()));
       
   280 		CleanupClosePushL(iTransaction);
       
   281 		RHTTPRequest rq = iTransaction.Request();
       
   282 		rq.SetBody(*this);
       
   283 		RHTTPHeaders hdr = rq.GetHeaderCollection();
       
   284 		THTTPHdrVal length(OverallDataSize());
       
   285 		hdr.SetFieldL(strP.StringF(HTTP::EContentLength,RHTTPSession::GetTable()), length);
       
   286 		THTTPHdrVal contType(strP.StringF(HTTP::EApplicationXWwwFormUrlEncoded,RHTTPSession::GetTable()));
       
   287 		hdr.SetFieldL(strP.StringF(HTTP::EContentType,RHTTPSession::GetTable()), contType);
       
   288 		
       
   289 		// On the last transaction, add a 'Connection: close' header
       
   290 		if (iTestIdx == iNumTrans - 1)
       
   291 			hdr.SetFieldL(strP.StringF(HTTP::EConnection,RHTTPSession::GetTable()), THTTPHdrVal(strP.StringF(HTTP::EClose,RHTTPSession::GetTable())));
       
   292 
       
   293 		// Submit the transaction
       
   294 		iTransaction.SubmitL();
       
   295 		iFailureError = KErrNone;
       
   296 		CActiveScheduler::Start();
       
   297 
       
   298 		if (iFailureError != KErrNone)
       
   299 			{
       
   300 			// Done with this transaction and session
       
   301 			CleanupStack::PopAndDestroy(2, &iSession); // iTransaction
       
   302 			// Check for failure error codes caught in MHFRunL
       
   303 			User::LeaveIfError(iFailureError);
       
   304 			}
       
   305 
       
   306 		// Get the completion code and inform the engine.  Anything other than HTTP/200 status is a failure
       
   307 		// for this test.
       
   308 		iEngine->SetCurrentStatusCode(iTransaction.Response().StatusCode());
       
   309 		iExpectedStatusCode = 200;
       
   310 
       
   311 		// Done with this transaction
       
   312 		CleanupStack::PopAndDestroy(); // iTransaction
       
   313 		}
       
   314 
       
   315 	// Done with the session
       
   316 	CleanupStack::PopAndDestroy(); // iSession
       
   317 	}
       
   318 
       
   319 TInt CTextModeTestPersistentConn::RunError(TInt aErr)
       
   320 	{
       
   321 	iEngine->Utils().LogIt(_L("\nTest failed with error code %d\n"), aErr);
       
   322 	return KErrNone;
       
   323 	}
       
   324 	
       
   325 void CTextModeTestPersistentConn::DoCancel()
       
   326 	{
       
   327 	}
       
   328 
       
   329 CTextModeTestPersistentConn::CTextModeTestPersistentConn(TInt aNumTrans)
       
   330 	: iNumTrans(aNumTrans)
       
   331 	{
       
   332 	}
       
   333 
       
   334 void CTextModeTestPersistentConn::ConstructL(const TDesC& aHostName, const TDesC& aPath)
       
   335 	{
       
   336 	HBufC8* tmp1 = HBufC8::NewL(KTestUrlFormat().Length() + aHostName.Length() + aPath.Length());
       
   337 	CleanupStack::PushL(tmp1);
       
   338 	HBufC8* tmp2 = HBufC8::NewL(aHostName.Length());
       
   339 	CleanupStack::PushL(tmp2);
       
   340 	tmp2->Des().Copy(aHostName);
       
   341 	HBufC8* tmp3 = HBufC8::NewL(aPath.Length());
       
   342 	CleanupStack::PushL(tmp3);
       
   343 	tmp3->Des().Copy(aPath);
       
   344 	tmp1->Des().Format(KTestUrlFormat, tmp2, tmp3);
       
   345 	TUriParser8 up;
       
   346 	up.Parse(*tmp1);
       
   347 	iTestUrl = CUri8::NewL(up);
       
   348 	CleanupStack::PopAndDestroy(3, tmp1);
       
   349 	}
       
   350