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.
5 #include "chrome/browser/prerender/prerender_util.h"
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"
14 using content::ResourceType
;
18 class PrerenderUtilTest
: public testing::Test
{
24 // Ensure that extracting a urlencoded URL in the url= query string component
26 TEST_F(PrerenderUtilTest
, ExtractURLInQueryStringTest
) {
28 EXPECT_TRUE(MaybeGetQueryStringBasedAliasURL(
29 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"),
31 ASSERT_EQ(GURL("http://www.abercrombie.com/webapp/wcs/stores/servlet/StoreLocator?catalogId=&storeId=10051&langId=-1").spec(), result
.spec());
32 EXPECT_FALSE(MaybeGetQueryStringBasedAliasURL(
33 GURL("http://www.google.com/url?sadf=test&blah=blahblahblah"), &result
));
34 EXPECT_FALSE(MaybeGetQueryStringBasedAliasURL(
35 GURL("http://www.google.com/?url=INVALIDurlsAREsoMUCHfun.com"), &result
));
36 EXPECT_TRUE(MaybeGetQueryStringBasedAliasURL(
37 GURL("http://www.google.com/?url=http://validURLSareGREAT.com"),
39 ASSERT_EQ(GURL("http://validURLSareGREAT.com").spec(), result
.spec());
42 // Ensure that extracting an experiment in the lpe= query string component
44 TEST_F(PrerenderUtilTest
, ExtractExperimentInQueryStringTest
) {
45 EXPECT_EQ(GetQueryStringBasedExperiment(
46 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);
47 EXPECT_EQ(GetQueryStringBasedExperiment(
48 GURL("http://www.google.com/test.php?a=b")), kNoExperiment
);
49 EXPECT_EQ(GetQueryStringBasedExperiment(
50 GURL("http://www.google.com/test.php?lpe=5")), 5);
51 EXPECT_EQ(GetQueryStringBasedExperiment(
52 GURL("http://www.google.com/test.php?lpe=50")), kNoExperiment
);
53 EXPECT_EQ(GetQueryStringBasedExperiment(
54 GURL("http://www.google.com/test.php?lpe=0")), kNoExperiment
);
55 EXPECT_EQ(GetQueryStringBasedExperiment(
56 GURL("http://www.google.com/test.php?lpe=10")), kNoExperiment
);
59 // Ensure that we detect Google search result URLs correctly.
60 TEST_F(PrerenderUtilTest
, DetectGoogleSearchREsultURLTest
) {
61 EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/#asdf")));
62 EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/")));
63 EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/?a=b")));
64 EXPECT_TRUE(IsGoogleSearchResultURL(
65 GURL("http://www.google.com/search?q=hi")));
66 EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/search")));
67 EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.com/webhp")));
68 EXPECT_TRUE(IsGoogleSearchResultURL(
69 GURL("http://www.google.com/webhp?a=b#123")));
70 EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://www.google.com/imgres")));
71 EXPECT_FALSE(IsGoogleSearchResultURL(
72 GURL("http://www.google.com/imgres?q=hi")));
73 EXPECT_FALSE(IsGoogleSearchResultURL(
74 GURL("http://www.google.com/imgres?q=hi#123")));
75 EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://google.com/search")));
76 EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://WWW.GooGLE.CoM/search")));
77 EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://WWW.GooGLE.CoM/SeArcH")));
78 EXPECT_TRUE(IsGoogleSearchResultURL(GURL("http://www.google.co.uk/search")));
79 EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://google.co.uk/search")));
80 EXPECT_FALSE(IsGoogleSearchResultURL(GURL("http://www.chromium.org/search")));
83 // Ensure that we count PageSpeed headers correctly.
84 TEST_F(PrerenderUtilTest
, CountPageSpeedHeadersTest
) {
85 base::StatisticsRecorder::Initialize();
86 GURL
url("http://google.com");
87 std::string
temp("HTTP/1.1 200 OK\n\n");
88 std::replace(temp
.begin(), temp
.end(), '\n', '\0');
89 scoped_refptr
<net::HttpResponseHeaders
> headers(
90 new net::HttpResponseHeaders(temp
));
92 int num_responses
= 0;
97 int num_bucket_1
= 0; // unrecognized format/value bucket
98 int num_bucket_30
= 0; // 1.2.24.1 bucket
99 int num_bucket_33
= 0; // 1.3.25.2 bucket
101 scoped_ptr
<base::HistogramSamples
> server_samples
;
102 scoped_ptr
<base::HistogramSamples
> version_samples
;
104 // No PageSpeed header. The VersionCounts histogram isn't created yet.
105 GatherPagespeedData(content::RESOURCE_TYPE_MAIN_FRAME
, url
, headers
.get());
106 base::HistogramBase
* server_histogram
=
107 base::StatisticsRecorder::FindHistogram(
108 "Prerender.PagespeedHeader.ServerCounts");
109 ASSERT_TRUE(server_histogram
!= NULL
);
110 ASSERT_TRUE(NULL
== base::StatisticsRecorder::FindHistogram(
111 "Prerender.PagespeedHeader.VersionCounts"));
113 server_samples
= server_histogram
->SnapshotSamples();
114 EXPECT_EQ(++num_responses
, server_samples
->GetCount(0));
115 EXPECT_EQ( num_mps
, server_samples
->GetCount(1));
116 EXPECT_EQ( num_ngx
, server_samples
->GetCount(2));
117 EXPECT_EQ( num_pss
, server_samples
->GetCount(3));
118 EXPECT_EQ( num_other
, server_samples
->GetCount(4));
120 // X-Mod-Pagespeed header in expected format. VersionCounts now exists.
121 headers
->AddHeader("X-Mod-Pagespeed: 1.2.24.1-2300");
122 GatherPagespeedData(content::RESOURCE_TYPE_MAIN_FRAME
, url
, headers
.get());
123 base::HistogramBase
* version_histogram
=
124 base::StatisticsRecorder::FindHistogram(
125 "Prerender.PagespeedHeader.VersionCounts");
126 ASSERT_TRUE(version_histogram
!= NULL
);
127 server_samples
= server_histogram
->SnapshotSamples();
128 version_samples
= version_histogram
->SnapshotSamples();
129 EXPECT_EQ(++num_responses
, server_samples
->GetCount(0));
130 EXPECT_EQ(++num_mps
, server_samples
->GetCount(1));
131 EXPECT_EQ( num_ngx
, server_samples
->GetCount(2));
132 EXPECT_EQ( num_pss
, server_samples
->GetCount(3));
133 EXPECT_EQ( num_other
, server_samples
->GetCount(4));
134 EXPECT_EQ( num_bucket_1
, version_samples
->GetCount(1));
135 EXPECT_EQ(++num_bucket_30
, version_samples
->GetCount(30)); // +1 for #30
136 EXPECT_EQ( num_bucket_33
, version_samples
->GetCount(33));
137 headers
->RemoveHeader("X-Mod-Pagespeed");
139 // X-Mod-Pagespeed header in unexpected format.
140 headers
->AddHeader("X-Mod-Pagespeed: Powered By PageSpeed!");
141 GatherPagespeedData(content::RESOURCE_TYPE_MAIN_FRAME
, url
, headers
.get());
142 server_samples
= server_histogram
->SnapshotSamples();
143 version_samples
= version_histogram
->SnapshotSamples();
144 EXPECT_EQ(++num_responses
, server_samples
->GetCount(0));
145 EXPECT_EQ(++num_mps
, server_samples
->GetCount(1));
146 EXPECT_EQ( num_ngx
, server_samples
->GetCount(2));
147 EXPECT_EQ( num_pss
, server_samples
->GetCount(3));
148 EXPECT_EQ( num_other
, server_samples
->GetCount(4));
149 EXPECT_EQ(++num_bucket_1
, version_samples
->GetCount(1)); // +1 for 'huh?'
150 EXPECT_EQ( num_bucket_30
, version_samples
->GetCount(30));
151 EXPECT_EQ( num_bucket_33
, version_samples
->GetCount(33));
152 headers
->RemoveHeader("X-Mod-Pagespeed");
154 // X-Page-Speed header in mod_pagespeed format (so ngx_pagespeed).
155 headers
->AddHeader("X-Page-Speed: 1.3.25.2-2530");
156 GatherPagespeedData(content::RESOURCE_TYPE_MAIN_FRAME
, url
, headers
.get());
157 server_samples
= server_histogram
->SnapshotSamples();
158 version_samples
= version_histogram
->SnapshotSamples();
159 EXPECT_EQ(++num_responses
, server_samples
->GetCount(0));
160 EXPECT_EQ( num_mps
, server_samples
->GetCount(1));
161 EXPECT_EQ(++num_ngx
, server_samples
->GetCount(2));
162 EXPECT_EQ( num_pss
, server_samples
->GetCount(3));
163 EXPECT_EQ( num_other
, server_samples
->GetCount(4));
164 EXPECT_EQ( num_bucket_1
, version_samples
->GetCount(1));
165 EXPECT_EQ( num_bucket_30
, version_samples
->GetCount(30));
166 EXPECT_EQ(++num_bucket_33
, version_samples
->GetCount(33)); // +1 for #33
167 headers
->RemoveHeader("X-Page-Speed");
169 // X-Page-Speed header in PageSpeed Service format.
170 headers
->AddHeader("X-Page-Speed: 97_4_bo");
171 GatherPagespeedData(content::RESOURCE_TYPE_MAIN_FRAME
, url
, headers
.get());
172 server_samples
= server_histogram
->SnapshotSamples();
173 version_samples
= version_histogram
->SnapshotSamples();
174 EXPECT_EQ(++num_responses
, server_samples
->GetCount(0));
175 EXPECT_EQ( num_mps
, server_samples
->GetCount(1)); // no change
176 EXPECT_EQ( num_ngx
, server_samples
->GetCount(2));
177 EXPECT_EQ(++num_pss
, server_samples
->GetCount(3)); // +1 for PSS
178 EXPECT_EQ( num_other
, server_samples
->GetCount(4));
179 EXPECT_EQ( num_bucket_1
, version_samples
->GetCount(1));
180 EXPECT_EQ( num_bucket_30
, version_samples
->GetCount(30));
181 EXPECT_EQ( num_bucket_33
, version_samples
->GetCount(33));
182 headers
->RemoveHeader("X-Page-Speed");
184 // X-Page-Speed header in an unrecognized format (IISpeed in this case).
185 headers
->AddHeader("X-Page-Speed: 1.0PS1.2-20130615");
186 GatherPagespeedData(content::RESOURCE_TYPE_MAIN_FRAME
, url
, headers
.get());
187 server_samples
= server_histogram
->SnapshotSamples();
188 version_samples
= version_histogram
->SnapshotSamples();
189 EXPECT_EQ(++num_responses
, server_samples
->GetCount(0));
190 EXPECT_EQ( num_mps
, server_samples
->GetCount(1)); // no change
191 EXPECT_EQ( num_pss
, server_samples
->GetCount(3));
192 EXPECT_EQ(++num_other
, server_samples
->GetCount(4)); // +1 for 'other'
193 EXPECT_EQ( num_bucket_1
, version_samples
->GetCount(1));
194 EXPECT_EQ( num_bucket_30
, version_samples
->GetCount(30));
195 EXPECT_EQ( num_bucket_33
, version_samples
->GetCount(33));
197 // Not a main frame => not counted at all.
198 GatherPagespeedData(content::RESOURCE_TYPE_SUB_FRAME
, url
, headers
.get());
199 server_samples
= server_histogram
->SnapshotSamples();
200 version_samples
= version_histogram
->SnapshotSamples();
201 EXPECT_EQ( num_responses
, server_samples
->GetCount(0));
202 EXPECT_EQ( num_mps
, server_samples
->GetCount(1));
203 EXPECT_EQ( num_ngx
, server_samples
->GetCount(2));
204 EXPECT_EQ( num_pss
, server_samples
->GetCount(3));
205 EXPECT_EQ( num_other
, server_samples
->GetCount(4));
206 EXPECT_EQ( num_bucket_1
, version_samples
->GetCount(1));
207 EXPECT_EQ( num_bucket_30
, version_samples
->GetCount(30));
208 EXPECT_EQ( num_bucket_33
, version_samples
->GetCount(33));
210 // Not a http/https URL => not counted at all.
211 GURL
data_url("data:image/png;base64,yadda yadda==");
213 content::RESOURCE_TYPE_MAIN_FRAME
, data_url
, headers
.get());
214 server_samples
= server_histogram
->SnapshotSamples();
215 version_samples
= version_histogram
->SnapshotSamples();
216 EXPECT_EQ( num_responses
, server_samples
->GetCount(0));
217 EXPECT_EQ( num_mps
, server_samples
->GetCount(1));
218 EXPECT_EQ( num_ngx
, server_samples
->GetCount(2));
219 EXPECT_EQ( num_pss
, server_samples
->GetCount(3));
220 EXPECT_EQ( num_other
, server_samples
->GetCount(4));
221 EXPECT_EQ( num_bucket_1
, version_samples
->GetCount(1));
222 EXPECT_EQ( num_bucket_30
, version_samples
->GetCount(30));
223 EXPECT_EQ( num_bucket_33
, version_samples
->GetCount(33));
225 headers
->RemoveHeader("X-Page-Speed");
228 } // namespace prerender