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