applayerprotocols/httpservice/test/httpservicetest/ctestclienthttpget.cpp
changeset 0 b16258d2340f
child 18 f21293830889
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 "ctestclienthttpget.h"
       
    17 #include <httpstringconstants.h>
       
    18 #include "mlocaltestserverobserver.h"
       
    19 #include "chttpclienttestparams.h"
       
    20 #include <tinternetdate.h>
       
    21 _LIT8(KAcceptHeader, "Accept");
       
    22 _LIT8(KAcceptValue, "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
       
    23 _LIT(KTestHttpClientAPITestTitle, "HTTP client API Unit Test Harness");
       
    24 _LIT(KTransferProgress, "File size: %d kb , Downloaded so far: %d kb");
       
    25 
       
    26 CTestClientHttpSimpleGet* CTestClientHttpSimpleGet::NewL(CHttpClientTestParams& aParams, MLocalTestServerObserver& aObserver, CHttpService& aClient, const TDesC8& aUri, CHTTPTestUtils* aUtils)
       
    27     {
       
    28     CTestClientHttpSimpleGet* self = new(ELeave) CTestClientHttpSimpleGet(aParams, aObserver, aUtils);
       
    29     CleanupStack::PushL(self);
       
    30     self->ConstructL(aClient, aUri);
       
    31     CleanupStack::Pop(); // self
       
    32     return self;
       
    33     }  
       
    34 
       
    35 CTestClientHttpSimpleGet::~CTestClientHttpSimpleGet()
       
    36     {
       
    37     }
       
    38 
       
    39 CTestClientHttpSimpleGet::CTestClientHttpSimpleGet(CHttpClientTestParams& aParams, MLocalTestServerObserver& aObserver, CHTTPTestUtils* aUtils)
       
    40 : iObserver(aObserver),
       
    41 iParams(aParams),
       
    42 iTestUtils(*aUtils)
       
    43     {
       
    44     
       
    45     }
       
    46 
       
    47 void CTestClientHttpSimpleGet::ConstructL(CHttpService& aClient, const TDesC8& aUri)
       
    48     {
       
    49     TPtrC8 method;
       
    50     aClient.String(HTTP::EGET, method);
       
    51     CHttpClientTransaction::Create(aClient, method, aUri);   
       
    52     }
       
    53 
       
    54 // From CHTTPClientTransaction
       
    55 void CTestClientHttpSimpleGet::OnResponseHeaders()
       
    56     {
       
    57     // Check whether the status code matches
       
    58     if(iParams.StatusCode() != StatusCode())
       
    59         {
       
    60         iObserver.EndTest(KErrCorrupt);
       
    61         }
       
    62     
       
    63     // IF we are receiving a file. just sink the content to the file.
       
    64     // We will not check 
       
    65     if(iParams.IsFileSending())
       
    66         {
       
    67         CHttpClientTransaction::SetContentSink(iParams.SinkFile());
       
    68         return;
       
    69         }
       
    70     else
       
    71         {
       
    72         CHttpClientTransaction::SetContentSink(*this);
       
    73         }    
       
    74     TInt i = 0;
       
    75     const RPointerArray<CHeaderInfo>& hdrInfos = iParams.HeaderInfos();
       
    76     
       
    77     THttpHeaderIterator iter = ResponseHeaders();
       
    78     while(!iter.AtEnd())
       
    79         {
       
    80         CHeaderInfo* info = iParams.FindHeaderInfo(iter.Name());
       
    81         if(info == NULL)
       
    82             {
       
    83             iObserver.EndTest(KErrCorrupt);
       
    84             return;
       
    85             }
       
    86         
       
    87         TInt j = 0;
       
    88         THttpHeaderElementIterator elementIter = iter.Elements();
       
    89         while(!elementIter.AtEnd())
       
    90             {
       
    91             CHeaderFieldInfo* fieldInfo = info->iFieldInfos[j];
       
    92             THttpHeaderValueVariant value = elementIter.Value();
       
    93             if(!iParams.CheckVariantValue(value, *fieldInfo->iFieldValue))
       
    94                 {
       
    95                 iObserver.EndTest(KErrCorrupt);
       
    96                 return;
       
    97                 }
       
    98             
       
    99             // Check the params
       
   100             THttpHeaderParamIterator paramIter = elementIter.Parameter();
       
   101             TInt k = 0;
       
   102             while(!paramIter.AtEnd())
       
   103                 {
       
   104                 CHeaderFieldParamInfo* paramInfo = fieldInfo->iFieldParamInfos[k];
       
   105                 // Check the param name and value
       
   106                 if(paramInfo->iParamName->CompareF(paramIter.Name()) != 0)
       
   107                     {
       
   108                     iObserver.EndTest(KErrCorrupt);
       
   109                     return;
       
   110                     }
       
   111                 THttpHeaderValueVariant value = paramIter.Value();
       
   112                 if(!iParams.CheckVariantValue(value, *paramInfo->iParamValue) != 0)
       
   113                     {
       
   114                     iObserver.EndTest(KErrCorrupt);
       
   115                     return;
       
   116                     }                
       
   117                 ++k;
       
   118                 ++paramIter;                
       
   119                 }
       
   120             ++j;
       
   121             ++elementIter;
       
   122             }
       
   123         TInt x = 0;
       
   124         THttpHeaderParamIterator hdrParamIter = iter.Parameters();
       
   125         while(!hdrParamIter.AtEnd())
       
   126             {
       
   127             CHeaderFieldParamInfo* paramInfo = info->iParamInfos[x];
       
   128             if(paramInfo->iParamName->CompareF(hdrParamIter.Name()) != 0)
       
   129                 {
       
   130                 iObserver.EndTest(KErrCorrupt);
       
   131                 return;
       
   132                 }
       
   133             THttpHeaderValueVariant value = hdrParamIter.Value();
       
   134             if(!iParams.CheckVariantValue(value, *paramInfo->iParamValue) != 0)
       
   135                 {
       
   136                 iObserver.EndTest(KErrCorrupt);
       
   137                 return;
       
   138                 }                
       
   139             ++x;
       
   140             ++hdrParamIter;
       
   141             }
       
   142         ++iter;
       
   143         ++i;
       
   144         }
       
   145     }
       
   146 
       
   147 void CTestClientHttpSimpleGet::OnCompletion()
       
   148     {
       
   149     if(iParams.IsFileSending())
       
   150         {
       
   151         iParams.SinkFile().Close(); // Close the file
       
   152         TBool ret = ETrue;
       
   153         TRAPD(err, ret = iParams.MatchFileContent());
       
   154         if(err == KErrNone && ret)
       
   155             iObserver.EndTest(KErrNone);
       
   156         else
       
   157             iObserver.EndTest(KErrCorrupt);
       
   158         }
       
   159     else
       
   160         {
       
   161         iObserver.EndTest(KErrNone);
       
   162         }
       
   163     }
       
   164 
       
   165 void CTestClientHttpSimpleGet::OnError(TInt aError)
       
   166     {
       
   167     if(aError == iParams.ExpectedError())
       
   168         iObserver.EndTest(KErrNone);
       
   169     else
       
   170         iObserver.EndTest(aError);    
       
   171     }
       
   172 
       
   173 void CTestClientHttpSimpleGet::OnTransferProgress(TInt aTotal, TInt aTransffered)
       
   174     {
       
   175     if(iParams.TransferProgress())
       
   176         {
       
   177         iTestUtils.LogIt(KTransferProgress(),aTotal,aTransffered);
       
   178         }    
       
   179     }
       
   180 
       
   181 TBool CTestClientHttpSimpleGet::OnRedirection()
       
   182     {
       
   183     if(iParams.IsRedirecting())
       
   184         return ETrue;
       
   185     else
       
   186         return EFalse;
       
   187     }
       
   188 
       
   189 void CTestClientHttpSimpleGet::OnData(THttpContentSinkOp& aData)
       
   190     {
       
   191     if(iParams.ResponseDynamicAndStatic())
       
   192         {
       
   193         aData.Store();
       
   194         }
       
   195     else
       
   196         {
       
   197         TPtrC8 ptr;
       
   198         aData.GetData(ptr);
       
   199         if(ptr.CompareF(iParams.ResponseBody()) != 0)
       
   200             {
       
   201             iObserver.EndTest(KErrCorrupt);
       
   202             return;
       
   203             }
       
   204         aData.Release();
       
   205         }
       
   206     }
       
   207 
       
   208 
       
   209 CTestClientHttpOnlineGet* CTestClientHttpOnlineGet::NewL(CHttpClientTestParams& aParams, MLocalTestServerObserver& aObserver, CHttpService& aClient, const TDesC8& aUri, CHTTPTestUtils* aUtils)
       
   210     {
       
   211     CTestClientHttpOnlineGet* self = new(ELeave) CTestClientHttpOnlineGet(aParams, aObserver, aUtils);
       
   212      CleanupStack::PushL(self);
       
   213      self->ConstructL(aClient, aUri);
       
   214      CleanupStack::Pop(); // self
       
   215      return self;    
       
   216     }
       
   217 
       
   218 CTestClientHttpOnlineGet::CTestClientHttpOnlineGet(CHttpClientTestParams& aParams, MLocalTestServerObserver& aObserver, CHTTPTestUtils* aUtils)
       
   219 : CTestClientHttpSimpleGet(aParams, aObserver, aUtils)
       
   220     {
       
   221     
       
   222     }
       
   223 
       
   224 void CTestClientHttpOnlineGet::ConstructL(CHttpService& aClient, const TDesC8& aUri)
       
   225     {
       
   226     CTestClientHttpSimpleGet::ConstructL(aClient, aUri);
       
   227     _LIT8(KLocalHost, "127.0.0.1");
       
   228     if(aUri.FindF(KLocalHost()) == KErrNotFound)
       
   229         {
       
   230         CHttpClientTransaction::AddCustomRequestHeader(KAcceptHeader(), KAcceptValue());
       
   231         THttpHeaderValueVariant variant;    
       
   232         variant.SetInt(300);
       
   233         CHttpClientTransaction::AddRequestHeader(HTTP::EKeepAlive, variant);
       
   234         _LIT8(KEnUs, "en-us");
       
   235         variant.SetStr(KEnUs);
       
   236         CHttpClientTransaction::AddRequestHeader(HTTP::EAcceptLanguage, variant);
       
   237         _LIT8(KEn, "en");
       
   238         _LIT8(KQVal, "0.9");
       
   239         
       
   240         //The following functions SetDateTime() and DateTime() have been used to avoid the code coverage issues.
       
   241         TDateTime dateTime(2009, EDecember, 0, 0, 0, 0, 0);
       
   242         variant.SetDateTime(dateTime);
       
   243         
       
   244         TInternetDate internetDate(variant.DateTime());
       
   245     
       
   246         TLex8 lex(KQVal);
       
   247         TReal64 value = 0.;
       
   248         lex.Val(value);             
       
   249         THTTPHdrVal::TQConv q(value);
       
   250         TInt val2 = q;
       
   251         THttpHeaderValueVariant variant2(val2);
       
   252         variant.SetStr(KEn);
       
   253         CHttpClientTransaction::AddRequestHeader(HTTP::EAcceptLanguage, variant, HTTP::EQ, variant2);
       
   254         
       
   255         //The following two temporary objects have been created to avoid the code coverage issues.
       
   256         THttpContentSinkOp httpContentSinkOp;
       
   257         THttpContentSourceOp httpContentSourceOp;
       
   258         // Disable the pipelining
       
   259         DisablePipelining();
       
   260         }
       
   261     else
       
   262         {
       
   263         _LIT8(KAcceptAll, "*/*");
       
   264         CHttpClientTransaction::AddCustomRequestHeader(KAcceptHeader(), KAcceptAll());        
       
   265         }
       
   266     }
       
   267 
       
   268 CTestClientHttpOnlineGet::~CTestClientHttpOnlineGet()
       
   269     {
       
   270     
       
   271     }
       
   272 
       
   273 void CTestClientHttpOnlineGet::OnResponseHeaders()
       
   274     {
       
   275     // Check whether the status code matches
       
   276      if(iParams.StatusCode() != StatusCode())
       
   277          {
       
   278          iObserver.EndTest(KErrCorrupt);
       
   279          return;
       
   280          }
       
   281      _LIT8(KStatusText, "Ok");
       
   282      const TDesC8& statusText = StatusText();
       
   283      
       
   284      if(statusText.CompareF(KStatusText) != 0)
       
   285          {
       
   286          // we do not fail here. But comparison is failed. print that.
       
   287          }
       
   288      
       
   289      // Iterate the response headers
       
   290      THttpHeaderIterator iter;
       
   291      iter = ResponseHeaders();
       
   292      while(!iter.AtEnd())
       
   293          {
       
   294          iter++;
       
   295          }
       
   296     
       
   297      THttpHeaderElementIterator it;
       
   298      it = ResponseHeader(HTTP::ECacheControl);
       
   299      while(!it.AtEnd())
       
   300          {
       
   301          it++;
       
   302          }
       
   303      _LIT8(KContentType, "Content-Type");
       
   304      it = ResponseHeader(KContentType());
       
   305      while(!it.AtEnd())
       
   306          {
       
   307          THttpHeaderParamIterator it2;
       
   308          it2 = it.Parameter();
       
   309          while(!it2.AtEnd())
       
   310              {
       
   311              it2++;
       
   312              }
       
   313          it++;
       
   314          }     
       
   315      CHttpClientTransaction::SetContentSink(*this);
       
   316     }
       
   317 
       
   318 void CTestClientHttpOnlineGet::OnCompletion()
       
   319     {
       
   320     iObserver.EndTest(KErrNone);
       
   321     }
       
   322  
       
   323 void CTestClientHttpOnlineGet::OnData(THttpContentSinkOp& aData)
       
   324     {
       
   325     // Just release the data
       
   326     aData.Release();
       
   327     }