applayerprotocols/httptransportfw/Test/T_HttpOnline/cinc105767.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2007-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 "cinc105767.h"
       
    17 #include "httptestutils.h"
       
    18 #include <es_sock.h>
       
    19 #include <httperr.h>
       
    20 
       
    21 _LIT8 ( KUriTransaction, "http://10.29.23.12:9000/tryit?req=4131.txt&resp=/dev/null" );
       
    22 _LIT8 ( KUriTransaction2, "http://10.29.23.12/mp3/sing_along.mp3" );
       
    23 _LIT8 ( KUriTransaction3, "http://10.29.23.12:9001");
       
    24 
       
    25 /**
       
    26 	This function is called to create a new instance of the class 
       
    27 	CINC105767
       
    28 
       
    29 	@param aTestNumber			The test number that has to be executed
       
    30 	@param aIniSettingsFile		The script file that holds the sections
       
    31 								from which the value has to be retrieved
       
    32 	@leave 						Leaves with a standard error
       
    33 */
       
    34 CINC105767* CINC105767::NewL(TInt aTestNumber, CScriptFile* aIniSettingsFile)
       
    35 	{
       
    36 	CINC105767* self = new(ELeave)CINC105767(aTestNumber, aIniSettingsFile);
       
    37 	CleanupStack::PushL(self);
       
    38 	CleanupStack::Pop(self);
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 /**
       
    43 	Destructor
       
    44 */
       
    45 CINC105767::~CINC105767()
       
    46 	{
       
    47 	}
       
    48 
       
    49 /**
       
    50 	This C++ constructor is called in the NewL function and is used to 
       
    51 	instantiate iTestNumber and iIniSettingsFile
       
    52 
       
    53 	@param aTestNumber		- The test number that has to be executed
       
    54 	@param aIniSettingsFile	- The script file that holds the sections
       
    55 							  from which the value has to be retrieved
       
    56 */
       
    57 CINC105767::CINC105767(TInt aTestNumber, CScriptFile* aIniSettingsFile) :
       
    58 	iIniSettingsFile(aIniSettingsFile),
       
    59 	iTestNumber(aTestNumber)
       
    60 	{
       
    61 	// does nothing
       
    62 	}
       
    63 
       
    64 /**
       
    65 	This function is called to initiate the tests.
       
    66 	
       
    67 	@leave	Leaves with a standard error.
       
    68 */
       
    69 void CINC105767::DoRunL()
       
    70 	{
       
    71 	TUriParser8 up;
       
    72 	up.Parse(KUriTransaction);
       
    73 
       
    74 	// Open the HTTP session
       
    75 	iSession.OpenL();
       
    76 	CleanupClosePushL(iSession);
       
    77 
       
    78 	RStringPool strP = iSession.StringPool();
       
    79 
       
    80 	// Open a GET transactions, specifying this object as the request body data supplier
       
    81 	iTransaction = iSession.OpenTransactionL(up, *this, strP.StringF(HTTP::EGET,RHTTPSession::GetTable()));
       
    82 	iEngine->Utils().LogIt( _L("\nSubmitting first transaction\n") );
       
    83 	iTransaction.SubmitL();
       
    84 	iTransaction.PropertySet().SetPropertyL (iSession.StringPool().StringF(HTTP::ENotifyOnDisconnect,RHTTPSession::GetTable()), iSession.StringPool().StringF(HTTP::EEnableDisconnectNotification,RHTTPSession::GetTable())); 
       
    85 	iExpectedError = KErrHttpResponseNotReceived;
       
    86 	CActiveScheduler::Start();	
       
    87 	User::LeaveIfError ( iFailureError );
       
    88 	iTransaction.Close ();
       
    89 	
       
    90 	up.Parse(KUriTransaction2);
       
    91 	iTransaction = iSession.OpenTransactionL(up, *this, strP.StringF(HTTP::EGET,RHTTPSession::GetTable()));	
       
    92 	iEngine->Utils().LogIt( _L("\nSubmitting second transaction\n") );
       
    93 	iTransaction.SubmitL();
       
    94 	iExpectedError = KErrHttpPartialResponseReceived;
       
    95 	CActiveScheduler::Start();	
       
    96 	User::LeaveIfError ( iFailureError );
       
    97 	iTransaction.Close ();
       
    98 	
       
    99 	up.Parse(KUriTransaction3);
       
   100 	iTransaction = iSession.OpenTransactionL(up, *this, strP.StringF(HTTP::EPOST,RHTTPSession::GetTable()));	
       
   101 	RHTTPRequest rq = iTransaction.Request();
       
   102 	rq.SetBody(*this);
       
   103 	RHTTPHeaders hdr = rq.GetHeaderCollection();
       
   104 	THTTPHdrVal length(OverallDataSize());
       
   105 	hdr.SetFieldL(strP.StringF(HTTP::EContentLength,RHTTPSession::GetTable()), length);
       
   106 	THTTPHdrVal contType(strP.StringF(HTTP::ETextPlain, RHTTPSession::GetTable()));
       
   107 	hdr.SetFieldL(strP.StringF(HTTP::EContentType, RHTTPSession::GetTable()), contType);
       
   108 	
       
   109 	iEngine->Utils().LogIt( _L("\nSubmitting third transaction\n") );	
       
   110 	iTransaction.SubmitL();
       
   111 	iTransaction.PropertySet().SetPropertyL (iSession.StringPool().StringF(HTTP::ENotifyOnDisconnect,RHTTPSession::GetTable()), iSession.StringPool().StringF(HTTP::EEnableDisconnectNotification,RHTTPSession::GetTable())); 
       
   112 	iExpectedError = KErrHttpRequestNotSent;
       
   113 	CActiveScheduler::Start();	
       
   114 	User::LeaveIfError ( iFailureError );
       
   115 	iTransaction.Close ();
       
   116 	
       
   117 	CleanupStack::PopAndDestroy(&iSession); //close iSession
       
   118 	}
       
   119 
       
   120 /**
       
   121 	If a test is failed then is function is executed to log the error code
       
   122 	with which the test failed
       
   123 
       
   124 	@param	aErr	The error code with which the test failed.
       
   125 */
       
   126 TInt CINC105767::RunError(TInt aErr)
       
   127 	{
       
   128 	iEngine->Utils().LogIt(_L("\nTest failed with error code %d\n"), aErr);
       
   129 	return KErrNone;
       
   130 	}
       
   131 
       
   132 void CINC105767::DoCancel()
       
   133 	{
       
   134 	}
       
   135 
       
   136 const TDesC& CINC105767::TestName()
       
   137 	{
       
   138 	_LIT(KHeaderTestName,"CINC105767");
       
   139 	return KHeaderTestName;
       
   140 	}
       
   141 
       
   142 TInt CINC105767::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& /*aEvent*/)
       
   143 	{
       
   144 	_LIT(KLogDescription, "\nTest failed with error code %d on transaction ID=%d\n");
       
   145 	iEngine->Utils().LogIt(KLogDescription, aError, aTransaction.Id());
       
   146 	iFailureError = aError;
       
   147 	CActiveScheduler::Stop();
       
   148 	return KErrNone;
       
   149 	}
       
   150 
       
   151 void CINC105767::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   152 	{
       
   153 	switch (aEvent.iStatus)
       
   154 		{
       
   155 		case THTTPEvent::EGotResponseHeaders:
       
   156 			{
       
   157 			// HTTP response headers have been received
       
   158 			iEngine->Utils().LogIt(_L("<Got response headers>"));
       
   159 			} break;
       
   160 		case THTTPEvent::ESubmit:
       
   161 			{
       
   162 			} break;
       
   163 		case THTTPEvent::EGotResponseBodyData:
       
   164 			{
       
   165 			// Some (more) body data has been received (in the HTTP response)
       
   166 			TPtrC8 bodyData;
       
   167 			if(aTransaction.Response().Body()->GetNextDataPart(bodyData) )
       
   168 				{
       
   169 				iEngine->Utils().LogIt(_L("1,Got Last Chunk...\n"));
       
   170 				}
       
   171 			aTransaction.Response().Body()->ReleaseData();
       
   172 			iTransaction.PropertySet().SetPropertyL (iSession.StringPool().StringF(HTTP::ENotifyOnDisconnect,RHTTPSession::GetTable()), iSession.StringPool().StringF(HTTP::EEnableDisconnectNotification,RHTTPSession::GetTable())); 			
       
   173 			CloseConnection ();
       
   174 			} break;
       
   175 		case THTTPEvent::EResponseComplete:
       
   176 			{
       
   177 			// The transaction's response is complete
       
   178 			iEngine->Utils().LogIt(_L("<Transaction Complete>"));
       
   179 			} break;
       
   180 		case THTTPEvent::ESucceeded:
       
   181 			{
       
   182 			// The transaction succeeded
       
   183 			iEngine->Utils().LogIt(_L("<Transaction succeeded>"));
       
   184 			aTransaction.Close();
       
   185 			CActiveScheduler::Stop();
       
   186 			}
       
   187 			break;
       
   188 		case THTTPEvent::EFailed:
       
   189 			{
       
   190 			// The transaction failed so fail the test
       
   191 			iEngine->Utils().LogIt(_L("<Transaction failed>"));
       
   192 			aTransaction.Close();
       
   193 			CActiveScheduler::Stop();
       
   194 			} break;
       
   195 		default:
       
   196 			{
       
   197 			_LIT(KLogDescription, "<unrecognised event> %d");
       
   198 			iEngine->Utils().LogIt(KLogDescription,aEvent.iStatus);
       
   199 			if (aEvent.iStatus < 0)
       
   200 				{
       
   201 				if ( aEvent.iStatus != iExpectedError )
       
   202 					iFailureError = aEvent.iStatus;
       
   203 				CActiveScheduler::Stop();
       
   204 				}
       
   205 			} 
       
   206 			break;
       
   207 		}
       
   208 	return;
       
   209 	}
       
   210 
       
   211 void CINC105767::CloseConnection ()
       
   212 	{
       
   213 	RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
       
   214 	RStringPool stringPool = iSession.StringPool ();
       
   215 
       
   216 	THTTPHdrVal value;
       
   217 	TBool hasValue = connInfo.Property ( stringPool.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable()), value );
       
   218 	if (hasValue)
       
   219 		{
       
   220 		RConnection* conn = REINTERPRET_CAST( RConnection*, value.Int() );
       
   221 		TUint count;
       
   222 		//get the no of active connections
       
   223 		TInt err = conn->EnumerateConnections( count );
       
   224 		if ( err == KErrNone )
       
   225 			{
       
   226 			if ( IsConnectionActive( *conn ) )
       
   227 				{
       
   228 				iEngine->Utils().LogIt( _L("\nConnection is active. Stop the active connection.\n") );
       
   229 				conn->Stop ();  // stop the active connection
       
   230 				if ( !IsConnectionActive(  *conn ) )
       
   231 					{
       
   232 					iEngine->Utils().LogIt( _L("\nConnection stopped.\n") );
       
   233 					}
       
   234 				else
       
   235 					{
       
   236 					iEngine->Utils().LogIt( _L("\nConnection stopped failed.\n") );
       
   237 					}				
       
   238 				}				
       
   239 			}		
       
   240 		else
       
   241 			{
       
   242 			iEngine->Utils().LogIt(_L("Unable to enumerate number of connections, Error: %d"), err);
       
   243 			User::Leave ( KErrNotFound );
       
   244 			}
       
   245 		}		
       
   246 	}
       
   247 
       
   248 TBool CINC105767::IsConnectionActive ( RConnection& aConnection )
       
   249 	{
       
   250 	TRequestStatus status;
       
   251 	TUint secs = 1; // 1 secs
       
   252 	TBool state = EFalse;
       
   253 
       
   254 	TPckg < TBool > stateDes( state );
       
   255 
       
   256 	aConnection.IsConnectionActiveRequest( secs, stateDes, status );
       
   257 	User::WaitForRequest ( status );
       
   258 
       
   259 	return state;
       
   260 	}
       
   261 
       
   262 TBool CINC105767::GetNextDataPart(TPtrC8& aDataChunk)
       
   263 	{
       
   264 	iEngine->Utils().LogIt( _L("\nPosting some data.\n") );
       
   265 	_LIT8 ( KPostData, "abcdefghijklmnopqrstuvwxvz");
       
   266 	aDataChunk.Set(KPostData());
       
   267 	return EFalse;		
       
   268 	}
       
   269 
       
   270 void CINC105767::ReleaseData()
       
   271 	{
       
   272 	// Do nothing	
       
   273 	}
       
   274 
       
   275 TInt CINC105767::OverallDataSize()
       
   276 	{
       
   277 	// return some value
       
   278 	return 20 * 1024;
       
   279 	}
       
   280 
       
   281 TInt CINC105767::Reset ()
       
   282 	{
       
   283 	return KErrNone;	
       
   284 	}