OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/base/net_log_util.h" | 5 #include "net/base/net_log_util.h" |
6 | 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/files/file_path.h" |
7 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
8 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "net/base/capturing_net_log_observer.h" |
9 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
10 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
11 #include "net/http/http_cache.h" | 15 #include "net/http/http_cache.h" |
12 #include "net/http/http_transaction.h" | 16 #include "net/http/http_transaction.h" |
| 17 #include "net/test/spawned_test_server/spawned_test_server.h" |
13 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
15 | 20 |
16 namespace net { | 21 namespace net { |
17 | 22 |
18 namespace { | 23 namespace { |
19 | 24 |
20 // Make sure GetNetConstants doesn't crash. | 25 // Make sure GetNetConstants doesn't crash. |
21 TEST(NetLogUtil, GetNetConstants) { | 26 TEST(NetLogUtil, GetNetConstants) { |
22 scoped_ptr<base::Value> constants(GetNetConstants()); | 27 scoped_ptr<base::Value> constants(GetNetConstants()); |
(...skipping 19 matching lines...) Expand all Loading... |
42 context.http_transaction_factory()->GetCache()->GetBackend( | 47 context.http_transaction_factory()->GetCache()->GetBackend( |
43 &backend, TestCompletionCallback().callback())); | 48 &backend, TestCompletionCallback().callback())); |
44 EXPECT_TRUE(http_cache->GetCurrentBackend()); | 49 EXPECT_TRUE(http_cache->GetCurrentBackend()); |
45 scoped_ptr<base::DictionaryValue> net_info_with_cache( | 50 scoped_ptr<base::DictionaryValue> net_info_with_cache( |
46 GetNetInfo(&context, NET_INFO_ALL_SOURCES)); | 51 GetNetInfo(&context, NET_INFO_ALL_SOURCES)); |
47 EXPECT_GT(net_info_with_cache->size(), 0u); | 52 EXPECT_GT(net_info_with_cache->size(), 0u); |
48 | 53 |
49 EXPECT_EQ(net_info_without_cache->size(), net_info_with_cache->size()); | 54 EXPECT_EQ(net_info_without_cache->size(), net_info_with_cache->size()); |
50 } | 55 } |
51 | 56 |
| 57 // Make sure CreateNetLogEntriesForActiveObjects works for requests from a |
| 58 // single URLRequestContext. |
| 59 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsOneContext) { |
| 60 // Using same context for each iteration makes sure deleted requests don't |
| 61 // appear in the list, or result in crashes. |
| 62 TestURLRequestContext context(true); |
| 63 NetLog net_log; |
| 64 context.set_net_log(&net_log); |
| 65 context.Init(); |
| 66 TestDelegate delegate; |
| 67 for (size_t num_requests = 0; num_requests < 5; ++num_requests) { |
| 68 ScopedVector<URLRequest> requests; |
| 69 for (size_t i = 0; i < num_requests; ++i) { |
| 70 requests.push_back(context.CreateRequest( |
| 71 GURL("about:life"), DEFAULT_PRIORITY, &delegate, nullptr).release()); |
| 72 } |
| 73 std::set<URLRequestContext*> contexts; |
| 74 contexts.insert(&context); |
| 75 CapturingNetLogObserver capturing_observer; |
| 76 CreateNetLogEntriesForActiveObjects(contexts, &capturing_observer); |
| 77 CapturedNetLogEntry::List entry_list; |
| 78 capturing_observer.GetEntries(&entry_list); |
| 79 ASSERT_EQ(num_requests, entry_list.size()); |
| 80 |
| 81 for (size_t i = 0; i < num_requests; ++i) { |
| 82 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id); |
| 83 } |
| 84 } |
| 85 } |
| 86 |
| 87 // Make sure CreateNetLogEntriesForActiveObjects works with multiple |
| 88 // URLRequestContexts. |
| 89 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsMultipleContexts) { |
| 90 TestDelegate delegate; |
| 91 for (size_t num_requests = 0; num_requests < 5; ++num_requests) { |
| 92 ScopedVector<TestURLRequestContext> contexts; |
| 93 ScopedVector<URLRequest> requests; |
| 94 NetLog net_log; |
| 95 std::set<URLRequestContext*> context_set; |
| 96 for (size_t i = 0; i < num_requests; ++i) { |
| 97 contexts.push_back(new TestURLRequestContext(true)); |
| 98 contexts[i]->set_net_log(&net_log); |
| 99 contexts[i]->Init(); |
| 100 context_set.insert(contexts[i]); |
| 101 requests.push_back(contexts[i]->CreateRequest( |
| 102 GURL("about:hats"), DEFAULT_PRIORITY, &delegate, nullptr).release()); |
| 103 } |
| 104 CapturingNetLogObserver capturing_observer; |
| 105 CreateNetLogEntriesForActiveObjects(context_set, &capturing_observer); |
| 106 CapturedNetLogEntry::List entry_list; |
| 107 capturing_observer.GetEntries(&entry_list); |
| 108 ASSERT_EQ(num_requests, entry_list.size()); |
| 109 |
| 110 for (size_t i = 0; i < num_requests; ++i) { |
| 111 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id); |
| 112 } |
| 113 } |
| 114 } |
| 115 |
52 } // namespace | 116 } // namespace |
53 | 117 |
54 } // namespace net | 118 } // namespace net |
OLD | NEW |