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" | |
willchan no longer on Chromium
2012/02/08 15:58:19
should go first
James Simonsen
2012/02/10 01:28:48
Done.
| |
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[] = { | |
willchan no longer on Chromium
2012/02/08 15:58:19
static const char* const
James Simonsen
2012/02/10 01:28:48
Done.
| |
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() { | |
mmenke1
2012/02/08 16:07:47
nit: These should be OVERRIDE.
James Simonsen
2012/02/10 01:28:48
Done.
| |
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: | |
willchan no longer on Chromium
2012/02/08 15:58:19
it shouldn't go protected=>private=>protected=>pri
James Simonsen
2012/02/10 01:28:48
Done.
| |
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, OldHttpVersion) { | |
339 HttpPipeliningCompatibilityClient::RequestInfo info; | |
340 info.filename = "http-1.0"; | |
341 info.expected_response = "abcdefghijklmnopqrstuvwxyz"; | |
342 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; | |
343 requests.push_back(info); | |
344 | |
345 RunTest(requests); | |
346 | |
347 base::Histogram::SampleSet status_sample = | |
348 GetHistogramValue(0, FIELD_STATUS); | |
349 EXPECT_EQ(1, status_sample.TotalCount()); | |
350 EXPECT_EQ(1, status_sample.counts( | |
351 HttpPipeliningCompatibilityClient::BAD_HTTP_VERSION)); | |
352 | |
353 base::Histogram::SampleSet network_sample = | |
354 GetHistogramValue(0, FIELD_NETWORK_ERROR); | |
355 EXPECT_EQ(0, network_sample.TotalCount()); | |
356 | |
357 base::Histogram::SampleSet response_sample = | |
358 GetHistogramValue(0, FIELD_RESPONSE_CODE); | |
359 EXPECT_EQ(1, response_sample.TotalCount()); | |
360 EXPECT_EQ(1, response_sample.counts(200)); | |
361 } | |
362 | |
363 TEST_F(HttpPipeliningCompatibilityClientTest, MultipleRequests) { | |
364 std::vector<HttpPipeliningCompatibilityClient::RequestInfo> requests; | |
365 | |
366 HttpPipeliningCompatibilityClient::RequestInfo info1; | |
367 info1.filename = "files/alphabet.txt"; | |
368 info1.expected_response = "abcdefghijklmnopqrstuvwxyz"; | |
369 requests.push_back(info1); | |
370 | |
371 HttpPipeliningCompatibilityClient::RequestInfo info2; | |
372 info2.filename = "close-socket"; | |
373 info2.expected_response = "shouldn't matter"; | |
374 requests.push_back(info2); | |
375 | |
376 HttpPipeliningCompatibilityClient::RequestInfo info3; | |
377 info3.filename = "auth-basic"; | |
378 info3.expected_response = "shouldn't matter"; | |
379 requests.push_back(info3); | |
380 | |
381 RunTest(requests); | |
382 | |
383 base::Histogram::SampleSet status_sample1 = | |
384 GetHistogramValue(0, FIELD_STATUS); | |
385 EXPECT_EQ(1, status_sample1.TotalCount()); | |
386 EXPECT_EQ(1, status_sample1.counts( | |
387 HttpPipeliningCompatibilityClient::SUCCESS)); | |
388 | |
389 base::Histogram::SampleSet network_sample1 = | |
390 GetHistogramValue(0, FIELD_NETWORK_ERROR); | |
391 EXPECT_EQ(0, network_sample1.TotalCount()); | |
392 | |
393 base::Histogram::SampleSet response_sample1 = | |
394 GetHistogramValue(0, FIELD_RESPONSE_CODE); | |
395 EXPECT_EQ(1, response_sample1.TotalCount()); | |
396 EXPECT_EQ(1, response_sample1.counts(200)); | |
397 | |
398 base::Histogram::SampleSet status_sample2 = | |
399 GetHistogramValue(1, FIELD_STATUS); | |
400 EXPECT_EQ(1, status_sample2.TotalCount()); | |
401 EXPECT_EQ(1, status_sample2.counts( | |
402 HttpPipeliningCompatibilityClient::NETWORK_ERROR)); | |
403 | |
404 base::Histogram::SampleSet network_sample2 = | |
405 GetHistogramValue(1, FIELD_NETWORK_ERROR); | |
406 EXPECT_EQ(1, network_sample2.TotalCount()); | |
407 EXPECT_EQ(1, network_sample2.counts(-net::ERR_EMPTY_RESPONSE)); | |
408 | |
409 base::Histogram::SampleSet response_sample2 = | |
410 GetHistogramValue(1, FIELD_RESPONSE_CODE); | |
411 EXPECT_EQ(0, response_sample2.TotalCount()); | |
412 | |
413 base::Histogram::SampleSet status_sample3 = | |
414 GetHistogramValue(2, FIELD_STATUS); | |
415 EXPECT_EQ(1, status_sample3.TotalCount()); | |
416 EXPECT_EQ(1, status_sample3.counts( | |
417 HttpPipeliningCompatibilityClient::BAD_RESPONSE_CODE)); | |
418 | |
419 base::Histogram::SampleSet network_sample3 = | |
420 GetHistogramValue(2, FIELD_NETWORK_ERROR); | |
421 EXPECT_EQ(0, network_sample3.TotalCount()); | |
422 | |
423 base::Histogram::SampleSet response_sample3 = | |
424 GetHistogramValue(2, FIELD_RESPONSE_CODE); | |
425 EXPECT_EQ(1, response_sample3.TotalCount()); | |
426 EXPECT_EQ(1, response_sample3.counts(401)); | |
427 } | |
428 | |
429 } // anonymous namespace | |
430 | |
431 } // namespace chrome_browser_net | |
OLD | NEW |