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 "content/child/resource_dispatcher.h" | 5 #include "content/child/resource_dispatcher.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <tuple> | 12 #include <tuple> |
13 #include <utility> | 13 #include <utility> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/memory/shared_memory.h" | 18 #include "base/memory/shared_memory.h" |
19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
21 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
22 #include "base/test/scoped_feature_list.h" | 22 #include "base/test/scoped_feature_list.h" |
23 #include "content/child/request_extra_data.h" | 23 #include "content/child/request_extra_data.h" |
| 24 #include "content/child/test_request_peer.h" |
24 #include "content/common/appcache_interfaces.h" | 25 #include "content/common/appcache_interfaces.h" |
25 #include "content/common/resource_messages.h" | 26 #include "content/common/resource_messages.h" |
26 #include "content/common/resource_request.h" | 27 #include "content/common/resource_request.h" |
27 #include "content/common/resource_request_completion_status.h" | 28 #include "content/common/resource_request_completion_status.h" |
28 #include "content/common/service_worker/service_worker_types.h" | 29 #include "content/common/service_worker/service_worker_types.h" |
29 #include "content/public/child/fixed_received_data.h" | 30 #include "content/public/child/fixed_received_data.h" |
30 #include "content/public/child/request_peer.h" | 31 #include "content/public/child/request_peer.h" |
31 #include "content/public/child/resource_dispatcher_delegate.h" | 32 #include "content/public/child/resource_dispatcher_delegate.h" |
32 #include "content/public/common/content_features.h" | 33 #include "content/public/common/content_features.h" |
33 #include "content/public/common/request_context_frame_type.h" | 34 #include "content/public/common/request_context_frame_type.h" |
(...skipping 10 matching lines...) Expand all Loading... |
44 static const char kTestPageUrl[] = "http://www.google.com/"; | 45 static const char kTestPageUrl[] = "http://www.google.com/"; |
45 static const char kTestPageHeaders[] = | 46 static const char kTestPageHeaders[] = |
46 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; | 47 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; |
47 static const char kTestPageMimeType[] = "text/html"; | 48 static const char kTestPageMimeType[] = "text/html"; |
48 static const char kTestPageCharset[] = ""; | 49 static const char kTestPageCharset[] = ""; |
49 static const char kTestPageContents[] = | 50 static const char kTestPageContents[] = |
50 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>"; | 51 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>"; |
51 static const char kTestRedirectHeaders[] = | 52 static const char kTestRedirectHeaders[] = |
52 "HTTP/1.1 302 Found\nLocation:http://www.google.com/\n\n"; | 53 "HTTP/1.1 302 Found\nLocation:http://www.google.com/\n\n"; |
53 | 54 |
54 // Listens for request response data and stores it so that it can be compared | |
55 // to the reference data. | |
56 class TestRequestPeer : public RequestPeer { | |
57 public: | |
58 struct Context; | |
59 TestRequestPeer(ResourceDispatcher* dispatcher, Context* context) | |
60 : dispatcher_(dispatcher), context_(context) {} | |
61 | |
62 void OnUploadProgress(uint64_t position, uint64_t size) override {} | |
63 | |
64 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info, | |
65 const ResourceResponseInfo& info) override { | |
66 EXPECT_FALSE(context_->cancelled); | |
67 ++context_->seen_redirects; | |
68 if (context_->defer_on_redirect) | |
69 dispatcher_->SetDefersLoading(context_->request_id, true); | |
70 return context_->follow_redirects; | |
71 } | |
72 | |
73 void OnReceivedResponse(const ResourceResponseInfo& info) override { | |
74 EXPECT_FALSE(context_->cancelled); | |
75 EXPECT_FALSE(context_->received_response); | |
76 context_->received_response = true; | |
77 if (context_->cancel_on_receive_response) { | |
78 dispatcher_->Cancel(context_->request_id); | |
79 context_->cancelled = true; | |
80 } | |
81 } | |
82 | |
83 void OnDownloadedData(int len, int encoded_data_length) override { | |
84 EXPECT_FALSE(context_->cancelled); | |
85 context_->total_downloaded_data_length += len; | |
86 context_->total_encoded_data_length += encoded_data_length; | |
87 } | |
88 | |
89 void OnReceivedData(std::unique_ptr<ReceivedData> data) override { | |
90 if (context_->cancelled) | |
91 return; | |
92 EXPECT_TRUE(context_->received_response); | |
93 EXPECT_FALSE(context_->complete); | |
94 context_->data.append(data->payload(), data->length()); | |
95 | |
96 if (context_->cancel_on_receive_data) { | |
97 dispatcher_->Cancel(context_->request_id); | |
98 context_->cancelled = true; | |
99 } | |
100 } | |
101 | |
102 void OnTransferSizeUpdated(int transfer_size_diff) override { | |
103 if (context_->cancelled) | |
104 return; | |
105 context_->total_encoded_data_length += transfer_size_diff; | |
106 } | |
107 | |
108 void OnCompletedRequest(int error_code, | |
109 bool was_ignored_by_handler, | |
110 bool stale_copy_in_cache, | |
111 const base::TimeTicks& completion_time, | |
112 int64_t total_transfer_size, | |
113 int64_t encoded_body_size) override { | |
114 if (context_->cancelled) | |
115 return; | |
116 EXPECT_TRUE(context_->received_response); | |
117 EXPECT_FALSE(context_->complete); | |
118 context_->complete = true; | |
119 } | |
120 | |
121 struct Context { | |
122 // True if should follow redirects, false if should cancel them. | |
123 bool follow_redirects = true; | |
124 // True if the request should be deferred on redirects. | |
125 bool defer_on_redirect = false; | |
126 | |
127 // Number of total redirects seen. | |
128 int seen_redirects = 0; | |
129 | |
130 bool cancel_on_receive_response = false; | |
131 bool cancel_on_receive_data = false; | |
132 bool received_response = false; | |
133 | |
134 // Data received. If downloading to file, remains empty. | |
135 std::string data; | |
136 | |
137 // Total encoded data length, regardless of whether downloading to a file or | |
138 // not. | |
139 int total_encoded_data_length = 0; | |
140 // Total length when downloading to a file. | |
141 int total_downloaded_data_length = 0; | |
142 | |
143 bool complete = false; | |
144 bool cancelled = false; | |
145 int request_id = -1; | |
146 }; | |
147 | |
148 private: | |
149 ResourceDispatcher* dispatcher_; | |
150 Context* context_; | |
151 | |
152 DISALLOW_COPY_AND_ASSIGN(TestRequestPeer); | |
153 }; | |
154 | |
155 // Sets up the message sender override for the unit test. | 55 // Sets up the message sender override for the unit test. |
156 class ResourceDispatcherTest : public testing::Test, public IPC::Sender { | 56 class ResourceDispatcherTest : public testing::Test, public IPC::Sender { |
157 public: | 57 public: |
158 ResourceDispatcherTest() | 58 ResourceDispatcherTest() |
159 : dispatcher_(new ResourceDispatcher(this, message_loop_.task_runner())) { | 59 : dispatcher_(new ResourceDispatcher(this, message_loop_.task_runner())) { |
160 } | 60 } |
161 | 61 |
162 ~ResourceDispatcherTest() override { | 62 ~ResourceDispatcherTest() override { |
163 shared_memory_map_.clear(); | 63 shared_memory_map_.clear(); |
164 dispatcher_.reset(); | 64 dispatcher_.reset(); |
(...skipping 880 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 ResourceResponseHead response_head; | 945 ResourceResponseHead response_head; |
1046 | 946 |
1047 PerformTest(response_head); | 947 PerformTest(response_head); |
1048 | 948 |
1049 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); | 949 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); |
1050 EXPECT_EQ(base::TimeTicks(), | 950 EXPECT_EQ(base::TimeTicks(), |
1051 response_info().load_timing.connect_timing.dns_start); | 951 response_info().load_timing.connect_timing.dns_start); |
1052 } | 952 } |
1053 | 953 |
1054 } // namespace content | 954 } // namespace content |
OLD | NEW |