OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/url_request/report_sender.h" | 5 #include "net/url_request/report_sender.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 80 |
81 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } | 81 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } |
82 | 82 |
83 size_t num_requests() const { return num_requests_; } | 83 size_t num_requests() const { return num_requests_; } |
84 | 84 |
85 // Sets whether cookies are expected to be sent on requests. | 85 // Sets whether cookies are expected to be sent on requests. |
86 void set_expect_cookies(bool expect_cookies) { | 86 void set_expect_cookies(bool expect_cookies) { |
87 expect_cookies_ = expect_cookies; | 87 expect_cookies_ = expect_cookies; |
88 } | 88 } |
89 | 89 |
| 90 void set_expected_content_type(const std::string& content_type) { |
| 91 expected_content_type_ = content_type; |
| 92 } |
| 93 |
90 // NetworkDelegateImpl implementation. | 94 // NetworkDelegateImpl implementation. |
91 int OnBeforeURLRequest(URLRequest* request, | 95 int OnBeforeURLRequest(URLRequest* request, |
92 const CompletionCallback& callback, | 96 const CompletionCallback& callback, |
93 GURL* new_url) override { | 97 GURL* new_url) override { |
94 num_requests_++; | 98 num_requests_++; |
95 EXPECT_EQ(expect_url_, request->url()); | 99 EXPECT_EQ(expect_url_, request->url()); |
96 EXPECT_STRCASEEQ("POST", request->method().data()); | 100 EXPECT_STRCASEEQ("POST", request->method().data()); |
97 | 101 |
98 if (expect_cookies_) { | 102 if (expect_cookies_) { |
99 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); | 103 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); |
100 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); | 104 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); |
101 } else { | 105 } else { |
102 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); | 106 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); |
103 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); | 107 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); |
104 } | 108 } |
105 | 109 |
| 110 const HttpRequestHeaders& extra_headers = request->extra_request_headers(); |
| 111 std::string content_type; |
| 112 EXPECT_TRUE(extra_headers.GetHeader(HttpRequestHeaders::kContentType, |
| 113 &content_type)); |
| 114 EXPECT_EQ(expected_content_type_, content_type); |
| 115 |
106 CheckUploadData(*request, &expect_reports_); | 116 CheckUploadData(*request, &expect_reports_); |
107 | 117 |
108 // Unconditionally return OK, since the sender ignores the results | 118 // Unconditionally return OK, since the sender ignores the results |
109 // anyway. | 119 // anyway. |
110 return OK; | 120 return OK; |
111 } | 121 } |
112 | 122 |
113 void OnURLRequestDestroyed(URLRequest* request) override { | 123 void OnURLRequestDestroyed(URLRequest* request) override { |
114 url_request_destroyed_callback_.Run(); | 124 url_request_destroyed_callback_.Run(); |
115 if (expect_reports_.empty()) | 125 if (expect_reports_.empty()) |
116 all_url_requests_destroyed_callback_.Run(); | 126 all_url_requests_destroyed_callback_.Run(); |
117 } | 127 } |
118 | 128 |
119 private: | 129 private: |
120 base::Closure url_request_destroyed_callback_; | 130 base::Closure url_request_destroyed_callback_; |
121 base::Closure all_url_requests_destroyed_callback_; | 131 base::Closure all_url_requests_destroyed_callback_; |
122 size_t num_requests_; | 132 size_t num_requests_; |
123 GURL expect_url_; | 133 GURL expect_url_; |
124 std::set<std::string> expect_reports_; | 134 std::set<std::string> expect_reports_; |
125 bool expect_cookies_; | 135 bool expect_cookies_; |
| 136 std::string expected_content_type_; |
126 | 137 |
127 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); | 138 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); |
128 }; | 139 }; |
129 | 140 |
130 class ReportSenderTest : public ::testing::Test { | 141 class ReportSenderTest : public ::testing::Test { |
131 public: | 142 public: |
132 ReportSenderTest() : context_(true) { | 143 ReportSenderTest() : context_(true) { |
133 context_.set_network_delegate(&network_delegate_); | 144 context_.set_network_delegate(&network_delegate_); |
134 context_.Init(); | 145 context_.Init(); |
135 } | 146 } |
(...skipping 11 matching lines...) Expand all Loading... |
147 void SendReport(ReportSender* reporter, | 158 void SendReport(ReportSender* reporter, |
148 const std::string& report, | 159 const std::string& report, |
149 const GURL& url, | 160 const GURL& url, |
150 size_t request_sequence_number) { | 161 size_t request_sequence_number) { |
151 base::RunLoop run_loop; | 162 base::RunLoop run_loop; |
152 network_delegate_.set_url_request_destroyed_callback( | 163 network_delegate_.set_url_request_destroyed_callback( |
153 run_loop.QuitClosure()); | 164 run_loop.QuitClosure()); |
154 | 165 |
155 network_delegate_.set_expect_url(url); | 166 network_delegate_.set_expect_url(url); |
156 network_delegate_.ExpectReport(report); | 167 network_delegate_.ExpectReport(report); |
| 168 network_delegate_.set_expected_content_type("application/foobar"); |
157 | 169 |
158 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); | 170 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); |
159 | 171 |
160 reporter->Send(url, report); | 172 reporter->Send(url, "application/foobar", report); |
161 | 173 |
162 // The report is sent asynchronously, so wait for the report's | 174 // The report is sent asynchronously, so wait for the report's |
163 // URLRequest to be destroyed before checking that the report was | 175 // URLRequest to be destroyed before checking that the report was |
164 // sent. | 176 // sent. |
165 run_loop.Run(); | 177 run_loop.Run(); |
166 | 178 |
167 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); | 179 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); |
168 } | 180 } |
169 | 181 |
170 TestReportSenderNetworkDelegate network_delegate_; | 182 TestReportSenderNetworkDelegate network_delegate_; |
(...skipping 19 matching lines...) Expand all Loading... |
190 | 202 |
191 TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) { | 203 TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) { |
192 base::RunLoop run_loop; | 204 base::RunLoop run_loop; |
193 network_delegate_.set_all_url_requests_destroyed_callback( | 205 network_delegate_.set_all_url_requests_destroyed_callback( |
194 run_loop.QuitClosure()); | 206 run_loop.QuitClosure()); |
195 | 207 |
196 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 208 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
197 network_delegate_.set_expect_url(url); | 209 network_delegate_.set_expect_url(url); |
198 network_delegate_.ExpectReport(kDummyReport); | 210 network_delegate_.ExpectReport(kDummyReport); |
199 network_delegate_.ExpectReport(kSecondDummyReport); | 211 network_delegate_.ExpectReport(kSecondDummyReport); |
| 212 network_delegate_.set_expected_content_type("application/foobar"); |
200 | 213 |
201 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 214 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
202 | 215 |
203 EXPECT_EQ(0u, network_delegate_.num_requests()); | 216 EXPECT_EQ(0u, network_delegate_.num_requests()); |
204 | 217 |
205 reporter.Send(url, kDummyReport); | 218 reporter.Send(url, "application/foobar", kDummyReport); |
206 reporter.Send(url, kSecondDummyReport); | 219 reporter.Send(url, "application/foobar", kSecondDummyReport); |
207 | 220 |
208 run_loop.Run(); | 221 run_loop.Run(); |
209 | 222 |
210 EXPECT_EQ(2u, network_delegate_.num_requests()); | 223 EXPECT_EQ(2u, network_delegate_.num_requests()); |
211 } | 224 } |
212 | 225 |
213 // Test that pending URLRequests get cleaned up when the report sender | 226 // Test that pending URLRequests get cleaned up when the report sender |
214 // is deleted. | 227 // is deleted. |
215 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) { | 228 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) { |
216 bool url_request_destroyed = false; | 229 bool url_request_destroyed = false; |
217 network_delegate_.set_url_request_destroyed_callback(base::Bind( | 230 network_delegate_.set_url_request_destroyed_callback(base::Bind( |
218 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); | 231 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); |
219 | 232 |
220 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 233 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
221 URLRequestFailedJob::START, ERR_IO_PENDING); | 234 URLRequestFailedJob::START, ERR_IO_PENDING); |
222 network_delegate_.set_expect_url(url); | 235 network_delegate_.set_expect_url(url); |
223 network_delegate_.ExpectReport(kDummyReport); | 236 network_delegate_.ExpectReport(kDummyReport); |
| 237 network_delegate_.set_expected_content_type("application/foobar"); |
224 | 238 |
225 EXPECT_EQ(0u, network_delegate_.num_requests()); | 239 EXPECT_EQ(0u, network_delegate_.num_requests()); |
226 | 240 |
227 std::unique_ptr<ReportSender> reporter( | 241 std::unique_ptr<ReportSender> reporter( |
228 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES)); | 242 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES)); |
229 reporter->Send(url, kDummyReport); | 243 reporter->Send(url, "application/foobar", kDummyReport); |
230 reporter.reset(); | 244 reporter.reset(); |
231 | 245 |
232 EXPECT_EQ(1u, network_delegate_.num_requests()); | 246 EXPECT_EQ(1u, network_delegate_.num_requests()); |
233 EXPECT_TRUE(url_request_destroyed); | 247 EXPECT_TRUE(url_request_destroyed); |
234 } | 248 } |
235 | 249 |
236 // Test that a request that returns an error gets cleaned up. | 250 // Test that a request that returns an error gets cleaned up. |
237 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { | 251 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { |
238 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | 252 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); |
239 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 253 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) { | 292 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) { |
279 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 293 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
280 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 294 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
281 | 295 |
282 network_delegate_.set_expect_cookies(false); | 296 network_delegate_.set_expect_cookies(false); |
283 SendReport(&reporter, kDummyReport, url, 0); | 297 SendReport(&reporter, kDummyReport, url, 0); |
284 } | 298 } |
285 | 299 |
286 } // namespace | 300 } // namespace |
287 } // namespace net | 301 } // namespace net |
OLD | NEW |