http/src/testhttpeboclientconditionalstep.cpp
branchSymbian2
changeset 1 8758140453c0
child 6 c108117318cb
equal deleted inserted replaced
0:e8c1ea2c6496 1:8758140453c0
       
     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 the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Contains implementation of CHttpEboClientStep class
       
    15 // @internalAll
       
    16 // 
       
    17 //
       
    18 
       
    19 // User Include
       
    20 #include "testhttpeboclientconditionalstep.h"
       
    21 
       
    22 // Standard headers used by default
       
    23 _LIT8(KUserAgent, "TestHttpEboClientConditional");
       
    24 _LIT8(KAccept, "*/*");
       
    25 _LIT8(KHttpUrl1, "http://www.google.co.in/");
       
    26 _LIT8(KHttpUrl2, "http://www.live.com/");
       
    27 _LIT8(KHttpUrl3, "http://www.search.com/");
       
    28 _LIT8(KHttpUrl4, "http://www.find.com/");
       
    29 
       
    30 /**
       
    31 Constructor: Sets the test step name.
       
    32 @internalTechnology
       
    33 @test
       
    34 */
       
    35 CHttpEboClientConditionalStep::CHttpEboClientConditionalStep()
       
    36 	{
       
    37 	
       
    38 	//Call base class method to set human readable name for test step
       
    39 	SetTestStepName(KTestHttpEboClientConditionalStep);
       
    40 	}
       
    41 
       
    42 
       
    43 /**
       
    44 Destructor
       
    45 @internalTechnology
       
    46 @test
       
    47 */
       
    48 CHttpEboClientConditionalStep::~CHttpEboClientConditionalStep()
       
    49 	{
       
    50 	}
       
    51 
       
    52 
       
    53 /**
       
    54 Sets the proxy and HttpDataOptimiser implemetation to session
       
    55 based on the patameter values in ini file and calls InvokeHttpMethodL()
       
    56 @internalTechnology
       
    57 @test
       
    58 @param		None
       
    59 @return		None
       
    60 */
       
    61 void CHttpEboClientConditionalStep::StartClientL()
       
    62 	{
       
    63 	if (!GetBoolFromConfig(ConfigSection(), KDisableTransOpt, iDisableTransOpt))
       
    64 		{
       
    65 		ERR_PRINTF2(_L("Problem in reading values from ini.			\
       
    66 						\nExpected fields are: \n%S\n"
       
    67 					  ),&KDisableTransOpt);
       
    68 		SetTestStepResult(EFail);
       
    69 		return;
       
    70 		}
       
    71 
       
    72 	RStringPool strP = iSess.StringPool();
       
    73 	RStringF method;
       
    74 	
       
    75 	// method
       
    76 	method = strP.StringF(HTTP::EGET,RHTTPSession::GetTable());		
       
    77 	TBuf8<256> url8;
       
    78 	TRAPD(err,InvokeHttpMethodL(url8, method));
       
    79 	if (err != KErrNone)
       
    80 		{
       
    81 		ERR_PRINTF2(_L("Teststep Failed: Leave occured in CTestHttpBufferSizeStep::InvokeHttpMethodL: %D\n"
       
    82 					  ),err
       
    83 				   );
       
    84 		SetTestStepResult(EFail);
       
    85 		}
       
    86 	method.Close(); 
       
    87 	}
       
    88 
       
    89 /**
       
    90 Invoke the http method
       
    91 This actually creates the transaction, sets the headers and HttpDataOptimiser implemetation to transaction
       
    92 and then starts the transaction 
       
    93 @internalTechnology
       
    94 @test
       
    95 @param		aUri 		Requested uri
       
    96 @param		aMethod 	Requested method
       
    97 @return		None		
       
    98 */
       
    99 void CHttpEboClientConditionalStep::InvokeHttpMethodL(const TDesC8& /*aUri*/, RStringF aMethod)
       
   100 	{
       
   101 	TUriParser8 uri1; 
       
   102 	uri1.Parse( KHttpUrl1 );
       
   103 	
       
   104 	// Opening transactions	
       
   105 	RHTTPTransaction trans1 = iSess.OpenTransactionL(uri1, *this, aMethod);
       
   106 	CleanupClosePushL(trans1);
       
   107 	RHTTPHeaders hdr1 = trans1.Request().GetHeaderCollection();
       
   108 	SetHeaderL(hdr1, HTTP::EUserAgent, KUserAgent);
       
   109 	SetHeaderL(hdr1, HTTP::EAccept, KAccept);
       
   110 		
       
   111 	TUriParser8 uri2; 
       
   112 	uri2.Parse( KHttpUrl2 );
       
   113 	RHTTPTransaction trans2 = iSess.OpenTransactionL(uri2, *this, aMethod);
       
   114 	CleanupClosePushL(trans2);
       
   115 	RHTTPHeaders hdr2 = trans2.Request().GetHeaderCollection();
       
   116 	SetHeaderL(hdr2, HTTP::EUserAgent, KUserAgent);
       
   117 	SetHeaderL(hdr2, HTTP::EAccept, KAccept);
       
   118 	
       
   119 	TUriParser8 uri3; 
       
   120 	uri3.Parse( KHttpUrl3 );
       
   121 	RHTTPTransaction trans3 = iSess.OpenTransactionL(uri3, *this, aMethod);
       
   122 	CleanupClosePushL(trans3);
       
   123 	RHTTPHeaders hdr3 = trans3.Request().GetHeaderCollection();
       
   124 	SetHeaderL(hdr3, HTTP::EUserAgent, KUserAgent);
       
   125 	SetHeaderL(hdr3, HTTP::EAccept, KAccept);
       
   126 	
       
   127 	TUriParser8 uri4; 
       
   128 	uri4.Parse( KHttpUrl4 );
       
   129 	RHTTPTransaction trans4 = iSess.OpenTransactionL(uri4, *this, aMethod);
       
   130 	CleanupClosePushL(trans4);
       
   131 	RHTTPHeaders hdr4 = trans4.Request().GetHeaderCollection();
       
   132 	SetHeaderL(hdr4, HTTP::EUserAgent, KUserAgent);
       
   133 	SetHeaderL(hdr4, HTTP::EAccept, KAccept);
       
   134 		
       
   135 	if (iDisableTransOpt)
       
   136 		{
       
   137 		iSess.SetupHttpDataOptimiser(*this);
       
   138 		THTTPHdrVal disableOptimiser(iSess.StringPool().StringF(HTTP::EDisableHTTPTransOptimiser,RHTTPSession::GetTable()));
       
   139 		trans3.PropertySet().SetPropertyL(iSess.StringPool().StringF(HTTP::EHTTPTransOptimiser,RHTTPSession::GetTable()), disableOptimiser);
       
   140 		}
       
   141 	else
       
   142 		{
       
   143 		trans1.SetupHttpDataOptimiser(*this);
       
   144 		trans2.SetupHttpDataOptimiser(*this);
       
   145 		trans4.SetupHttpDataOptimiser(*this);
       
   146 		}
       
   147 	
       
   148 	trans1.SubmitL();
       
   149 	CActiveScheduler::Start();
       
   150 		
       
   151 	trans2.SubmitL();
       
   152 	CActiveScheduler::Start();
       
   153 		
       
   154 	trans3.SubmitL();
       
   155 	CActiveScheduler::Start();
       
   156 		
       
   157 	trans4.SubmitL();
       
   158 	CActiveScheduler::Start();
       
   159 	
       
   160 	CleanupStack::Pop(&trans4);
       
   161 	CleanupStack::Pop(&trans3);
       
   162 	CleanupStack::Pop(&trans2);
       
   163 	CleanupStack::Pop(&trans1);
       
   164 	}
       
   165 
       
   166 /**
       
   167 This is the implementation of interface method MHttpDataOptimiser::MHFRunL
       
   168 If there is a reponse body it will be copied to a file.
       
   169 Just printing the fired events to the log file.
       
   170 @internalTechnology 
       
   171 @test
       
   172 @param		aTransaction Transaction which is being processed currently
       
   173 @param      THTTPEvent Event for which it is called
       
   174 @return		None
       
   175 */	
       
   176 void CHttpEboClientConditionalStep::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   177 	{
       
   178 	switch (aEvent.iStatus)
       
   179 		{
       
   180 		case THTTPEvent::EGotResponseHeaders:
       
   181 			{
       
   182 			INFO_PRINTF2(_L("Processing Transaction %d\n"), aTransaction.Id());
       
   183 			RHTTPResponse resp = aTransaction.Response();
       
   184 			TInt status = resp.StatusCode();
       
   185 			RStringF statusStr = resp.StatusText();
       
   186 			TBuf<32> statusStr16;
       
   187 			statusStr16.Copy(statusStr.DesC());
       
   188 			INFO_PRINTF3(_L("Status: %D (%S)\n"),status, &statusStr16);
       
   189 
       
   190 			if (resp.HasBody() && (status >= 200) && (status < 300) && (status != 204))
       
   191 				{
       
   192 				TInt dataSize = resp.Body()->OverallDataSize();
       
   193 				if (dataSize >= 0)
       
   194 					INFO_PRINTF2(_L("Response body size is %d\n"), dataSize);
       
   195 				else
       
   196 					INFO_PRINTF1(_L("Response body size is unknown\n"));
       
   197 				} 
       
   198 			} break;
       
   199 		case THTTPEvent::EGotResponseBodyData:
       
   200 			{
       
   201 			// Get the body data supplier
       
   202 			iRespBody = aTransaction.Response().Body();
       
   203 			INFO_PRINTF1(_L("Receiving response body...\n"));
       
   204 			
       
   205 			TPtrC8 bodyData;
       
   206 			TBool lastChunk = iRespBody->GetNextDataPart(bodyData);
       
   207 			if(bodyData.Size() > iChunkSize)
       
   208 				{
       
   209 				iChunkSize = bodyData.Size();
       
   210 				}
       
   211 			iRespBody->ReleaseData();
       
   212 			} break;
       
   213 		case THTTPEvent::EResponseComplete:
       
   214 			{
       
   215 			// The transaction's response is complete
       
   216 			INFO_PRINTF1(_L("\nTransaction Complete\n"));
       
   217 			} break;
       
   218 		case KErrHttpOptimiserFailsTrans:
       
   219 			{
       
   220 			INFO_PRINTF1(_L("Cancelling/Failing Transaction\n"));
       
   221 			aTransaction.Fail(THTTPFilterHandle::EProtocolHandler);
       
   222 			//CActiveScheduler::Stop();
       
   223 			} break;
       
   224 		case THTTPEvent::ESucceeded:
       
   225 			{
       
   226 			INFO_PRINTF1(_L("Transaction Successful\n"));
       
   227 			aTransaction.Close();
       
   228 			CActiveScheduler::Stop();
       
   229 			} break;
       
   230 		case THTTPEvent::EFailed:
       
   231 			{
       
   232 			INFO_PRINTF1(_L("Transaction Failed\n"));
       
   233 			aTransaction.Close();
       
   234 			CActiveScheduler::Stop();
       
   235 			} break;
       
   236 		case THTTPEvent::ERedirectedPermanently:
       
   237 			{
       
   238 			INFO_PRINTF1(_L("Permanent Redirection\n"));
       
   239 			} break;
       
   240 		case THTTPEvent::ERedirectedTemporarily:
       
   241 			{
       
   242 			INFO_PRINTF1(_L("Temporary Redirection\n"));
       
   243 			} break;
       
   244 		case THTTPEvent::ERedirectRequiresConfirmation:
       
   245  			{
       
   246 			// 301(Moved Permanently), 302(Found) or 307(Temporary Redirect) status is received 
       
   247 			// from a transaction and hence ERedirectRequiresConfirmation is sent by filter
       
   248 			// client has opted to close the transaction
       
   249 			INFO_PRINTF1(_L("Redirect requires confirmation\n"));
       
   250  			aTransaction.Close();
       
   251  			CActiveScheduler::Stop();
       
   252  			} break;
       
   253 		default:
       
   254 			{
       
   255 			INFO_PRINTF2(_L("<unrecognised event: %D>\n"), aEvent.iStatus);
       
   256 			// close off the transaction if it's an error
       
   257 			if (aEvent.iStatus < 0)
       
   258 				{
       
   259 				aTransaction.Close();
       
   260 				CActiveScheduler::Stop();
       
   261 				}
       
   262 			} break;
       
   263 		}
       
   264 	}
       
   265 
       
   266 
       
   267 /**
       
   268 This is the implementation of interface method MHttpDataOptimiser::MHFRunError
       
   269 @internalTechnology 
       
   270 @test
       
   271 @param		aError  Error which has been raised.	
       
   272 @param		aTransaction Transaction which is being processed currently
       
   273 @param      THTTPEvent Event which it is being handled currently.
       
   274 @return		None
       
   275 */
       
   276 TInt CHttpEboClientConditionalStep::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/)
       
   277 	{
       
   278 	INFO_PRINTF2(_L("MHFRunError fired with error code %D\n"), aError);
       
   279 	return KErrNone;
       
   280 	}
       
   281 
       
   282 
       
   283 /**
       
   284 It enocodes the request data.
       
   285 @internalTechnology 
       
   286 @test
       
   287 @param		aHttpData  Actual request.
       
   288 @param		aEncodedData Encoded request.
       
   289 @return		None
       
   290 */
       
   291 void CHttpEboClientConditionalStep::EncodeL (const TDesC8& aHttpData, HBufC8* &aEncodedData)
       
   292 	{
       
   293 	INFO_PRINTF1(_L("######## CHttpEboClientStep::EncodeL ########\n"));
       
   294 	aEncodedData =  aHttpData.AllocLC();
       
   295 	CleanupStack::Pop();
       
   296 	INFO_PRINTF1(_L("Encoded/Changed the request\n"));
       
   297 	}
       
   298 
       
   299 	
       
   300 /**
       
   301 It decodes the responsing data before parsing.
       
   302 @internalTechnology 
       
   303 @test
       
   304 @param		aHttpData  Actual response.
       
   305 @param		aEncodedData Decoded response.
       
   306 @return		None
       
   307 */
       
   308 void CHttpEboClientConditionalStep::DecodeL (const TDesC8& aData, HBufC8*& aHttpData, TBool& /*aTransFail*/)
       
   309 	{
       
   310 	INFO_PRINTF1(_L("######## CHttpEboClientStep::DecodeL ########\n"));
       
   311 	aHttpData = aData.AllocLC();
       
   312 	CleanupStack::Pop();	
       
   313 	INFO_PRINTF1(_L("Sent the actual response as a decoded response\n"));
       
   314 	}