applayerprotocols/httpservice/test/httpservicetest/ctesthttpclientstep.cpp
changeset 0 b16258d2340f
child 7 337070b4fa18
child 19 2f328ce1b263
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-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 "ctesthttpclientstep.h"
       
    17 #include "chttpservice.h"
       
    18 #include "ctestclienthttpget.h"
       
    19 #include "ctestclienthttppost.h"
       
    20 #include <escapeutils.h>
       
    21 #include <thttphdrval.h>
       
    22 #include "httptestutils.h"
       
    23 #include "clocaltestserver.h"
       
    24 #include "chttpclienttestparams.h"
       
    25 
       
    26 _LIT(KTestCaseName, "TestCaseName");
       
    27 _LIT8(KUserAgent, "HTTP Client API Test");
       
    28 _LIT(KTestHttpClientAPITestTitle, "HTTP client API Unit Test Harness");
       
    29 const TInt KMaxNoOfConnections = 6;
       
    30 const TInt KMaxTransToPipeline = 5;
       
    31 
       
    32 CTestHttpClientStep::CTestHttpClientStep()
       
    33     {
       
    34     
       
    35     } 
       
    36 
       
    37 CTestHttpClientStep::~CTestHttpClientStep()
       
    38     {
       
    39     iTestServer->StopServer();
       
    40     delete iHttpTrans;
       
    41     delete iHttpClient;
       
    42     delete iTestServer;
       
    43     delete iTestUtils;    
       
    44     delete iTestParamArray;
       
    45     delete iActiveScheduler;
       
    46     }
       
    47 
       
    48 // TEF virtuals
       
    49 TVerdict CTestHttpClientStep::doTestStepPreambleL()
       
    50     {
       
    51     iActiveScheduler = new (ELeave) CActiveScheduler();
       
    52     CActiveScheduler::Install(iActiveScheduler);
       
    53     iTestParamArray = new(ELeave) CHttpClientTestParamArray;
       
    54     iTestUtils = CHTTPTestUtils::NewL(KTestHttpClientAPITestTitle());
       
    55     iTestUtils->InitCommsL();
       
    56     
       
    57     iTestServer = CLocalTestServer::NewL(*iTestUtils, *this, *iTestParamArray);
       
    58     iHttpClient = CHttpService::NewL();
       
    59     THttpHeaderValueVariant variant(KUserAgent());
       
    60     if(iHttpClient->AddRequestHeader(HTTP::EUserAgent, variant))
       
    61         {
       
    62         SetTestStepResult(EFail);
       
    63         return TestStepResult();
       
    64         }
       
    65     if(iHttpClient->MaxConnections() != KMaxNoOfConnections)
       
    66         {
       
    67         SetTestStepResult(EFail);
       
    68         return TestStepResult();
       
    69         }
       
    70     if(iHttpClient->MaxTransactionsToPipeline() != KMaxTransToPipeline)
       
    71         {
       
    72         SetTestStepResult(EFail);
       
    73         return TestStepResult();
       
    74         }
       
    75 
       
    76     iTestServer->StartServerL();
       
    77     return TestStepResult();
       
    78     }
       
    79 
       
    80 // TEF pure virtual
       
    81 TVerdict CTestHttpClientStep::doTestStepL()
       
    82     {
       
    83     TPtrC testStepName;
       
    84     if(!GetStringFromConfig(ConfigSection(), KTestCaseName(), testStepName))
       
    85         {
       
    86         User::Leave(KErrCorrupt);
       
    87         }
       
    88     SetTestStepName(testStepName);
       
    89     _LIT8(KGetMethod, "GET");
       
    90     CHttpClientTestParams* param = CHttpClientTestParams::NewL(*this);
       
    91     iTestParamArray->AddTestParams(param);
       
    92     const TDesC8& proxyAddress = param->ProxyAddress();
       
    93     if(proxyAddress.Length() > 0)
       
    94         {
       
    95         User::LeaveIfError(iHttpClient->SetProxy(proxyAddress));
       
    96         if(iHttpClient->ProxyAddress().CompareF(proxyAddress) != 0)
       
    97             {
       
    98             User::Leave(KErrCorrupt);
       
    99             }
       
   100         }
       
   101     
       
   102     if(param->Method().CompareF(KGetMethod) == 0)
       
   103         {
       
   104         if(param->OnlineTest())
       
   105             {
       
   106             iHttpTrans = CTestClientHttpOnlineGet::NewL(*param, *this, *iHttpClient, param->Uri(), iTestUtils);
       
   107             }
       
   108         else
       
   109             {
       
   110             iHttpTrans = CTestClientHttpSimpleGet::NewL(*param, *this, *iHttpClient, param->Uri(), iTestUtils);
       
   111             }
       
   112         
       
   113         }
       
   114     else
       
   115         {
       
   116         iHttpTrans = CTestClientHttpSimplePost::NewL(*param, *this, *iHttpClient, param->Uri(), *iTestUtils);
       
   117         TInt len = param->RequestBody().Length();
       
   118         if(len > 0)
       
   119             {
       
   120             THttpHeaderValueVariant variant(len);
       
   121             iHttpTrans->AddRequestHeader(HTTP::EContentLength, variant);
       
   122             }
       
   123         }  
       
   124    
       
   125     if(param->NoRetryOnDisconnect())
       
   126         {
       
   127         iHttpTrans->SetNoRetry();
       
   128         }
       
   129     
       
   130     if(param->ResponseTimeoutEnable())
       
   131         {
       
   132         iHttpTrans->SetResponseTimeout(10);
       
   133         }
       
   134     
       
   135     const RPointerArray<CHeaderInfo>& info = param->RequestHeaderInfos();
       
   136     for(TInt i = 0; i < info.Count(); ++i)
       
   137         {
       
   138         CHeaderInfo* headerInfo = info[i];
       
   139         const TDesC8& headerName = *headerInfo->iFieldName;
       
   140         
       
   141         for(TInt j = 0; j < headerInfo->iFieldInfos.Count(); ++j)
       
   142             {
       
   143             CHeaderFieldInfo* headerFieldInfo = headerInfo->iFieldInfos[j];
       
   144             const TDesC8& headerFieldValue = *headerFieldInfo->iFieldValue;
       
   145             THttpHeaderValueVariant variant(headerFieldValue);
       
   146             if(headerFieldInfo->iFieldParamInfos.Count() == 0)
       
   147                 {                
       
   148                 // Set the header
       
   149                 iHttpTrans->AddRequestHeader(headerName, variant);
       
   150                 }
       
   151             else
       
   152                 {
       
   153                 for(TInt k = 0; k < headerFieldInfo->iFieldParamInfos.Count(); ++k)
       
   154                     {
       
   155                     CHeaderFieldParamInfo* paramInfo = headerFieldInfo->iFieldParamInfos[k];
       
   156                     const TDesC8& paramName = *paramInfo->iParamName;
       
   157                     const TDesC8& paramValue = *paramInfo->iParamValue;
       
   158                     TLex8 lex(paramValue);
       
   159                     TReal64 value = 0.;
       
   160                     lex.Val(value);             
       
   161                     THTTPHdrVal::TQConv q(value);
       
   162                     TInt val2 = q;
       
   163                     THttpHeaderValueVariant variant2(val2);
       
   164                     // Set the header with the param
       
   165                     iHttpTrans->AddRequestHeader(headerName, variant, paramName, variant2);
       
   166                     }
       
   167                 }
       
   168             }
       
   169         }
       
   170     
       
   171     iHttpTrans->SendRequest();
       
   172  
       
   173     CActiveScheduler::Start();
       
   174     return TestStepResult();    
       
   175     }
       
   176 
       
   177 void CTestHttpClientStep::EndTest(TInt aError)
       
   178     {
       
   179     if(aError != KErrNone)
       
   180         {
       
   181         SetTestStepResult(EFail);
       
   182         }
       
   183     // Failed with error.
       
   184     iHttpTrans->CancelRequest();
       
   185     CActiveScheduler::Stop();
       
   186     }