| 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& expected_content_type) { |
| 91 expected_content_type_ = expected_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 if (!expected_content_type_.empty()) { |
| 111 HttpRequestHeaders headers; |
| 112 request->GetFullRequestHeaders(&headers); |
| 113 std::string content_type; |
| 114 EXPECT_TRUE( |
| 115 headers.GetHeader(HttpRequestHeaders::kContentType, &content_type)); |
| 116 EXPECT_EQ(expected_content_type_, content_type); |
| 117 } |
| 118 |
| 106 CheckUploadData(*request, &expect_reports_); | 119 CheckUploadData(*request, &expect_reports_); |
| 107 | 120 |
| 108 // Unconditionally return OK, since the sender ignores the results | 121 // Unconditionally return OK, since the sender ignores the results |
| 109 // anyway. | 122 // anyway. |
| 110 return OK; | 123 return OK; |
| 111 } | 124 } |
| 112 | 125 |
| 113 void OnURLRequestDestroyed(URLRequest* request) override { | 126 void OnURLRequestDestroyed(URLRequest* request) override { |
| 114 url_request_destroyed_callback_.Run(); | 127 url_request_destroyed_callback_.Run(); |
| 115 if (expect_reports_.empty()) | 128 if (expect_reports_.empty()) |
| 116 all_url_requests_destroyed_callback_.Run(); | 129 all_url_requests_destroyed_callback_.Run(); |
| 117 } | 130 } |
| 118 | 131 |
| 119 private: | 132 private: |
| 120 base::Closure url_request_destroyed_callback_; | 133 base::Closure url_request_destroyed_callback_; |
| 121 base::Closure all_url_requests_destroyed_callback_; | 134 base::Closure all_url_requests_destroyed_callback_; |
| 122 size_t num_requests_; | 135 size_t num_requests_; |
| 123 GURL expect_url_; | 136 GURL expect_url_; |
| 124 std::set<std::string> expect_reports_; | 137 std::set<std::string> expect_reports_; |
| 125 bool expect_cookies_; | 138 bool expect_cookies_; |
| 139 std::string expected_content_type_; |
| 126 | 140 |
| 127 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); | 141 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); |
| 128 }; | 142 }; |
| 129 | 143 |
| 130 class ReportSenderTest : public ::testing::Test { | 144 class ReportSenderTest : public ::testing::Test { |
| 131 public: | 145 public: |
| 132 ReportSenderTest() : context_(true) { | 146 ReportSenderTest() : context_(true) { |
| 133 context_.set_network_delegate(&network_delegate_); | 147 context_.set_network_delegate(&network_delegate_); |
| 134 context_.Init(); | 148 context_.Init(); |
| 135 } | 149 } |
| (...skipping 142 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 |