| 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/common/net/url_fetcher_impl.h" | 5 #include "content/common/net/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "content/public/common/url_fetcher_delegate.h" | |
| 17 #include "crypto/nss_util.h" | 16 #include "crypto/nss_util.h" |
| 18 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 19 #include "net/test/test_server.h" | 18 #include "net/test/test_server.h" |
| 19 #include "net/url_request/url_fetcher_delegate.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "net/url_request/url_request_test_util.h" | 21 #include "net/url_request/url_request_test_util.h" |
| 22 #include "net/url_request/url_request_throttler_manager.h" | 22 #include "net/url_request/url_request_throttler_manager.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 #if defined(USE_NSS) | 25 #if defined(USE_NSS) |
| 26 #include "net/ocsp/nss_ocsp.h" | 26 #include "net/ocsp/nss_ocsp.h" |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 using base::Time; | 29 using base::Time; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 protected: | 65 protected: |
| 66 virtual ~ThrottlingTestURLRequestContextGetter() {} | 66 virtual ~ThrottlingTestURLRequestContextGetter() {} |
| 67 | 67 |
| 68 TestURLRequestContext* const context_; | 68 TestURLRequestContext* const context_; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 } // namespace | 71 } // namespace |
| 72 | 72 |
| 73 class URLFetcherTest : public testing::Test, | 73 class URLFetcherTest : public testing::Test, |
| 74 public content::URLFetcherDelegate { | 74 public net::URLFetcherDelegate { |
| 75 public: | 75 public: |
| 76 URLFetcherTest() | 76 URLFetcherTest() |
| 77 : fetcher_(NULL), | 77 : fetcher_(NULL), |
| 78 context_(new ThrottlingTestURLRequestContext()) { | 78 context_(new ThrottlingTestURLRequestContext()) { |
| 79 } | 79 } |
| 80 | 80 |
| 81 static int GetNumFetcherCores() { | 81 static int GetNumFetcherCores() { |
| 82 return URLFetcherImpl::GetNumFetcherCores(); | 82 return URLFetcherImpl::GetNumFetcherCores(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Creates a URLFetcher, using the program's main thread to do IO. | 85 // Creates a URLFetcher, using the program's main thread to do IO. |
| 86 virtual void CreateFetcher(const GURL& url); | 86 virtual void CreateFetcher(const GURL& url); |
| 87 | 87 |
| 88 // content::URLFetcherDelegate | 88 // net::URLFetcherDelegate |
| 89 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 89 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 90 | 90 |
| 91 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 91 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
| 92 return io_message_loop_proxy_; | 92 return io_message_loop_proxy_; |
| 93 } | 93 } |
| 94 | 94 |
| 95 TestURLRequestContext* request_context() { | 95 TestURLRequestContext* request_context() { |
| 96 return context_.get(); | 96 return context_.get(); |
| 97 } | 97 } |
| 98 | 98 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 } | 150 } |
| 151 | 151 |
| 152 namespace { | 152 namespace { |
| 153 | 153 |
| 154 // Version of URLFetcherTest that does a POST instead | 154 // Version of URLFetcherTest that does a POST instead |
| 155 class URLFetcherPostTest : public URLFetcherTest { | 155 class URLFetcherPostTest : public URLFetcherTest { |
| 156 public: | 156 public: |
| 157 // URLFetcherTest override. | 157 // URLFetcherTest override. |
| 158 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 158 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 159 | 159 |
| 160 // content::URLFetcherDelegate | 160 // net::URLFetcherDelegate |
| 161 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 161 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 162 }; | 162 }; |
| 163 | 163 |
| 164 // Version of URLFetcherTest that tests download progress reports. | 164 // Version of URLFetcherTest that tests download progress reports. |
| 165 class URLFetcherDownloadProgressTest : public URLFetcherTest { | 165 class URLFetcherDownloadProgressTest : public URLFetcherTest { |
| 166 public: | 166 public: |
| 167 // URLFetcherTest override. | 167 // URLFetcherTest override. |
| 168 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 168 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 169 | 169 |
| 170 // content::URLFetcherDelegate | 170 // net::URLFetcherDelegate |
| 171 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 171 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 172 int64 current, int64 total) OVERRIDE; | 172 int64 current, int64 total) OVERRIDE; |
| 173 protected: | 173 protected: |
| 174 int64 previous_progress_; | 174 int64 previous_progress_; |
| 175 int64 expected_total_; | 175 int64 expected_total_; |
| 176 }; | 176 }; |
| 177 | 177 |
| 178 /// Version of URLFetcherTest that tests progress reports at cancellation. | 178 /// Version of URLFetcherTest that tests progress reports at cancellation. |
| 179 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { | 179 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { |
| 180 public: | 180 public: |
| 181 // URLFetcherTest override. | 181 // URLFetcherTest override. |
| 182 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 182 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 183 | 183 |
| 184 // content::URLFetcherDelegate | 184 // net::URLFetcherDelegate |
| 185 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 185 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 186 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 186 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 187 int64 current, int64 total) OVERRIDE; | 187 int64 current, int64 total) OVERRIDE; |
| 188 protected: | 188 protected: |
| 189 bool cancelled_; | 189 bool cancelled_; |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 // Version of URLFetcherTest that tests upload progress reports. | 192 // Version of URLFetcherTest that tests upload progress reports. |
| 193 class URLFetcherUploadProgressTest : public URLFetcherTest { | 193 class URLFetcherUploadProgressTest : public URLFetcherTest { |
| 194 public: | 194 public: |
| 195 virtual void CreateFetcher(const GURL& url); | 195 virtual void CreateFetcher(const GURL& url); |
| 196 | 196 |
| 197 // content::URLFetcherDelegate | 197 // net::URLFetcherDelegate |
| 198 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source, | 198 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source, |
| 199 int64 current, int64 total); | 199 int64 current, int64 total); |
| 200 protected: | 200 protected: |
| 201 int64 previous_progress_; | 201 int64 previous_progress_; |
| 202 std::string chunk_; | 202 std::string chunk_; |
| 203 int64 number_of_chunks_added_; | 203 int64 number_of_chunks_added_; |
| 204 }; | 204 }; |
| 205 | 205 |
| 206 // Version of URLFetcherTest that tests headers. | 206 // Version of URLFetcherTest that tests headers. |
| 207 class URLFetcherHeadersTest : public URLFetcherTest { | 207 class URLFetcherHeadersTest : public URLFetcherTest { |
| 208 public: | 208 public: |
| 209 // content::URLFetcherDelegate | 209 // net::URLFetcherDelegate |
| 210 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 210 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 211 }; | 211 }; |
| 212 | 212 |
| 213 // Version of URLFetcherTest that tests SocketAddress. | 213 // Version of URLFetcherTest that tests SocketAddress. |
| 214 class URLFetcherSocketAddressTest : public URLFetcherTest { | 214 class URLFetcherSocketAddressTest : public URLFetcherTest { |
| 215 public: | 215 public: |
| 216 // content::URLFetcherDelegate | 216 // net::URLFetcherDelegate |
| 217 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 217 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 218 protected: | 218 protected: |
| 219 std::string expected_host_; | 219 std::string expected_host_; |
| 220 uint16 expected_port_; | 220 uint16 expected_port_; |
| 221 }; | 221 }; |
| 222 | 222 |
| 223 // Version of URLFetcherTest that tests overload protection. | 223 // Version of URLFetcherTest that tests overload protection. |
| 224 class URLFetcherProtectTest : public URLFetcherTest { | 224 class URLFetcherProtectTest : public URLFetcherTest { |
| 225 public: | 225 public: |
| 226 // URLFetcherTest override. | 226 // URLFetcherTest override. |
| 227 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 227 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 228 // content::URLFetcherDelegate | 228 // net::URLFetcherDelegate |
| 229 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 229 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 230 private: | 230 private: |
| 231 Time start_time_; | 231 Time start_time_; |
| 232 }; | 232 }; |
| 233 | 233 |
| 234 // Version of URLFetcherTest that tests overload protection, when responses | 234 // Version of URLFetcherTest that tests overload protection, when responses |
| 235 // passed through. | 235 // passed through. |
| 236 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | 236 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { |
| 237 public: | 237 public: |
| 238 // URLFetcherTest override. | 238 // URLFetcherTest override. |
| 239 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 239 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 240 // content::URLFetcherDelegate | 240 // net::URLFetcherDelegate |
| 241 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 241 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 242 private: | 242 private: |
| 243 Time start_time_; | 243 Time start_time_; |
| 244 }; | 244 }; |
| 245 | 245 |
| 246 // Version of URLFetcherTest that tests bad HTTPS requests. | 246 // Version of URLFetcherTest that tests bad HTTPS requests. |
| 247 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 247 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| 248 public: | 248 public: |
| 249 URLFetcherBadHTTPSTest(); | 249 URLFetcherBadHTTPSTest(); |
| 250 | 250 |
| 251 // content::URLFetcherDelegate | 251 // net::URLFetcherDelegate |
| 252 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 252 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 253 | 253 |
| 254 private: | 254 private: |
| 255 FilePath cert_dir_; | 255 FilePath cert_dir_; |
| 256 }; | 256 }; |
| 257 | 257 |
| 258 // Version of URLFetcherTest that tests request cancellation on shutdown. | 258 // Version of URLFetcherTest that tests request cancellation on shutdown. |
| 259 class URLFetcherCancelTest : public URLFetcherTest { | 259 class URLFetcherCancelTest : public URLFetcherTest { |
| 260 public: | 260 public: |
| 261 // URLFetcherTest override. | 261 // URLFetcherTest override. |
| 262 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 262 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 263 // content::URLFetcherDelegate | 263 // net::URLFetcherDelegate |
| 264 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 264 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 265 | 265 |
| 266 void CancelRequest(); | 266 void CancelRequest(); |
| 267 }; | 267 }; |
| 268 | 268 |
| 269 // Version of TestURLRequestContext that posts a Quit task to the IO | 269 // Version of TestURLRequestContext that posts a Quit task to the IO |
| 270 // thread once it is deleted. | 270 // thread once it is deleted. |
| 271 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { | 271 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { |
| 272 public: | 272 public: |
| 273 explicit CancelTestURLRequestContext() { | 273 explicit CancelTestURLRequestContext() { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 private: | 325 private: |
| 326 scoped_ptr<TestURLRequestContext> context_; | 326 scoped_ptr<TestURLRequestContext> context_; |
| 327 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 327 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 328 base::WaitableEvent context_created_; | 328 base::WaitableEvent context_created_; |
| 329 GURL throttle_for_url_; | 329 GURL throttle_for_url_; |
| 330 }; | 330 }; |
| 331 | 331 |
| 332 // Version of URLFetcherTest that tests retying the same request twice. | 332 // Version of URLFetcherTest that tests retying the same request twice. |
| 333 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 333 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
| 334 public: | 334 public: |
| 335 // content::URLFetcherDelegate | 335 // net::URLFetcherDelegate |
| 336 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 336 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 337 private: | 337 private: |
| 338 std::string data_; | 338 std::string data_; |
| 339 }; | 339 }; |
| 340 | 340 |
| 341 class URLFetcherFileTest : public URLFetcherTest { | 341 class URLFetcherFileTest : public URLFetcherTest { |
| 342 public: | 342 public: |
| 343 URLFetcherFileTest() : take_ownership_of_file_(false), | 343 URLFetcherFileTest() : take_ownership_of_file_(false), |
| 344 expected_file_error_(base::PLATFORM_FILE_OK) {} | 344 expected_file_error_(base::PLATFORM_FILE_OK) {} |
| 345 | 345 |
| 346 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); | 346 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); |
| 347 void CreateFetcherForTempFile(const GURL& url); | 347 void CreateFetcherForTempFile(const GURL& url); |
| 348 | 348 |
| 349 // content::URLFetcherDelegate | 349 // net::URLFetcherDelegate |
| 350 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 350 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 351 | 351 |
| 352 protected: | 352 protected: |
| 353 FilePath expected_file_; | 353 FilePath expected_file_; |
| 354 FilePath file_path_; | 354 FilePath file_path_; |
| 355 | 355 |
| 356 // Set by the test. Used in OnURLFetchComplete() to decide if | 356 // Set by the test. Used in OnURLFetchComplete() to decide if |
| 357 // the URLFetcher should own the temp file, so that we can test | 357 // the URLFetcher should own the temp file, so that we can test |
| 358 // disowning prevents the file from being deleted. | 358 // disowning prevents the file from being deleted. |
| 359 bool take_ownership_of_file_; | 359 bool take_ownership_of_file_; |
| (...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1146 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1147 | 1147 |
| 1148 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1148 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1149 | 1149 |
| 1150 MessageLoop::current()->RunAllPending(); | 1150 MessageLoop::current()->RunAllPending(); |
| 1151 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1151 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1152 << file_path_.value() << " not removed."; | 1152 << file_path_.value() << " not removed."; |
| 1153 } | 1153 } |
| 1154 | 1154 |
| 1155 } // namespace. | 1155 } // namespace. |
| OLD | NEW |