OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 #include "net/url_request/url_request_test_util.h" | 43 #include "net/url_request/url_request_test_util.h" |
44 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
45 #include "url/gurl.h" | 45 #include "url/gurl.h" |
46 | 46 |
47 #if BUILDFLAG(ENABLE_EXTENSIONS) | 47 #if BUILDFLAG(ENABLE_EXTENSIONS) |
48 #include "chrome/browser/extensions/event_router_forwarder.h" | 48 #include "chrome/browser/extensions/event_router_forwarder.h" |
49 #endif | 49 #endif |
50 | 50 |
51 namespace { | 51 namespace { |
52 | 52 |
53 const char* const kHttpRequestCompletionErrorCodes = | |
54 "Net.HttpRequestCompletionErrorCodes"; | |
asanka
2017/03/07 19:15:47
Use something like:
const char kHttpRequestComple
Amey J
2017/03/08 02:05:18
Acknowledged.
| |
55 const char* const kHttpRequestCompletionErrorCodesMainFrame = | |
56 "Net.HttpRequestCompletionErrorCodes.MainFrame"; | |
57 | |
53 // This function requests a URL, and makes it return a known response. | 58 // This function requests a URL, and makes it return a known response. |
54 // ResourceRequestInfo is attached to the URLRequest, to represent this request | 59 // ResourceRequestInfo is attached to the URLRequest, to represent this request |
55 // as an user initiated. | 60 // as an user initiated. |
56 std::unique_ptr<net::URLRequest> RequestURL( | 61 std::unique_ptr<net::URLRequest> RequestURL( |
57 net::URLRequestContext* context, | 62 net::URLRequestContext* context, |
58 net::MockClientSocketFactory* socket_factory) { | 63 net::MockClientSocketFactory* socket_factory) { |
59 net::MockRead redirect_mock_reads[] = { | 64 net::MockRead redirect_mock_reads[] = { |
60 net::MockRead("HTTP/1.1 302 Found\r\n" | 65 net::MockRead("HTTP/1.1 302 Found\r\n" |
61 "Location: http://bar.com/\r\n\r\n"), | 66 "Location: http://bar.com/\r\n\r\n"), |
62 net::MockRead(net::SYNCHRONOUS, net::OK), | 67 net::MockRead(net::SYNCHRONOUS, net::OK), |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 RequestURL(context(), socket_factory()); | 233 RequestURL(context(), socket_factory()); |
229 | 234 |
230 EXPECT_EQ(0, fake_aggregator.on_the_record_tx_bytes()); | 235 EXPECT_EQ(0, fake_aggregator.on_the_record_tx_bytes()); |
231 EXPECT_EQ(0, fake_aggregator.on_the_record_rx_bytes()); | 236 EXPECT_EQ(0, fake_aggregator.on_the_record_rx_bytes()); |
232 EXPECT_EQ(request->GetTotalSentBytes(), | 237 EXPECT_EQ(request->GetTotalSentBytes(), |
233 fake_aggregator.off_the_record_tx_bytes()); | 238 fake_aggregator.off_the_record_tx_bytes()); |
234 EXPECT_EQ(request->GetTotalReceivedBytes(), | 239 EXPECT_EQ(request->GetTotalReceivedBytes(), |
235 fake_aggregator.off_the_record_rx_bytes()); | 240 fake_aggregator.off_the_record_rx_bytes()); |
236 } | 241 } |
237 | 242 |
243 TEST_F(ChromeNetworkDelegateTest, HttpRequestCompletionErrorCodes) { | |
244 Initialize(); | |
245 | |
246 const struct { | |
247 const GURL url; | |
248 int net_error; | |
249 bool main_frame; | |
asanka
2017/03/07 19:15:47
is_main_frame
Amey J
2017/03/08 02:05:18
Acknowledged.
| |
250 int expected_sample_bucket; | |
251 int expected_request_completion_count; | |
252 int expected_request_completion_main_frame_count; | |
253 } tests[] = { | |
254 {GURL("http://example.com"), net::OK, true, std::abs(net::OK), 1, 1}, | |
255 {GURL("http://example.com"), net::ERR_ABORTED, true, | |
256 std::abs(net::ERR_ABORTED), 1, 1}, | |
257 {GURL("http://example.com"), net::OK, false, std::abs(net::OK), 1, 0}, | |
258 {GURL("https://example.com"), net::OK, true, std::abs(net::OK), 0, 0}, | |
259 }; | |
260 | |
261 for (size_t i = 0; i < arraysize(tests); ++i) { | |
asanka
2017/03/07 19:15:47
use a range based iterator. I.e. something like:
Amey J
2017/03/08 02:05:18
Sweet! Somehow, I always thought that range based
| |
262 base::HistogramTester histograms; | |
263 | |
264 net::TestDelegate test_delegate; | |
265 std::unique_ptr<net::URLRequest> request(context()->CreateRequest( | |
266 tests[i].url, net::DEFAULT_PRIORITY, &test_delegate)); | |
267 if (tests[i].main_frame) { | |
268 request->SetLoadFlags(request->load_flags() | | |
269 net::LOAD_MAIN_FRAME_DEPRECATED); | |
270 } | |
271 network_delegate()->NotifyCompleted(request.get(), false, | |
272 tests[i].net_error); | |
273 | |
274 histograms.ExpectTotalCount(kHttpRequestCompletionErrorCodes, | |
275 tests[i].expected_request_completion_count); | |
276 histograms.ExpectUniqueSample(kHttpRequestCompletionErrorCodes, | |
277 tests[i].expected_sample_bucket, | |
278 tests[i].expected_request_completion_count); | |
279 histograms.ExpectTotalCount( | |
280 kHttpRequestCompletionErrorCodesMainFrame, | |
281 tests[i].expected_request_completion_main_frame_count); | |
282 histograms.ExpectUniqueSample( | |
283 kHttpRequestCompletionErrorCodesMainFrame, | |
284 tests[i].expected_sample_bucket, | |
285 tests[i].expected_request_completion_main_frame_count); | |
286 } | |
287 } | |
288 | |
238 class ChromeNetworkDelegatePolicyTest : public testing::Test { | 289 class ChromeNetworkDelegatePolicyTest : public testing::Test { |
239 public: | 290 public: |
240 ChromeNetworkDelegatePolicyTest() | 291 ChromeNetworkDelegatePolicyTest() |
241 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 292 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
242 #if BUILDFLAG(ENABLE_EXTENSIONS) | 293 #if BUILDFLAG(ENABLE_EXTENSIONS) |
243 forwarder_ = new extensions::EventRouterForwarder(); | 294 forwarder_ = new extensions::EventRouterForwarder(); |
244 #endif | 295 #endif |
245 } | 296 } |
246 | 297 |
247 protected: | 298 protected: |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
583 | 634 |
584 // Files in external storage are allowed. | 635 // Files in external storage are allowed. |
585 base::FilePath external_storage_path; | 636 base::FilePath external_storage_path; |
586 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path); | 637 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path); |
587 EXPECT_TRUE(IsAccessAllowed( | 638 EXPECT_TRUE(IsAccessAllowed( |
588 external_storage_path.AppendASCII("foo.txt").AsUTF8Unsafe(), "")); | 639 external_storage_path.AppendASCII("foo.txt").AsUTF8Unsafe(), "")); |
589 // The external storage root itself is not allowed. | 640 // The external storage root itself is not allowed. |
590 EXPECT_FALSE(IsAccessAllowed(external_storage_path.AsUTF8Unsafe(), "")); | 641 EXPECT_FALSE(IsAccessAllowed(external_storage_path.AsUTF8Unsafe(), "")); |
591 #endif | 642 #endif |
592 } | 643 } |
OLD | NEW |