OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 <map> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/metrics/histogram.h" |
| 12 #include "base/stl_util.h" |
| 13 #include "base/stringprintf.h" |
| 14 #include "chrome/browser/net/http_pipelining_compatibility_client.h" |
| 15 #include "chrome/test/base/test_url_request_context_getter.h" |
| 16 #include "content/test/test_browser_thread.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/test_completion_callback.h" |
| 19 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "net/test/test_server.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace chrome_browser_net { |
| 24 |
| 25 namespace { |
| 26 |
| 27 static const char* kHistogramNames[] = { |
| 28 "NetConnectivity.Pipeline.0.NetworkError", |
| 29 "NetConnectivity.Pipeline.0.ResponseCode", |
| 30 "NetConnectivity.Pipeline.0.Status", |
| 31 "NetConnectivity.Pipeline.1.NetworkError", |
| 32 "NetConnectivity.Pipeline.1.ResponseCode", |
| 33 "NetConnectivity.Pipeline.1.Status", |
| 34 "NetConnectivity.Pipeline.2.NetworkError", |
| 35 "NetConnectivity.Pipeline.2.ResponseCode", |
| 36 "NetConnectivity.Pipeline.2.Status", |
| 37 }; |
| 38 |
| 39 enum HistogramField { |
| 40 FIELD_NETWORK_ERROR, |
| 41 FIELD_RESPONSE_CODE, |
| 42 FIELD_STATUS, |
| 43 }; |
| 44 |
| 45 class HttpPipeliningCompatibilityClientTest : public testing::Test { |
| 46 public: |
| 47 HttpPipeliningCompatibilityClientTest() |
| 48 : test_server_( |
| 49 net::TestServer::TYPE_HTTP, |
| 50 FilePath(FILE_PATH_LITERAL("chrome/test/data/http_pipelining"))) { |
| 51 } |
| 52 |
| 53 protected: |
| 54 virtual void SetUp() { |
| 55 ASSERT_TRUE(test_server_.Start()); |
| 56 context_ = new TestURLRequestContextGetter; |
| 57 context_->AddRef(); |
| 58 |
| 59 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 60 const char* name = kHistogramNames[i]; |
| 61 base::Histogram::SampleSet sample = GetHistogram(name); |
| 62 if (sample.TotalCount() > 0) { |
| 63 original_histograms_[name] = sample; |
| 64 } |
| 65 } |
| 66 } |
| 67 |
| 68 virtual void TearDown() { |
| 69 content::BrowserThread::ReleaseSoon(content::BrowserThread::IO, |
| 70 FROM_HERE, context_); |
| 71 message_loop_.RunAllPending(); |
| 72 } |
| 73 |
| 74 void RunTest( |
| 75 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests) { |
| 76 HttpPipeliningCompatibilityClient client; |
| 77 net::TestCompletionCallback callback; |
| 78 client.Start(test_server_.GetURL("").spec(), |
| 79 requests, callback.callback(), |
| 80 context_->GetURLRequestContext()); |
| 81 callback.WaitForResult(); |
| 82 |
| 83 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 84 const char* name = kHistogramNames[i]; |
| 85 base::Histogram::SampleSet sample = GetHistogram(name); |
| 86 if (ContainsKey(original_histograms_, name)) { |
| 87 sample.Subtract(original_histograms_[name]); |
| 88 } |
| 89 histograms_[name] = sample; |
| 90 } |
| 91 } |
| 92 |
| 93 base::Histogram::SampleSet GetHistogramValue(int request_id, |
| 94 HistogramField field) { |
| 95 const char* field_str = ""; |
| 96 switch (field) { |
| 97 case FIELD_STATUS: |
| 98 field_str = "Status"; |
| 99 break; |
| 100 |
| 101 case FIELD_NETWORK_ERROR: |
| 102 field_str = "NetworkError"; |
| 103 break; |
| 104 |
| 105 case FIELD_RESPONSE_CODE: |
| 106 field_str = "ResponseCode"; |
| 107 break; |
| 108 |
| 109 default: |
| 110 NOTREACHED(); |
| 111 break; |
| 112 } |
| 113 |
| 114 std::string name = base::StringPrintf("NetConnectivity.Pipeline.%d.%s", |
| 115 request_id, field_str); |
| 116 return histograms_[name]; |
| 117 } |
| 118 |
| 119 private: |
| 120 base::Histogram::SampleSet GetHistogram(const char* name) { |
| 121 base::Histogram::SampleSet sample; |
| 122 base::Histogram* histogram; |
| 123 if (base::StatisticsRecorder::FindHistogram(name, &histogram)) { |
| 124 histogram->SnapshotSample(&sample); |
| 125 } |
| 126 return sample; |
| 127 } |
| 128 |
| 129 protected: |
| 130 MessageLoopForIO message_loop_; |
| 131 net::TestServer test_server_; |
| 132 TestURLRequestContextGetter* context_; |
| 133 |
| 134 private: |
| 135 std::map<std::string, base::Histogram::SampleSet> histograms_; |
| 136 std::map<std::string, base::Histogram::SampleSet> original_histograms_; |
| 137 }; |
| 138 |
| 139 TEST_F(HttpPipeliningCompatibilityClientTest, Success) { |
| 140 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 141 info.filename = "files/alphabet.txt"; |
| 142 info.expected_response = "abcdefghijklmnopqrstuvwxyz"; |
| 143 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 144 requests.push_back(info); |
| 145 |
| 146 RunTest(requests); |
| 147 |
| 148 base::Histogram::SampleSet status_sample = |
| 149 GetHistogramValue(0, FIELD_STATUS); |
| 150 EXPECT_EQ(1, status_sample.TotalCount()); |
| 151 EXPECT_EQ(1, status_sample.counts( |
| 152 HttpPipeliningCompatibilityClient::SUCCESS)); |
| 153 |
| 154 base::Histogram::SampleSet network_sample = |
| 155 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 156 EXPECT_EQ(0, network_sample.TotalCount()); |
| 157 |
| 158 base::Histogram::SampleSet response_sample = |
| 159 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 160 EXPECT_EQ(1, response_sample.TotalCount()); |
| 161 EXPECT_EQ(1, response_sample.counts(200)); |
| 162 } |
| 163 |
| 164 TEST_F(HttpPipeliningCompatibilityClientTest, TooSmall) { |
| 165 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 166 info.filename = "files/alphabet.txt"; |
| 167 info.expected_response = "abcdefghijklmnopqrstuvwxyz26"; |
| 168 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 169 requests.push_back(info); |
| 170 |
| 171 RunTest(requests); |
| 172 |
| 173 base::Histogram::SampleSet status_sample = |
| 174 GetHistogramValue(0, FIELD_STATUS); |
| 175 EXPECT_EQ(1, status_sample.TotalCount()); |
| 176 EXPECT_EQ(1, status_sample.counts( |
| 177 HttpPipeliningCompatibilityClient::TOO_SMALL)); |
| 178 |
| 179 base::Histogram::SampleSet network_sample = |
| 180 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 181 EXPECT_EQ(0, network_sample.TotalCount()); |
| 182 |
| 183 base::Histogram::SampleSet response_sample = |
| 184 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 185 EXPECT_EQ(1, response_sample.TotalCount()); |
| 186 EXPECT_EQ(1, response_sample.counts(200)); |
| 187 } |
| 188 |
| 189 TEST_F(HttpPipeliningCompatibilityClientTest, TooLarge) { |
| 190 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 191 info.filename = "files/alphabet.txt"; |
| 192 info.expected_response = "abc"; |
| 193 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 194 requests.push_back(info); |
| 195 |
| 196 RunTest(requests); |
| 197 |
| 198 base::Histogram::SampleSet status_sample = |
| 199 GetHistogramValue(0, FIELD_STATUS); |
| 200 EXPECT_EQ(1, status_sample.TotalCount()); |
| 201 EXPECT_EQ(1, status_sample.counts( |
| 202 HttpPipeliningCompatibilityClient::TOO_LARGE)); |
| 203 |
| 204 base::Histogram::SampleSet network_sample = |
| 205 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 206 EXPECT_EQ(0, network_sample.TotalCount()); |
| 207 |
| 208 base::Histogram::SampleSet response_sample = |
| 209 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 210 EXPECT_EQ(1, response_sample.TotalCount()); |
| 211 EXPECT_EQ(1, response_sample.counts(200)); |
| 212 } |
| 213 |
| 214 TEST_F(HttpPipeliningCompatibilityClientTest, Mismatch) { |
| 215 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 216 info.filename = "files/alphabet.txt"; |
| 217 info.expected_response = "zyxwvutsrqponmlkjihgfedcba"; |
| 218 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 219 requests.push_back(info); |
| 220 |
| 221 RunTest(requests); |
| 222 |
| 223 base::Histogram::SampleSet status_sample = |
| 224 GetHistogramValue(0, FIELD_STATUS); |
| 225 EXPECT_EQ(1, status_sample.TotalCount()); |
| 226 EXPECT_EQ(1, status_sample.counts( |
| 227 HttpPipeliningCompatibilityClient::CONTENT_MISMATCH)); |
| 228 |
| 229 base::Histogram::SampleSet network_sample = |
| 230 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 231 EXPECT_EQ(0, network_sample.TotalCount()); |
| 232 |
| 233 base::Histogram::SampleSet response_sample = |
| 234 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 235 EXPECT_EQ(1, response_sample.TotalCount()); |
| 236 EXPECT_EQ(1, response_sample.counts(200)); |
| 237 } |
| 238 |
| 239 TEST_F(HttpPipeliningCompatibilityClientTest, Redirect) { |
| 240 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 241 info.filename = "server-redirect?http://foo.bar/asdf"; |
| 242 info.expected_response = "shouldn't matter"; |
| 243 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 244 requests.push_back(info); |
| 245 |
| 246 RunTest(requests); |
| 247 |
| 248 base::Histogram::SampleSet status_sample = |
| 249 GetHistogramValue(0, FIELD_STATUS); |
| 250 EXPECT_EQ(1, status_sample.TotalCount()); |
| 251 EXPECT_EQ(1, status_sample.counts( |
| 252 HttpPipeliningCompatibilityClient::REDIRECTED)); |
| 253 |
| 254 base::Histogram::SampleSet network_sample = |
| 255 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 256 EXPECT_EQ(0, network_sample.TotalCount()); |
| 257 |
| 258 base::Histogram::SampleSet response_sample = |
| 259 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 260 EXPECT_EQ(0, response_sample.TotalCount()); |
| 261 } |
| 262 |
| 263 TEST_F(HttpPipeliningCompatibilityClientTest, AuthRequired) { |
| 264 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 265 info.filename = "auth-basic"; |
| 266 info.expected_response = "shouldn't matter"; |
| 267 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 268 requests.push_back(info); |
| 269 |
| 270 RunTest(requests); |
| 271 |
| 272 base::Histogram::SampleSet status_sample = |
| 273 GetHistogramValue(0, FIELD_STATUS); |
| 274 EXPECT_EQ(1, status_sample.TotalCount()); |
| 275 EXPECT_EQ(1, status_sample.counts( |
| 276 HttpPipeliningCompatibilityClient::BAD_RESPONSE_CODE)); |
| 277 |
| 278 base::Histogram::SampleSet network_sample = |
| 279 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 280 EXPECT_EQ(0, network_sample.TotalCount()); |
| 281 |
| 282 base::Histogram::SampleSet response_sample = |
| 283 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 284 EXPECT_EQ(1, response_sample.TotalCount()); |
| 285 EXPECT_EQ(1, response_sample.counts(401)); |
| 286 } |
| 287 |
| 288 TEST_F(HttpPipeliningCompatibilityClientTest, NoContent) { |
| 289 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 290 info.filename = "nocontent"; |
| 291 info.expected_response = "shouldn't matter"; |
| 292 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 293 requests.push_back(info); |
| 294 |
| 295 RunTest(requests); |
| 296 |
| 297 base::Histogram::SampleSet status_sample = |
| 298 GetHistogramValue(0, FIELD_STATUS); |
| 299 EXPECT_EQ(1, status_sample.TotalCount()); |
| 300 EXPECT_EQ(1, status_sample.counts( |
| 301 HttpPipeliningCompatibilityClient::BAD_RESPONSE_CODE)); |
| 302 |
| 303 base::Histogram::SampleSet network_sample = |
| 304 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 305 EXPECT_EQ(0, network_sample.TotalCount()); |
| 306 |
| 307 base::Histogram::SampleSet response_sample = |
| 308 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 309 EXPECT_EQ(1, response_sample.TotalCount()); |
| 310 EXPECT_EQ(1, response_sample.counts(204)); |
| 311 } |
| 312 |
| 313 TEST_F(HttpPipeliningCompatibilityClientTest, CloseSocket) { |
| 314 HttpPipeliningCompatibilityClient::RequestInfo info; |
| 315 info.filename = "close-socket"; |
| 316 info.expected_response = "shouldn't matter"; |
| 317 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 318 requests.push_back(info); |
| 319 |
| 320 RunTest(requests); |
| 321 |
| 322 base::Histogram::SampleSet status_sample = |
| 323 GetHistogramValue(0, FIELD_STATUS); |
| 324 EXPECT_EQ(1, status_sample.TotalCount()); |
| 325 EXPECT_EQ(1, status_sample.counts( |
| 326 HttpPipeliningCompatibilityClient::NETWORK_ERROR)); |
| 327 |
| 328 base::Histogram::SampleSet network_sample = |
| 329 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 330 EXPECT_EQ(1, network_sample.TotalCount()); |
| 331 EXPECT_EQ(1, network_sample.counts(-net::ERR_EMPTY_RESPONSE)); |
| 332 |
| 333 base::Histogram::SampleSet response_sample = |
| 334 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 335 EXPECT_EQ(0, response_sample.TotalCount()); |
| 336 } |
| 337 |
| 338 TEST_F(HttpPipeliningCompatibilityClientTest, MultipleRequests) { |
| 339 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; |
| 340 |
| 341 HttpPipeliningCompatibilityClient::RequestInfo info1; |
| 342 info1.filename = "files/alphabet.txt"; |
| 343 info1.expected_response = "abcdefghijklmnopqrstuvwxyz"; |
| 344 requests.push_back(info1); |
| 345 |
| 346 HttpPipeliningCompatibilityClient::RequestInfo info2; |
| 347 info2.filename = "close-socket"; |
| 348 info2.expected_response = "shouldn't matter"; |
| 349 requests.push_back(info2); |
| 350 |
| 351 HttpPipeliningCompatibilityClient::RequestInfo info3; |
| 352 info3.filename = "auth-basic"; |
| 353 info3.expected_response = "shouldn't matter"; |
| 354 requests.push_back(info3); |
| 355 |
| 356 RunTest(requests); |
| 357 |
| 358 base::Histogram::SampleSet status_sample1 = |
| 359 GetHistogramValue(0, FIELD_STATUS); |
| 360 EXPECT_EQ(1, status_sample1.TotalCount()); |
| 361 EXPECT_EQ(1, status_sample1.counts( |
| 362 HttpPipeliningCompatibilityClient::SUCCESS)); |
| 363 |
| 364 base::Histogram::SampleSet network_sample1 = |
| 365 GetHistogramValue(0, FIELD_NETWORK_ERROR); |
| 366 EXPECT_EQ(0, network_sample1.TotalCount()); |
| 367 |
| 368 base::Histogram::SampleSet response_sample1 = |
| 369 GetHistogramValue(0, FIELD_RESPONSE_CODE); |
| 370 EXPECT_EQ(1, response_sample1.TotalCount()); |
| 371 EXPECT_EQ(1, response_sample1.counts(200)); |
| 372 |
| 373 base::Histogram::SampleSet status_sample2 = |
| 374 GetHistogramValue(1, FIELD_STATUS); |
| 375 EXPECT_EQ(1, status_sample2.TotalCount()); |
| 376 EXPECT_EQ(1, status_sample2.counts( |
| 377 HttpPipeliningCompatibilityClient::NETWORK_ERROR)); |
| 378 |
| 379 base::Histogram::SampleSet network_sample2 = |
| 380 GetHistogramValue(1, FIELD_NETWORK_ERROR); |
| 381 EXPECT_EQ(1, network_sample2.TotalCount()); |
| 382 EXPECT_EQ(1, network_sample2.counts(-net::ERR_EMPTY_RESPONSE)); |
| 383 |
| 384 base::Histogram::SampleSet response_sample2 = |
| 385 GetHistogramValue(1, FIELD_RESPONSE_CODE); |
| 386 EXPECT_EQ(0, response_sample2.TotalCount()); |
| 387 |
| 388 base::Histogram::SampleSet status_sample3 = |
| 389 GetHistogramValue(2, FIELD_STATUS); |
| 390 EXPECT_EQ(1, status_sample3.TotalCount()); |
| 391 EXPECT_EQ(1, status_sample3.counts( |
| 392 HttpPipeliningCompatibilityClient::BAD_RESPONSE_CODE)); |
| 393 |
| 394 base::Histogram::SampleSet network_sample3 = |
| 395 GetHistogramValue(2, FIELD_NETWORK_ERROR); |
| 396 EXPECT_EQ(0, network_sample3.TotalCount()); |
| 397 |
| 398 base::Histogram::SampleSet response_sample3 = |
| 399 GetHistogramValue(2, FIELD_RESPONSE_CODE); |
| 400 EXPECT_EQ(1, response_sample3.TotalCount()); |
| 401 EXPECT_EQ(1, response_sample3.counts(401)); |
| 402 } |
| 403 |
| 404 } // anonymous namespace |
| 405 |
| 406 } // namespace chrome_browser_net |
OLD | NEW |