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