Home | History | Annotate | Download | only in prerender
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/prerender/prerender_util.h"
      6 
      7 #include "base/metrics/histogram.h"
      8 #include "base/metrics/histogram_samples.h"
      9 #include "base/metrics/statistics_recorder.h"
     10 #include "net/http/http_response_headers.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 #include "url/gurl.h"
     13 
     14 namespace prerender {
     15 
     16 class PrerenderUtilTest : public testing::Test {
     17  public:
     18   PrerenderUtilTest() {
     19   }
     20 };
     21 
     22 // Ensure that extracting a urlencoded URL in the url= query string component
     23 // works.
     24 TEST_F(PrerenderUtilTest, ExtractURLInQueryStringTest) {
     25   GURL result;
     26   EXPECT_TRUE(MaybeGetQueryStringBasedAliasURL(
     27       GURL("http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBcQFjAA&url=http%3A%2F%2Fwww.abercrombie.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FStoreLocator%3FcatalogId%3D%26storeId%3D10051%26langId%3D-1&rct=j&q=allinurl%3A%26&ei=KLyUTYGSEdTWiAKUmLCdCQ&usg=AFQjCNF8nJ2MpBFfr1ijO39_f22bcKyccw&sig2=2ymyGpO0unJwU1d4kdCUjQ"),
     28       &result));
     29   ASSERT_EQ(GURL("http://www.abercrombie.com/webapp/wcs/stores/servlet/StoreLocator?catalogId=&storeId=10051&langId=-1").spec(), result.spec());
     30   EXPECT_FALSE(MaybeGetQueryStringBasedAliasURL(
     31       GURL("http://www.google.com/url?sadf=test&blah=blahblahblah"), &result));
     32   EXPECT_FALSE(MaybeGetQueryStringBasedAliasURL(
     33       GURL("http://www.google.com/?url=INVALIDurlsAREsoMUCHfun.com"), &result));
     34   EXPECT_TRUE(MaybeGetQueryStringBasedAliasURL(
     35       GURL("http://www.google.com/?url=http://validURLSareGREAT.com"),
     36       &result));
     37   ASSERT_EQ(GURL("http://validURLSareGREAT.com").spec(), result.spec());
     38 }
     39 
     40 // Ensure that extracting an experiment in the lpe= query string component
     41 // works.
     42 TEST_F(PrerenderUtilTest, ExtractExperimentInQueryStringTest) {
     43   EXPECT_EQ(GetQueryStringBasedExperiment(
     44       GURL("http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBcQFjAA&url=http%3A%2F%2Fwww.abercrombie.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FStoreLocator%3FcatalogId%3D%26storeId%3D10051%26langId%3D-1&rct=j&q=allinurl%3A%26&ei=KLyUTYGSEdTWiAKUmLCdCQ&usg=AFQjCNF8nJ2MpBFfr1ijO39_f22bcKyccw&sig2=2ymyGpO0unJwU1d4kdCUjQ&lpe=4&asdf=test")), 4);
     45   EXPECT_EQ(GetQueryStringBasedExperiment(
     46       GURL("http://www.google.com/test.php?a=b")), kNoExperiment);
     47   EXPECT_EQ(GetQueryStringBasedExperiment(
     48       GURL("http://www.google.com/test.php?lpe=5")), 5);
     49   EXPECT_EQ(GetQueryStringBasedExperiment(
     50       GURL("http://www.google.com/test.php?lpe=50")), kNoExperiment);
     51   EXPECT_EQ(GetQueryStringBasedExperiment(
     52       GURL("http://www.google.com/test.php?lpe=0")), kNoExperiment);
     53   EXPECT_EQ(GetQueryStringBasedExperiment(
     54       GURL("http://www.google.com/test.php?lpe=10")), kNoExperiment);
     55 }
     56 
     57 // Ensure that we detect Google search result URLs correctly.
     58 TEST_F(PrerenderUtilTest, DetectGoogleSearchREsultURLTest) {
     59   EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/#asdf")));
     60   EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/")));
     61   EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/?a=b")));
     62   EXPECT_TRUE(IsGoogleSearchResultURL(
     63       GURL("http://www.google.com/search?q=hi")));
     64   EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/search")));
     65   EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/webhp")));
     66   EXPECT_TRUE(IsGoogleSearchResultURL(
     67       GURL("http://www.google.com/webhp?a=b#123")));
     68   EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://www.google.com/imgres")));
     69   EXPECT_FALSE(IsGoogleSearchResultURL(
     70       GURL("http://www.google.com/imgres?q=hi")));
     71   EXPECT_FALSE(IsGoogleSearchResultURL(
     72       GURL("http://www.google.com/imgres?q=hi#123")));
     73   EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://google.com/search")));
     74   EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://WWW.GooGLE.CoM/search")));
     75   EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://WWW.GooGLE.CoM/SeArcH")));
     76   EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.co.uk/search")));
     77   EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://google.co.uk/search")));
     78   EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://www.chromium.org/search")));
     79 }
     80 
     81 // Ensure that we count PageSpeed headers correctly.
     82 TEST_F(PrerenderUtilTest, CountPageSpeedHeadersTest) {
     83   base::StatisticsRecorder::Initialize();
     84   GURL url("http://google.com");
     85   std::string temp("HTTP/1.1 200 OK\n\n");
     86   std::replace(temp.begin(), temp.end(), '\n', '\0');
     87   scoped_refptr<net::HttpResponseHeaders> headers(
     88       new net::HttpResponseHeaders(temp));
     89 
     90   int num_responses = 0;
     91   int num_mps = 0;
     92   int num_ngx = 0;
     93   int num_pss = 0;
     94   int num_other = 0;
     95   int num_bucket_1  = 0;  // unrecognized format/value bucket
     96   int num_bucket_30 = 0;  // 1.2.24.1 bucket
     97   int num_bucket_33 = 0;  // 1.3.25.2 bucket
     98 
     99   scoped_ptr<base::HistogramSamples> server_samples;
    100   scoped_ptr<base::HistogramSamples> version_samples;
    101 
    102   // No PageSpeed header. The VersionCounts histogram isn't created yet.
    103   GatherPagespeedData(ResourceType::MAIN_FRAME, url, headers.get());
    104   base::HistogramBase* server_histogram =
    105       base::StatisticsRecorder::FindHistogram(
    106           "Prerender.PagespeedHeader.ServerCounts");
    107   ASSERT_TRUE(server_histogram != NULL);
    108   ASSERT_TRUE(NULL == base::StatisticsRecorder::FindHistogram(
    109       "Prerender.PagespeedHeader.VersionCounts"));
    110 
    111   server_samples = server_histogram->SnapshotSamples();
    112   EXPECT_EQ(++num_responses, server_samples->GetCount(0));
    113   EXPECT_EQ(  num_mps,       server_samples->GetCount(1));
    114   EXPECT_EQ(  num_ngx,       server_samples->GetCount(2));
    115   EXPECT_EQ(  num_pss,       server_samples->GetCount(3));
    116   EXPECT_EQ(  num_other,     server_samples->GetCount(4));
    117 
    118   // X-Mod-Pagespeed header in expected format. VersionCounts now exists.
    119   headers->AddHeader("X-Mod-Pagespeed: 1.2.24.1-2300");
    120   GatherPagespeedData(ResourceType::MAIN_FRAME, url, headers.get());
    121   base::HistogramBase* version_histogram =
    122       base::StatisticsRecorder::FindHistogram(
    123           "Prerender.PagespeedHeader.VersionCounts");
    124   ASSERT_TRUE(version_histogram != NULL);
    125   server_samples = server_histogram->SnapshotSamples();
    126   version_samples = version_histogram->SnapshotSamples();
    127   EXPECT_EQ(++num_responses, server_samples->GetCount(0));
    128   EXPECT_EQ(++num_mps,       server_samples->GetCount(1));
    129   EXPECT_EQ(  num_ngx,       server_samples->GetCount(2));
    130   EXPECT_EQ(  num_pss,       server_samples->GetCount(3));
    131   EXPECT_EQ(  num_other,     server_samples->GetCount(4));
    132   EXPECT_EQ(  num_bucket_1,  version_samples->GetCount(1));
    133   EXPECT_EQ(++num_bucket_30, version_samples->GetCount(30));  // +1 for #30
    134   EXPECT_EQ(  num_bucket_33, version_samples->GetCount(33));
    135   headers->RemoveHeader("X-Mod-Pagespeed");
    136 
    137   // X-Mod-Pagespeed header in unexpected format.
    138   headers->AddHeader("X-Mod-Pagespeed: Powered By PageSpeed!");
    139   GatherPagespeedData(ResourceType::MAIN_FRAME, url, headers.get());
    140   server_samples = server_histogram->SnapshotSamples();
    141   version_samples = version_histogram->SnapshotSamples();
    142   EXPECT_EQ(++num_responses, server_samples->GetCount(0));
    143   EXPECT_EQ(++num_mps,       server_samples->GetCount(1));
    144   EXPECT_EQ(  num_ngx,       server_samples->GetCount(2));
    145   EXPECT_EQ(  num_pss,       server_samples->GetCount(3));
    146   EXPECT_EQ(  num_other,     server_samples->GetCount(4));
    147   EXPECT_EQ(++num_bucket_1,  version_samples->GetCount(1));   // +1 for 'huh?'
    148   EXPECT_EQ(  num_bucket_30, version_samples->GetCount(30));
    149   EXPECT_EQ(  num_bucket_33, version_samples->GetCount(33));
    150   headers->RemoveHeader("X-Mod-Pagespeed");
    151 
    152   // X-Page-Speed header in mod_pagespeed format (so ngx_pagespeed).
    153   headers->AddHeader("X-Page-Speed: 1.3.25.2-2530");
    154   GatherPagespeedData(ResourceType::MAIN_FRAME, url, headers.get());
    155   server_samples = server_histogram->SnapshotSamples();
    156   version_samples = version_histogram->SnapshotSamples();
    157   EXPECT_EQ(++num_responses, server_samples->GetCount(0));
    158   EXPECT_EQ(  num_mps,       server_samples->GetCount(1));
    159   EXPECT_EQ(++num_ngx,       server_samples->GetCount(2));
    160   EXPECT_EQ(  num_pss,       server_samples->GetCount(3));
    161   EXPECT_EQ(  num_other,     server_samples->GetCount(4));
    162   EXPECT_EQ(  num_bucket_1,  version_samples->GetCount(1));
    163   EXPECT_EQ(  num_bucket_30, version_samples->GetCount(30));
    164   EXPECT_EQ(++num_bucket_33, version_samples->GetCount(33));  // +1 for #33
    165   headers->RemoveHeader("X-Page-Speed");
    166 
    167   // X-Page-Speed header in PageSpeed Service format.
    168   headers->AddHeader("X-Page-Speed: 97_4_bo");
    169   GatherPagespeedData(ResourceType::MAIN_FRAME, url, headers.get());
    170   server_samples = server_histogram->SnapshotSamples();
    171   version_samples = version_histogram->SnapshotSamples();
    172   EXPECT_EQ(++num_responses, server_samples->GetCount(0));
    173   EXPECT_EQ(  num_mps,       server_samples->GetCount(1));    // no change
    174   EXPECT_EQ(  num_ngx,       server_samples->GetCount(2));
    175   EXPECT_EQ(++num_pss,       server_samples->GetCount(3));    // +1 for PSS
    176   EXPECT_EQ(  num_other,     server_samples->GetCount(4));
    177   EXPECT_EQ(  num_bucket_1,  version_samples->GetCount(1));
    178   EXPECT_EQ(  num_bucket_30, version_samples->GetCount(30));
    179   EXPECT_EQ(  num_bucket_33, version_samples->GetCount(33));
    180   headers->RemoveHeader("X-Page-Speed");
    181 
    182   // X-Page-Speed header in an unrecognized format (IISpeed in this case).
    183   headers->AddHeader("X-Page-Speed: 1.0PS1.2-20130615");
    184   GatherPagespeedData(ResourceType::MAIN_FRAME, url, headers.get());
    185   server_samples = server_histogram->SnapshotSamples();
    186   version_samples = version_histogram->SnapshotSamples();
    187   EXPECT_EQ(++num_responses, server_samples->GetCount(0));
    188   EXPECT_EQ(  num_mps,       server_samples->GetCount(1));    // no change
    189   EXPECT_EQ(  num_pss,       server_samples->GetCount(3));
    190   EXPECT_EQ(++num_other,     server_samples->GetCount(4));    // +1 for 'other'
    191   EXPECT_EQ(  num_bucket_1,  version_samples->GetCount(1));
    192   EXPECT_EQ(  num_bucket_30, version_samples->GetCount(30));
    193   EXPECT_EQ(  num_bucket_33, version_samples->GetCount(33));
    194 
    195   // Not a main frame => not counted at all.
    196   GatherPagespeedData(ResourceType::SUB_FRAME, url, headers.get());
    197   server_samples = server_histogram->SnapshotSamples();
    198   version_samples = version_histogram->SnapshotSamples();
    199   EXPECT_EQ(  num_responses, server_samples->GetCount(0));
    200   EXPECT_EQ(  num_mps,       server_samples->GetCount(1));
    201   EXPECT_EQ(  num_ngx,       server_samples->GetCount(2));
    202   EXPECT_EQ(  num_pss,       server_samples->GetCount(3));
    203   EXPECT_EQ(  num_other,     server_samples->GetCount(4));
    204   EXPECT_EQ(  num_bucket_1,  version_samples->GetCount(1));
    205   EXPECT_EQ(  num_bucket_30, version_samples->GetCount(30));
    206   EXPECT_EQ(  num_bucket_33, version_samples->GetCount(33));
    207 
    208   // Not a http/https URL => not counted at all.
    209   GURL data_url("data:image/png;base64,yadda yadda==");
    210   GatherPagespeedData(ResourceType::MAIN_FRAME, data_url, headers.get());
    211   server_samples = server_histogram->SnapshotSamples();
    212   version_samples = version_histogram->SnapshotSamples();
    213   EXPECT_EQ(  num_responses, server_samples->GetCount(0));
    214   EXPECT_EQ(  num_mps,       server_samples->GetCount(1));
    215   EXPECT_EQ(  num_ngx,       server_samples->GetCount(2));
    216   EXPECT_EQ(  num_pss,       server_samples->GetCount(3));
    217   EXPECT_EQ(  num_other,     server_samples->GetCount(4));
    218   EXPECT_EQ(  num_bucket_1,  version_samples->GetCount(1));
    219   EXPECT_EQ(  num_bucket_30, version_samples->GetCount(30));
    220   EXPECT_EQ(  num_bucket_33, version_samples->GetCount(33));
    221 
    222   headers->RemoveHeader("X-Page-Speed");
    223 }
    224 
    225 }  // namespace prerender
    226