applayerprotocols/httptransportfw/Test/TestScriptTest/CPipeliningTestClient.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-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 //
       
    15 
       
    16 #include "CPipeliningTestClient.h"
       
    17 #include "httptestutils.h"
       
    18 #include "MPipeliningTestCase.h"
       
    19 #include "MPipeliningTestObserver.h"
       
    20 
       
    21 CPipeliningTestClient* CPipeliningTestClient::NewL(CHTTPTestUtils& aTestUtils, MPipeliningTestObserver& aObserver)
       
    22 	{
       
    23 	CPipeliningTestClient* self = new (ELeave) CPipeliningTestClient(aTestUtils, aObserver);
       
    24 	CleanupStack::PushL(self);
       
    25 	self->ConstructL();
       
    26 	CleanupStack::Pop(self);
       
    27 	return self;
       
    28 	}
       
    29 
       
    30 CPipeliningTestClient::CPipeliningTestClient(CHTTPTestUtils& aTestUtils, MPipeliningTestObserver& aObserver)
       
    31 : iTestUtils(aTestUtils), iObserver(aObserver)
       
    32 	{
       
    33 	}
       
    34 
       
    35 void CPipeliningTestClient::ConstructL()
       
    36 	{
       
    37 	iSession.OpenL();
       
    38 	}
       
    39 
       
    40 CPipeliningTestClient::~CPipeliningTestClient()
       
    41 	{
       
    42 	iTransArray.Reset();
       
    43 	iTransArray.Close();
       
    44 	iSession.Close();
       
    45 	iNetworkConnection.Close();
       
    46 	iSocketServ.Close();
       
    47 	}
       
    48 
       
    49 RHTTPSession& CPipeliningTestClient::GetSession()
       
    50 	{
       
    51 	return iSession;
       
    52 	}
       
    53 	
       
    54 // From MHTTPTransactionCallback
       
    55 void CPipeliningTestClient::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
    56 	{
       
    57 	TInt expectedError = iTestCase->ExpectedError(aTransaction);		
       
    58 	TInt tranID = aTransaction.Id();
       
    59 	switch (aEvent.iStatus)
       
    60 		{
       
    61 		case THTTPEvent::EGotResponseHeaders:
       
    62 			{
       
    63 			} break;
       
    64 		case THTTPEvent::EGotResponseBodyData:
       
    65 			{
       
    66 			// Done with that bit of body data
       
    67 			aTransaction.Response().Body()->ReleaseData();
       
    68 			} break;
       
    69 		case THTTPEvent::EResponseComplete:
       
    70 			{
       
    71 			TRAPD(err1, iTestCase->ProcessHeadersL(aTransaction));
       
    72 			if ( err1 != KErrNone )
       
    73 				{
       
    74 				_LIT(KProcessHeadersError, "Client - Transaction %d Failed, Error = %d");
       
    75 				iTestUtils.LogIt(KProcessHeadersError, tranID, err1);
       
    76 				EndTestL(err1);
       
    77 				}
       
    78 			} break;
       
    79 		case THTTPEvent::ESucceeded:
       
    80 			{
       
    81 			++iCurrentTrans;
       
    82 			aTransaction.Close();
       
    83 			_LIT(KTxtSuccessful, "Client - Transaction %d, completed successfully.");
       
    84 			iTestUtils.LogIt(KTxtSuccessful, tranID);
       
    85 
       
    86 			if( expectedError != KErrNone )
       
    87 				{
       
    88 				_LIT(KTxtErrorExpected, "Client - Expected error code %d");
       
    89 				iTestUtils.LogIt(KTxtErrorExpected, expectedError);
       
    90 				EndTestL(KErrNotFound);
       
    91 				}
       
    92 
       
    93 			if( iTestCase->TotalTransactionCount() == iCurrentTrans )
       
    94 				{	
       
    95 				EndTestL(KErrNone);
       
    96 				}
       
    97 			} break;
       
    98 		case THTTPEvent::EFailed:
       
    99 			{
       
   100 			++iCurrentTrans;
       
   101 			aTransaction.Close();
       
   102 			if( expectedError == iLastError )
       
   103 				{
       
   104 				_LIT(KTxtExpectedError, "Client - Transaction %d Failed, Expected: %d, Actual: %d");
       
   105 				iTestUtils.LogIt(KTxtExpectedError(), tranID, expectedError, iLastError);
       
   106 				if( iTestCase->TotalTransactionCount() == iCurrentTrans )
       
   107 					{
       
   108 					EndTestL(KErrNone);
       
   109 					}
       
   110 				}
       
   111 			else
       
   112 				{
       
   113 				_LIT(KTxtErrorUnexpected, "Client - Error code mismatch.");
       
   114 				iTestUtils.LogIt(KTxtErrorUnexpected());
       
   115 				EndTestL(KErrNotFound);
       
   116 				}
       
   117 			} break;
       
   118 		case THTTPEvent::ERedirectedPermanently:
       
   119 			{
       
   120 			} break;
       
   121 		case THTTPEvent::ERedirectedTemporarily:
       
   122 			{
       
   123 			} break;
       
   124 		case THTTPEvent::ERedirectRequiresConfirmation:
       
   125  			{
       
   126  			aTransaction.SubmitL();
       
   127  			} break;
       
   128 		default:
       
   129 			{
       
   130 			if (aEvent.iStatus < 0)
       
   131 				{
       
   132 				
       
   133 				iLastError = aEvent.iStatus;
       
   134 				_LIT(KTxtClientError, "Client - Transaction %d, received error code %d");
       
   135 				iTestUtils.LogIt(KTxtClientError, tranID, iLastError);
       
   136 				
       
   137 				}
       
   138 			
       
   139 			} break;
       
   140 		}
       
   141 	}
       
   142 	
       
   143 	
       
   144 void CPipeliningTestClient::EndTestL(TInt aErrorCode)
       
   145 	{
       
   146 	#if defined (_DEBUG)
       
   147 	_LIT(KTxtClientError, "Error - Code %d");	
       
   148 	iTestUtils.LogIt(KTxtClientError, aErrorCode);      	
       
   149 	
       
   150 	if (aErrorCode == KErrNone)
       
   151 		{
       
   152 		TInt expectedConnections = iTestCase->RealExpectedConnectionCount();
       
   153 		RStringPool stringPool = iSession.StringPool();
       
   154 		_LIT8(KNumberConnectionManagers, "__NumConnectionManagers");
       
   155 		RStringF numberConnectionsString = stringPool.OpenFStringL(KNumberConnectionManagers);
       
   156 		CleanupClosePushL(numberConnectionsString);
       
   157 		RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
       
   158 		TInt numberConnections =0;
       
   159 	
       
   160 		THTTPHdrVal numberConnectionsVal;
       
   161 		if (connInfo.Property(numberConnectionsString, numberConnectionsVal))
       
   162 			{
       
   163 			numberConnections = numberConnectionsVal.Int();
       
   164 			connInfo.RemoveProperty(numberConnectionsString);
       
   165 			
       
   166 			if (numberConnections > expectedConnections)
       
   167 				{
       
   168                 _LIT(KTxtClientError, "Client - Connections %d, Expected: %d");
       
   169                 iTestUtils.LogIt(KTxtClientError, numberConnections, expectedConnections);				
       
   170 				aErrorCode = KErrNotFound;
       
   171 				}
       
   172 
       
   173 			}
       
   174 		
       
   175 		CleanupStack::PopAndDestroy(&numberConnectionsString);
       
   176 		}
       
   177 	#endif
       
   178 	
       
   179 	iObserver.EndTest(aErrorCode);
       
   180 	}	
       
   181 	
       
   182 	
       
   183 TInt CPipeliningTestClient::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& /*aEvent*/)
       
   184 	{
       
   185 	_LIT(KTxtRunError, "Client - MHFRunError - Transaction %d with error code %d.");
       
   186 	iTestUtils.LogIt(KTxtRunError(), aTransaction.Id(), aError);
       
   187 
       
   188 	return KErrNone;
       
   189 	}
       
   190 
       
   191 void CPipeliningTestClient::AddTransactionL(RHTTPTransaction aTransaction)
       
   192 	{
       
   193 	User::LeaveIfError(iTransArray.Append(aTransaction));
       
   194 	}
       
   195 	
       
   196 void CPipeliningTestClient::StartClientL()
       
   197 	{
       
   198 	__ASSERT_DEBUG(iTestCase != NULL, User::Invariant());
       
   199 
       
   200 	if( iTestCase->EnableBatching() )
       
   201 		{
       
   202 		// @todo we need to add the session property for enabling batching
       
   203 		RHTTPConnectionInfo connInfo = GetSession().ConnectionInfo();
       
   204 		THTTPHdrVal batchingSupport(iSession.StringPool().StringF(HTTP::EEnableBatching,RHTTPSession::GetTable()));
       
   205 		connInfo.SetPropertyL(iSession.StringPool().StringF(HTTP::EHttpBatching,RHTTPSession::GetTable()), batchingSupport);
       
   206 		}
       
   207 
       
   208 	TBool batchTrans = iTestCase->BatchTransactions();	
       
   209 	// Create transactions
       
   210 	for( TInt ii=0; ii < iTestCase->TotalTransactionCount(); ++ii )
       
   211 		{
       
   212 		RHTTPTransaction trans = iTestCase->GetTransactionL(ii, GetSession(), *this);
       
   213 		CleanupClosePushL(trans);
       
   214 		AddTransactionL(trans);
       
   215 		if(!batchTrans)
       
   216 			{
       
   217 			trans.SubmitL();
       
   218 			}
       
   219 		CleanupStack::Pop(&trans);
       
   220 		}
       
   221 	
       
   222 	if(batchTrans)
       
   223 		{
       
   224 		for( TInt jj=0; jj < iTransArray.Count(); ++jj )
       
   225 			{
       
   226 			iTransArray[jj].SubmitL();
       
   227 			}
       
   228 		}
       
   229 	}
       
   230 	
       
   231 TInt CPipeliningTestClient::LastError()
       
   232 {
       
   233 	return iLastError;
       
   234 }
       
   235 
       
   236 void CPipeliningTestClient::SetTestCase(MPipeliningTestCase* aTestCase)
       
   237 	{
       
   238 	iTestCase = aTestCase;
       
   239 	}
       
   240 
       
   241 void CPipeliningTestClient::StartAndSetNetworkToHttp()
       
   242     {
       
   243     if(iSocketServ.Handle() != KNullHandle)
       
   244         {
       
   245         iNetworkConnection.Close();
       
   246         iSocketServ.Close();
       
   247         }
       
   248     User::LeaveIfError(iSocketServ.Connect());
       
   249     User::LeaveIfError(iNetworkConnection.Open(iSocketServ));
       
   250     User::LeaveIfError(iNetworkConnection.Start());
       
   251     // Set the RSocketServ & RConnection
       
   252     RStringPool strPool = iSession.StringPool();
       
   253     RHTTPConnectionInfo connInfo1 = iSession.ConnectionInfo();
       
   254     connInfo1.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ, 
       
   255                              RHTTPSession::GetTable() ), 
       
   256                              THTTPHdrVal (iSocketServ.Handle()) );
       
   257     TInt connPtr = REINTERPRET_CAST(TInt, &iNetworkConnection);                
       
   258     connInfo1.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection, 
       
   259                              RHTTPSession::GetTable() ), 
       
   260                              THTTPHdrVal(connPtr));    
       
   261     }
       
   262