applayerprotocols/httptransportfw/Test/Acceptance/Iter1/Tc12.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 // Author: Franco.Bellu@symbian.com
       
    15 // 
       
    16 //
       
    17 
       
    18 
       
    19 #include "Tc12.h"
       
    20 #include <http.h>
       
    21 #include "testservers.h"
       
    22 
       
    23 #include "csrvaddrval.h"
       
    24 
       
    25 _LIT8(KWapTestUrl,			"http://WapTestIP");
       
    26 
       
    27 CHttpTestCase12::CHttpTestCase12(CScriptFile* aIniSettingsFile) : 
       
    28 	CHttpTestTransBase(),
       
    29 	iIniSettingsFile(aIniSettingsFile)
       
    30 	{
       
    31 	}
       
    32 
       
    33 void CHttpTestCase12::DoRunL()
       
    34 	{	
       
    35 	//open  a Session
       
    36 	iSession.OpenL();
       
    37 	iEngine->Utils().LogIt(_L("Session Created(TC(12)(Default parameters)"));
       
    38 
       
    39 	//Get the iSession'string pool handle;
       
    40 	iMyStrP = iSession.StringPool();
       
    41 	//First test transaction
       
    42 	//Prepare anything needed for a correct POST Request based on TestCase5 
       
    43 	//and submit it
       
    44 	OpenTestTransaction1L();
       
    45 	iTransaction.SubmitL();
       
    46 	CActiveScheduler::Start();
       
    47 	CloseTestTransaction1();
       
    48 
       
    49 	//Second test transaction
       
    50 	OpenTestTransaction2L();
       
    51 	iTransaction.SubmitL();
       
    52 	CActiveScheduler::Start();
       
    53 	CloseTestTransaction2();
       
    54 
       
    55 	//Third test transaction
       
    56 	OpenTestTransaction3L();
       
    57 	iTransaction.SubmitL();
       
    58 	CActiveScheduler::Start();
       
    59 	CloseTestTransaction3();
       
    60 
       
    61 	iSession.Close();
       
    62 	if (iTestFail==1)
       
    63 		{
       
    64 		User::Leave(-1);
       
    65 		}
       
    66 	iEngine->Utils().LogIt(_L("Session terminated"));
       
    67 	}
       
    68 	
       
    69 TInt CHttpTestCase12::RunError(TInt aErr)
       
    70 	{
       
    71 	iEngine->Utils().LogIt(_L("\nTest failed with error code %d\n"), aErr);
       
    72 	return KErrNone;
       
    73 	}
       
    74 
       
    75 void CHttpTestCase12::DoCancel()
       
    76 	{
       
    77 	}
       
    78 
       
    79 CHttpTestCase12::~CHttpTestCase12()
       
    80 	{
       
    81 	}	
       
    82 
       
    83 /** increment the value of iState and set active to create call to RunL
       
    84 	with new state value 
       
    85 */
       
    86 
       
    87 /** set this object active */
       
    88 void CHttpTestCase12::CompleteOwnRequest()
       
    89 	{
       
    90 	TRequestStatus* stat = &iStatus;
       
    91 	User::RequestComplete(stat,KErrNone);	
       
    92 	if (!IsActive())
       
    93 		SetActive();
       
    94 	}
       
    95 
       
    96 /** sets base test name.
       
    97 	@param void
       
    98 	@return string - the test name 
       
    99 */
       
   100 const TDesC& CHttpTestCase12::TestName()
       
   101 	{
       
   102 	_LIT(KHeaderTestName,"Test Case 12 ");
       
   103 	return KHeaderTestName;
       
   104 	}
       
   105 
       
   106 
       
   107 TInt CHttpTestCase12::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/,
       
   108 								  const THTTPEvent& /*aEvent*/)
       
   109 	{
       
   110 	iEngine->Utils().LogIt(_L("\nTest failed with error code %d\n"), aError);
       
   111 	return KErrNone;
       
   112 	}
       
   113 
       
   114 void CHttpTestCase12::MHFRunL(RHTTPTransaction aTransaction,
       
   115 							  const THTTPEvent& aEvent)
       
   116 	{
       
   117 	switch (aEvent.iStatus)
       
   118 		{
       
   119 		case THTTPEvent::EGotResponseHeaders:
       
   120 			{
       
   121 			// HTTP response headers have been received
       
   122 			iEngine->Utils().LogIt(_L("<EGotResponseHeaders>\n"));
       
   123 			TInt  statusCode = aTransaction.Response().StatusCode();
       
   124 			iEngine->Utils().LogIt(_L("Status Code: %d \n"),statusCode);
       
   125 			DumpRespHeaders(aTransaction);
       
   126 			iEngine->SetCurrentStatusCode(statusCode);
       
   127 			} break;
       
   128 		case THTTPEvent::EGotResponseBodyData:
       
   129 			{
       
   130 			// Some (more) body data has been received (in the HTTP response)
       
   131 			iEngine->Utils().LogIt(_L("<EGotResponseBodyData received>\n"));
       
   132 			DumpRespHeaders(aTransaction);
       
   133 			// for each chunk of data received we have to empty the buffer 
       
   134 			// before to be able to receive 
       
   135 			MHTTPDataSupplier* body = aTransaction.Response().Body();
       
   136 			body->ReleaseData();
       
   137 
       
   138 	//		DumpResponseBody(aTransaction);
       
   139 			} break;
       
   140 		case THTTPEvent::EResponseComplete:
       
   141 			{
       
   142 			// The transaction's response is complete
       
   143 			iEngine->Utils().LogIt(_L("<EResponseComplete received>\n"));
       
   144 			} break;
       
   145 		case THTTPEvent::ESucceeded:
       
   146 			{
       
   147 			iEngine->Utils().LogIt(_L("<ESucceeded Received from the VF>\n"));
       
   148 			CActiveScheduler::Stop();
       
   149 			} break;
       
   150 		case THTTPEvent::EFailed:
       
   151 			{
       
   152 			iEngine->Utils().LogIt(_L("<EFailed Received from the VF>\n"));
       
   153 			CActiveScheduler::Stop();
       
   154 			} break;
       
   155 		case KErrHttpRequestHasBody:
       
   156 			{
       
   157 			iEngine->Utils().LogIt(_L("<KErrHttpGetReqWithBody  received from the VF>"));
       
   158 			} break;
       
   159 		case KErrHttpEntityHeaderMissingContentType:
       
   160 			{
       
   161 			iEngine->Utils().LogIt(_L("<KErrHttpNoEntityHeaderWithBodyContentType  received from the VF>"));
       
   162 			} break;
       
   163 		case KErrHttpRequestBodyMissing:
       
   164 			{
       
   165 			iEngine->Utils().LogIt(_L("<KErrHttpPostReqWithoutBody  received from the VF>"));
       
   166 			} break;
       
   167 		default:
       
   168 			{
       
   169 			iEngine->Utils().LogIt(_L("<unrecognised event>\n %d"),aEvent.iStatus);
       
   170 			if (aEvent.iStatus < THTTPEvent::ERedirectedPermanently)
       
   171 				{
       
   172 				iTestFail=1;
       
   173 				iEngine->Utils().LogIt(_L("Test Failed: The event received was not expected and it's not just a warning\n"));
       
   174 				iEngine->PressAnyKey();
       
   175 				CActiveScheduler::Stop();
       
   176 				}
       
   177 			} 
       
   178 			break;
       
   179 		}
       
   180 	}
       
   181 
       
   182 void CHttpTestCase12::OpenTestTransaction2L()
       
   183 	{
       
   184 	// Literals used in the function
       
   185 	_LIT8(KUrlPath,				"/perl/dumpform.pl");
       
   186 
       
   187 	// Replace the host name in the URL
       
   188 	HBufC8* newUrl8 = TSrvAddrVal::ReplaceHostNameL(KWapTestUrl(), iIniSettingsFile);
       
   189 	CleanupStack::PushL(newUrl8);
       
   190 	TPtr8 newUrlPtr8 = newUrl8->Des();
       
   191 	newUrlPtr8.Append(KUrlPath());
       
   192 
       
   193 	TUriParser8 testURI;
       
   194 	testURI.Parse(newUrlPtr8);
       
   195 
       
   196 	//open string this testcase needs
       
   197 	RStringF textPlain = iMyStrP.StringF(HTTP::ETextPlain,RHTTPSession::GetTable());
       
   198 	RStringF textHtml = iMyStrP.StringF(HTTP::ETextHtml,RHTTPSession::GetTable());
       
   199 	RStringF mimeType = iMyStrP.StringF(HTTP::EApplicationXWwwFormUrlEncoded,RHTTPSession::GetTable());
       
   200 
       
   201 	//open a Transaction in iSession 
       
   202 	iTransaction = iSession.OpenTransactionL(testURI, *this,iMyStrP.StringF(HTTP::EPOST,RHTTPSession::GetTable()));
       
   203 	iEngine->Utils().LogIt(_L("Transaction (%d)Created in Session"),iTransaction.Id());
       
   204 
       
   205 	//Get a handle of the request in iTransaction
       
   206 	RHTTPRequest myRequest = iTransaction.Request();
       
   207 	RHTTPHeaders myHeaders = myRequest.GetHeaderCollection();
       
   208 	
       
   209 	//provide  some headers 
       
   210 	THTTPHdrVal v2(textPlain);
       
   211 	THTTPHdrVal v3(textHtml);
       
   212 	THTTPHdrVal v4(6);
       
   213 	THTTPHdrVal v5(mimeType);
       
   214 
       
   215 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EAccept,RHTTPSession::GetTable()),v2);
       
   216 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EAccept,RHTTPSession::GetTable()),v3);
       
   217 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EContentLength,RHTTPSession::GetTable()),v4);
       
   218 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EContentType,RHTTPSession::GetTable()), v5);
       
   219 
       
   220 	TSrvAddrVal::LogUsing8BitDesL(iEngine, newUrlPtr8);
       
   221 	iEngine->Utils().LogIt(_L("Method:Post"));
       
   222 	iEngine->Utils().LogIt(_L("Accept:test/plain,text/html"));
       
   223 	iEngine->Utils().LogIt(_L("Content-Type: application/x-www-form-urlencoded"));
       
   224 	iEngine->Utils().LogIt(_L("Content-Length: 6"));
       
   225 	CleanupStack::PopAndDestroy(newUrl8);
       
   226 	//NB: No body posted this  whill generate the EFailed message we want to test
       
   227 	}
       
   228 
       
   229 void CHttpTestCase12::CloseTestTransaction2()
       
   230 	{
       
   231 	//close Transaction and session
       
   232 	iTransaction.Close();
       
   233 	iEngine->Utils().LogIt(_L("Transaction terminated\n"));
       
   234 	}
       
   235 
       
   236 void CHttpTestCase12::OpenTestTransaction1L()
       
   237 	{
       
   238 	// Literals used in the function
       
   239 	_LIT8(KUrlPath,				"/");
       
   240 
       
   241 	// Replace the host name in the URL
       
   242 	HBufC8* newUrl8 = TSrvAddrVal::ReplaceHostNameL(KWapTestUrl(), iIniSettingsFile);
       
   243 	CleanupStack::PushL(newUrl8);
       
   244 	TPtr8 newUrlPtr8 = newUrl8->Des();
       
   245 	newUrlPtr8.Append(KUrlPath());
       
   246 
       
   247 	TUriParser8 testURI;
       
   248 	testURI.Parse(newUrlPtr8);
       
   249 
       
   250 	//get string handles this testcase needs
       
   251 	RStringF textPlain = iMyStrP.StringF(HTTP::ETextPlain,RHTTPSession::GetTable());
       
   252 	RStringF textHtml = iMyStrP.StringF(HTTP::ETextHtml,RHTTPSession::GetTable());
       
   253 
       
   254 	//open a Transaction in iSession 
       
   255 	iTransaction = iSession.OpenTransactionL(testURI, *this,iMyStrP.StringF(HTTP::EGET,RHTTPSession::GetTable()));
       
   256 	iEngine->Utils().LogIt(_L("Transaction (%d)Created in Session"),iTransaction.Id());
       
   257 
       
   258 	//Get a handle of the request in iTransaction
       
   259 	RHTTPRequest myRequest = iTransaction.Request();
       
   260 	RHTTPHeaders myHeaders = myRequest.GetHeaderCollection();
       
   261 	//provide  some headers 
       
   262 	THTTPHdrVal v2(textPlain);
       
   263 	THTTPHdrVal v3(textHtml);
       
   264 	THTTPHdrVal v4(6);
       
   265 
       
   266 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EAccept,RHTTPSession::GetTable()),v2);
       
   267 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EAccept,RHTTPSession::GetTable()),v3);
       
   268 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EContentLength,RHTTPSession::GetTable()),v4);
       
   269 
       
   270 	TSrvAddrVal::LogUsing8BitDesL(iEngine, newUrlPtr8);
       
   271 	iEngine->Utils().LogIt(_L("Method:GET"));
       
   272 	iEngine->Utils().LogIt(_L("Accept:test/plain,text/html"));
       
   273 	iEngine->Utils().LogIt(_L("Content-Length: 6"));
       
   274 	myRequest.SetBody(*this);
       
   275 	CleanupStack::PopAndDestroy(newUrl8);
       
   276 	}
       
   277 
       
   278 void CHttpTestCase12::CloseTestTransaction1()
       
   279 	{
       
   280 	//close Transaction and session
       
   281 	iTransaction.Close();
       
   282 	iEngine->Utils().LogIt(_L("Transaction terminated\n"));
       
   283 	}
       
   284 
       
   285 
       
   286 void CHttpTestCase12::OpenTestTransaction3L()
       
   287 	{
       
   288 	// Literals used in the function
       
   289 	_LIT8(KUrlPath,				"/perl/dumpform.pl");
       
   290 
       
   291 	// Replace the host name in the URL
       
   292 	HBufC8* newUrl8 = TSrvAddrVal::ReplaceHostNameL(KWapTestUrl(), iIniSettingsFile);
       
   293 	CleanupStack::PushL(newUrl8);
       
   294 	TPtr8 newUrlPtr8 = newUrl8->Des();
       
   295 	newUrlPtr8.Append(KUrlPath());
       
   296 
       
   297 	TUriParser8 testURI;
       
   298 	testURI.Parse(newUrlPtr8);
       
   299 
       
   300 	//get string handles this testcase needs
       
   301 	RStringF textPlain = iMyStrP.StringF(HTTP::ETextPlain,RHTTPSession::GetTable());
       
   302 	RStringF textHtml = iMyStrP.StringF(HTTP::ETextHtml,RHTTPSession::GetTable());
       
   303 	RStringF mimeType = iMyStrP.StringF(HTTP::EApplicationXWwwFormUrlEncoded,RHTTPSession::GetTable());
       
   304 
       
   305 	//open a Transaction in iSession 
       
   306 	iTransaction = iSession.OpenTransactionL(testURI, *this,iMyStrP.StringF(HTTP::EPOST,RHTTPSession::GetTable()));
       
   307 	iEngine->Utils().LogIt(_L("Transaction (%d)Created in Session"),iTransaction.Id());
       
   308 
       
   309 	//Get a handle of the request in iTransaction
       
   310 	RHTTPRequest myRequest = iTransaction.Request();
       
   311 	RHTTPHeaders myHeaders = myRequest.GetHeaderCollection();
       
   312 	//provide  some headers 
       
   313 	THTTPHdrVal v2(textPlain);
       
   314 	THTTPHdrVal v3(textHtml);
       
   315 	THTTPHdrVal v5(mimeType);
       
   316 	
       
   317 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EAccept,RHTTPSession::GetTable()),v2);
       
   318 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EAccept,RHTTPSession::GetTable()),v3);
       
   319 	//No Content-Length mandatory entity header for POST: this generates the 
       
   320 	//failure;
       
   321 	myHeaders.SetFieldL(iMyStrP.StringF(HTTP::EContentType,RHTTPSession::GetTable()), v5);
       
   322 
       
   323 	TSrvAddrVal::LogUsing8BitDesL(iEngine, newUrlPtr8);
       
   324 	iEngine->Utils().LogIt(_L("Method:GET"));
       
   325 	iEngine->Utils().LogIt(_L("Accept:test/plain,text/html"));
       
   326 	iEngine->Utils().LogIt(_L("Content-Type : application/x-www-form-urlencoded"));
       
   327 	myRequest.SetBody(*this);
       
   328 	CleanupStack::PopAndDestroy(newUrl8);
       
   329 	}
       
   330 
       
   331 void CHttpTestCase12::CloseTestTransaction3()
       
   332 	{
       
   333 	//close Transaction and session
       
   334 	iTransaction.Close();
       
   335 	iEngine->Utils().LogIt(_L("Transaction terminated\n"));
       
   336 	}