applayerprotocols/httptransportfw/Test/T_HttpOnline/T_PostBodyChunks.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2004-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: CTextModeTestPostBodyChunks
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "T_PostBodyChunks.h"
       
    19 
       
    20 // User include
       
    21 #include "csrvaddrval.h"
       
    22 
       
    23 const TInt KMilliSeconds = 1000000;
       
    24 
       
    25 // Constants used in CTextModeTestPostBodyChunks test
       
    26 _LIT8(KHttpPostBodyChunksUrl,	"http://WapTestIP/perl/dumpform.pl");
       
    27 _LIT8(KHttpPostBodyChunk1, 		"abc=def&ghi=jkl&");
       
    28 _LIT8(KHttpPostBodyChunk2, 		"mno=pqr&stu=vwx&");
       
    29 _LIT8(KHttpPostBodyChunk3, 		"y=z");
       
    30 
       
    31 // CTextModeTestPostBodyChunks-----------------------------------
       
    32 
       
    33 /**
       
    34 	This function is called to create a new instance of the class 
       
    35 	CTextModeTestCookies
       
    36 
       
    37 	@param aIniSettingsFile	  The script file that holds the sections
       
    38 							  from which the value has to be retrieved
       
    39 	@leave 					  KErrNoMemory if there is insufficient memory
       
    40 */
       
    41 CTextModeTestPostBodyChunks* CTextModeTestPostBodyChunks::NewL(CScriptFile* aIniSettingsFile)
       
    42 	{
       
    43 	CTextModeTestPostBodyChunks* self = new(ELeave) CTextModeTestPostBodyChunks(aIniSettingsFile);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL();
       
    46 	CleanupStack::Pop();
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 /**
       
    51 	Destructor
       
    52 */
       
    53 CTextModeTestPostBodyChunks::~CTextModeTestPostBodyChunks()
       
    54 	{
       
    55 	}
       
    56 
       
    57 void CTextModeTestPostBodyChunks::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
    58 	{
       
    59 	switch (aEvent.iStatus)
       
    60 		{
       
    61 		case THTTPEvent::EGotResponseHeaders:
       
    62 			{
       
    63 			// HTTP response headers have been received
       
    64 			//iEngine->Utils().LogIt(_L("<Got response headers>"));
       
    65 			DumpRespHeaders(aTransaction);
       
    66 			} break;
       
    67 		case THTTPEvent::ESubmit:
       
    68 			{
       
    69 			} break;
       
    70 		case THTTPEvent::EGotResponseBodyData:
       
    71 			{
       
    72 			// Some (more) body data has been received (in the HTTP response)
       
    73 			//iEngine->Utils().LogIt(_L("<Got a chunk of data>"));
       
    74 			DumpResponseBody(aTransaction);
       
    75 			} break;
       
    76 		case THTTPEvent::EResponseComplete:
       
    77 			{
       
    78 			// The transaction's response is complete
       
    79 			iEngine->Utils().LogIt(_L("<Transaction Complete>"));
       
    80 			} break;
       
    81 		case THTTPEvent::ESucceeded:
       
    82 			{
       
    83 			iEngine->Utils().LogIt(_L("<Transaction validated successfully>"));
       
    84 			CActiveScheduler::Stop();
       
    85 			} break;
       
    86 		case THTTPEvent::EFailed:
       
    87 			{
       
    88 			iEngine->Utils().LogIt(
       
    89 				_L("<Transaction NOT validated successfully>"));
       
    90 			iFailureError = KErrTestFailed;
       
    91 			CActiveScheduler::Stop();
       
    92 			} break;
       
    93 		case THTTPEvent::EUnrecoverableError:
       
    94 			{
       
    95 			iEngine->Utils().LogIt(_L("<Test failed unrecoverably>"));
       
    96 			iFailureError = KErrTestFailed;
       
    97 			} break;
       
    98 		default:
       
    99 			{
       
   100 			iEngine->Utils().LogIt(_L("<unrecognised event> %d"), aEvent.iStatus);
       
   101 			if (aEvent.iStatus < 0)
       
   102 				{
       
   103 				iFailureError = aEvent.iStatus;
       
   104 
       
   105 				// Special case for doing heap-failure testing only - in some 
       
   106 				// failure circumstances, the outstanding socket read/writes 
       
   107 				// complete with a time out
       
   108 				if ((iLeakTests > 0) && (iFailureError == KErrTimedOut))
       
   109 					{
       
   110 					iFailureError = KErrTestFailed;
       
   111 					}
       
   112 					
       
   113 				CActiveScheduler::Stop();
       
   114 				}
       
   115 			} 
       
   116 			break;
       
   117 		}
       
   118 	}
       
   119 
       
   120 TInt CTextModeTestPostBodyChunks::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   121 	{
       
   122 	iEngine->Utils().LogIt(
       
   123 		_L("\nMHFRunL left: error code %d on transaction ID=%d for event %d\n"),
       
   124 		aError, aTransaction.Id(), aEvent.iStatus);
       
   125 	return KErrNone;
       
   126 	}
       
   127 
       
   128 /**
       
   129 	This function is called to get the next chunk of data.
       
   130 
       
   131 	@param aDataChunk	The pointer which holds the next data chunk.
       
   132 */
       
   133 TBool CTextModeTestPostBodyChunks::GetNextDataPart(TPtrC8& aDataChunk)
       
   134 	{
       
   135 	TBool retVal = EFalse;
       
   136 	switch (iReqBodyChunkCount)
       
   137 		{
       
   138 		case 0:
       
   139 			{
       
   140 			aDataChunk.Set(KHttpPostBodyChunk1());
       
   141 			} break;
       
   142 		case 1:
       
   143 			{
       
   144 			aDataChunk.Set(KHttpPostBodyChunk2());
       
   145 			} break;
       
   146 		case 2:
       
   147 			{
       
   148 			aDataChunk.Set(KHttpPostBodyChunk3());
       
   149 			retVal = ETrue;
       
   150 			} break;
       
   151 		}
       
   152 	iReqBodyChunkCount++;
       
   153 	return retVal;
       
   154 	}
       
   155 
       
   156 /**
       
   157 	Set timer to fire when we want to signal the availability of the next 
       
   158 	chunk, provided we've not finished
       
   159 */
       
   160 void CTextModeTestPostBodyChunks::ReleaseData()
       
   161 
       
   162 	{
       
   163 	if (iReqBodyChunkCount < 3)
       
   164 		{
       
   165 		iReleaseTimer->After(KMilliSeconds);
       
   166 		}
       
   167 	}
       
   168 
       
   169 TInt CTextModeTestPostBodyChunks::OverallDataSize()
       
   170 	{
       
   171 	return (KHttpPostBodyChunk1().Length() + KHttpPostBodyChunk2().Length() + KHttpPostBodyChunk3().Length());
       
   172 	}
       
   173 
       
   174 const TDesC& CTextModeTestPostBodyChunks::TestName()
       
   175 	{
       
   176 	_LIT(KHeaderTestName,"CTextModeTestPostBodyChunks");
       
   177 	return KHeaderTestName;
       
   178 	}
       
   179 
       
   180 /**
       
   181 	This function is called to initiate the tests.
       
   182 	It may leave with one of the system-wide error codes.
       
   183 */
       
   184 void CTextModeTestPostBodyChunks::DoRunL()
       
   185 	{
       
   186 	// Open the HTTP session
       
   187 	iSession.OpenL();
       
   188 	CleanupClosePushL(iSession);
       
   189 	RStringPool strP = iSession.StringPool();
       
   190 
       
   191 	// Open a POST transactions, specifying this object as the request body 
       
   192 	// data supplier
       
   193 	TUriParser8 up;
       
   194 
       
   195 	HBufC8* newUrl8 = TSrvAddrVal::ReplaceHostNameL(KHttpPostBodyChunksUrl(), iIniSettingsFile);
       
   196 	CleanupStack::PushL(newUrl8);
       
   197 	TPtr8 newUrlPtr8 = newUrl8->Des();
       
   198 
       
   199 	up.Parse(newUrlPtr8);
       
   200 	
       
   201 	iTransaction = iSession.OpenTransactionL(up, *this, strP.StringF(HTTP::EPOST,RHTTPSession::GetTable()));
       
   202 	RHTTPRequest rq = iTransaction.Request();
       
   203 	rq.SetBody(*this);
       
   204 	RHTTPHeaders hdr = rq.GetHeaderCollection();
       
   205 	THTTPHdrVal length(OverallDataSize());
       
   206 	hdr.SetFieldL(strP.StringF(HTTP::EContentLength,RHTTPSession::GetTable()), length);
       
   207 	THTTPHdrVal contType(strP.StringF(HTTP::EApplicationXWwwFormUrlEncoded, RHTTPSession::GetTable()));
       
   208 	hdr.SetFieldL(strP.StringF(HTTP::EContentType, RHTTPSession::GetTable()), contType);
       
   209 	
       
   210 	// Create the delayed notifier
       
   211 	iReleaseTimer = CDelayedBodyDataNotifier::NewL(iTransaction);
       
   212 	CleanupStack::PushL(iReleaseTimer);
       
   213 	CActiveScheduler::Add(iReleaseTimer);
       
   214 
       
   215 	// Submit the transaction
       
   216 	iTransaction.SubmitL();
       
   217 	iFailureError = KErrNone;
       
   218 	CActiveScheduler::Start();
       
   219 
       
   220 	// Get the completion code and inform the engine.  Anything other than 
       
   221 	// HTTP/200 status is a failure for this test.
       
   222 	iEngine->SetCurrentStatusCode(iTransaction.Response().StatusCode());
       
   223 	iExpectedStatusCode = 200;
       
   224 
       
   225 	// Close anything opened
       
   226 	CleanupStack::PopAndDestroy(3, &iSession); // and iReleaseTimer, newUrl8;
       
   227 
       
   228 	// Check for failure error codes caught in MHFRunL
       
   229 	User::LeaveIfError(iFailureError);
       
   230 	}
       
   231 
       
   232 TInt CTextModeTestPostBodyChunks::RunError(TInt aErr)
       
   233 	{
       
   234 	iEngine->Utils().LogIt(_L("\nTest failed with error code %d\n"), aErr);
       
   235 	return KErrNone;
       
   236 	}
       
   237 	
       
   238 void CTextModeTestPostBodyChunks::DoCancel()
       
   239 	{
       
   240 	iReleaseTimer->Cancel();
       
   241 	}
       
   242 
       
   243 CTextModeTestPostBodyChunks::CTextModeTestPostBodyChunks(CScriptFile* aIniSettingsFile) :
       
   244 	iIniSettingsFile(aIniSettingsFile)
       
   245 	{
       
   246 	}
       
   247 
       
   248 /**
       
   249 	This constructor is called in the NewL function and used by the class to
       
   250 	construct self.  Currently this has no functionality.  In future this 
       
   251 	constructor can be used.
       
   252 */
       
   253 void CTextModeTestPostBodyChunks::ConstructL()
       
   254 	{
       
   255 	}
       
   256 
       
   257 CTextModeTestPostBodyChunks::CDelayedBodyDataNotifier*
       
   258 CTextModeTestPostBodyChunks::CDelayedBodyDataNotifier::NewL(RHTTPTransaction aTrans)
       
   259 	{
       
   260 	CDelayedBodyDataNotifier* self = new(ELeave) CDelayedBodyDataNotifier(aTrans);
       
   261 	CleanupStack::PushL(self);
       
   262 	self->ConstructL();
       
   263 	CleanupStack::Pop(self);
       
   264 	return self;
       
   265 	}
       
   266 
       
   267 CTextModeTestPostBodyChunks::CDelayedBodyDataNotifier::CDelayedBodyDataNotifier(RHTTPTransaction aTrans)
       
   268 	: 	CTimer(EPriorityStandard), iTrans(aTrans)
       
   269 	{
       
   270 	}
       
   271 
       
   272 void CTextModeTestPostBodyChunks::CDelayedBodyDataNotifier::RunL()
       
   273 	{
       
   274 	iTrans.NotifyNewRequestBodyPartL();
       
   275 	}
       
   276