OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/net/url_fetcher_impl.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/file_util.h" | |
11 #include "base/message_loop_proxy.h" | |
12 #include "base/scoped_temp_dir.h" | |
13 #include "base/synchronization/waitable_event.h" | |
14 #include "base/threading/thread.h" | |
15 #include "build/build_config.h" | |
16 #include "crypto/nss_util.h" | |
17 #include "net/http/http_response_headers.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" | |
21 #include "net/url_request/url_request_test_util.h" | |
22 #include "net/url_request/url_request_throttler_manager.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 #if defined(USE_NSS) | |
26 #include "net/ocsp/nss_ocsp.h" | |
27 #endif | |
28 | |
29 using base::Time; | |
30 using base::TimeDelta; | |
31 | |
32 // TODO(eroman): Add a regression test for http://crbug.com/40505. | |
33 | |
34 namespace { | |
35 | |
36 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); | |
37 const char kTestServerFilePrefix[] = "files/"; | |
38 | |
39 class ThrottlingTestURLRequestContext : public TestURLRequestContext { | |
40 public: | |
41 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) { | |
42 set_throttler_manager(&throttler_manager_); | |
43 Init(); | |
44 DCHECK(throttler_manager() != NULL); | |
45 } | |
46 | |
47 private: | |
48 net::URLRequestThrottlerManager throttler_manager_; | |
49 }; | |
50 | |
51 class ThrottlingTestURLRequestContextGetter | |
52 : public TestURLRequestContextGetter { | |
53 public: | |
54 ThrottlingTestURLRequestContextGetter( | |
55 base::MessageLoopProxy* io_message_loop_proxy, | |
56 TestURLRequestContext* request_context) | |
57 : TestURLRequestContextGetter(io_message_loop_proxy), | |
58 context_(request_context) { | |
59 } | |
60 | |
61 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | |
62 return context_; | |
63 } | |
64 | |
65 protected: | |
66 virtual ~ThrottlingTestURLRequestContextGetter() {} | |
67 | |
68 TestURLRequestContext* const context_; | |
69 }; | |
70 | |
71 } // namespace | |
72 | |
73 class URLFetcherTest : public testing::Test, | |
74 public net::URLFetcherDelegate { | |
75 public: | |
76 URLFetcherTest() | |
77 : fetcher_(NULL), | |
78 context_(new ThrottlingTestURLRequestContext()) { | |
79 } | |
80 | |
81 static int GetNumFetcherCores() { | |
82 return URLFetcherImpl::GetNumFetcherCores(); | |
83 } | |
84 | |
85 // Creates a URLFetcher, using the program's main thread to do IO. | |
86 virtual void CreateFetcher(const GURL& url); | |
87 | |
88 // net::URLFetcherDelegate | |
89 // Subclasses that override this should either call this function or | |
90 // CleanupAfterFetchComplete() at the end of their processing, depending on | |
91 // whether they want to check for a non-empty HTTP 200 response or not. | |
92 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
93 | |
94 // Deletes |fetcher| and terminates the message loop. | |
95 void CleanupAfterFetchComplete(); | |
96 | |
97 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | |
98 return io_message_loop_proxy_; | |
99 } | |
100 | |
101 TestURLRequestContext* request_context() { | |
102 return context_.get(); | |
103 } | |
104 | |
105 protected: | |
106 virtual void SetUp() OVERRIDE { | |
107 testing::Test::SetUp(); | |
108 | |
109 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | |
110 | |
111 #if defined(USE_NSS) | |
112 crypto::EnsureNSSInit(); | |
113 net::EnsureNSSHttpIOInit(); | |
114 #endif | |
115 } | |
116 | |
117 virtual void TearDown() OVERRIDE { | |
118 #if defined(USE_NSS) | |
119 net::ShutdownNSSHttpIO(); | |
120 #endif | |
121 } | |
122 | |
123 // URLFetcher is designed to run on the main UI thread, but in our tests | |
124 // we assume that the current thread is the IO thread where the URLFetcher | |
125 // dispatches its requests to. When we wish to simulate being used from | |
126 // a UI thread, we dispatch a worker thread to do so. | |
127 MessageLoopForIO io_loop_; | |
128 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
129 | |
130 URLFetcherImpl* fetcher_; | |
131 const scoped_ptr<TestURLRequestContext> context_; | |
132 }; | |
133 | |
134 void URLFetcherTest::CreateFetcher(const GURL& url) { | |
135 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
136 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
137 io_message_loop_proxy(), request_context())); | |
138 fetcher_->Start(); | |
139 } | |
140 | |
141 void URLFetcherTest::OnURLFetchComplete(const net::URLFetcher* source) { | |
142 EXPECT_TRUE(source->GetStatus().is_success()); | |
143 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | |
144 | |
145 std::string data; | |
146 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
147 EXPECT_FALSE(data.empty()); | |
148 | |
149 CleanupAfterFetchComplete(); | |
150 } | |
151 | |
152 void URLFetcherTest::CleanupAfterFetchComplete() { | |
153 delete fetcher_; // Have to delete this here and not in the destructor, | |
154 // because the destructor won't necessarily run on the | |
155 // same thread that CreateFetcher() did. | |
156 | |
157 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
158 // If the current message loop is not the IO loop, it will be shut down when | |
159 // the main loop returns and this thread subsequently goes out of scope. | |
160 } | |
161 | |
162 namespace { | |
163 | |
164 // Version of URLFetcherTest that does a POST instead | |
165 class URLFetcherPostTest : public URLFetcherTest { | |
166 public: | |
167 // URLFetcherTest override. | |
168 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
169 | |
170 // net::URLFetcherDelegate | |
171 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
172 }; | |
173 | |
174 // Version of URLFetcherTest that tests download progress reports. | |
175 class URLFetcherDownloadProgressTest : public URLFetcherTest { | |
176 public: | |
177 // URLFetcherTest override. | |
178 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
179 | |
180 // net::URLFetcherDelegate | |
181 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, | |
182 int64 current, int64 total) OVERRIDE; | |
183 protected: | |
184 int64 previous_progress_; | |
185 int64 expected_total_; | |
186 }; | |
187 | |
188 /// Version of URLFetcherTest that tests progress reports at cancellation. | |
189 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { | |
190 public: | |
191 // URLFetcherTest override. | |
192 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
193 | |
194 // net::URLFetcherDelegate | |
195 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
196 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, | |
197 int64 current, int64 total) OVERRIDE; | |
198 protected: | |
199 bool cancelled_; | |
200 }; | |
201 | |
202 // Version of URLFetcherTest that tests upload progress reports. | |
203 class URLFetcherUploadProgressTest : public URLFetcherTest { | |
204 public: | |
205 virtual void CreateFetcher(const GURL& url); | |
206 | |
207 // net::URLFetcherDelegate | |
208 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source, | |
209 int64 current, int64 total); | |
210 protected: | |
211 int64 previous_progress_; | |
212 std::string chunk_; | |
213 int64 number_of_chunks_added_; | |
214 }; | |
215 | |
216 // Version of URLFetcherTest that tests headers. | |
217 class URLFetcherHeadersTest : public URLFetcherTest { | |
218 public: | |
219 // net::URLFetcherDelegate | |
220 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
221 }; | |
222 | |
223 // Version of URLFetcherTest that tests SocketAddress. | |
224 class URLFetcherSocketAddressTest : public URLFetcherTest { | |
225 public: | |
226 // net::URLFetcherDelegate | |
227 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
228 protected: | |
229 std::string expected_host_; | |
230 uint16 expected_port_; | |
231 }; | |
232 | |
233 // Version of URLFetcherTest that tests stopping on a redirect. | |
234 class URLFetcherStopOnRedirectTest : public URLFetcherTest { | |
235 public: | |
236 URLFetcherStopOnRedirectTest(); | |
237 virtual ~URLFetcherStopOnRedirectTest(); | |
238 | |
239 // URLFetcherTest override. | |
240 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
241 // net::URLFetcherDelegate | |
242 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
243 | |
244 protected: | |
245 // The URL we should be redirected to. | |
246 static const char* kRedirectTarget; | |
247 | |
248 bool callback_called_; // Set to true in OnURLFetchComplete(). | |
249 }; | |
250 | |
251 // Version of URLFetcherTest that tests overload protection. | |
252 class URLFetcherProtectTest : public URLFetcherTest { | |
253 public: | |
254 // URLFetcherTest override. | |
255 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
256 // net::URLFetcherDelegate | |
257 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
258 private: | |
259 Time start_time_; | |
260 }; | |
261 | |
262 // Version of URLFetcherTest that tests overload protection, when responses | |
263 // passed through. | |
264 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | |
265 public: | |
266 // URLFetcherTest override. | |
267 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
268 // net::URLFetcherDelegate | |
269 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
270 private: | |
271 Time start_time_; | |
272 }; | |
273 | |
274 // Version of URLFetcherTest that tests bad HTTPS requests. | |
275 class URLFetcherBadHTTPSTest : public URLFetcherTest { | |
276 public: | |
277 URLFetcherBadHTTPSTest(); | |
278 | |
279 // net::URLFetcherDelegate | |
280 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
281 | |
282 private: | |
283 FilePath cert_dir_; | |
284 }; | |
285 | |
286 // Version of URLFetcherTest that tests request cancellation on shutdown. | |
287 class URLFetcherCancelTest : public URLFetcherTest { | |
288 public: | |
289 // URLFetcherTest override. | |
290 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
291 // net::URLFetcherDelegate | |
292 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
293 | |
294 void CancelRequest(); | |
295 }; | |
296 | |
297 // Version of TestURLRequestContext that posts a Quit task to the IO | |
298 // thread once it is deleted. | |
299 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { | |
300 public: | |
301 explicit CancelTestURLRequestContext() { | |
302 } | |
303 | |
304 private: | |
305 virtual ~CancelTestURLRequestContext() { | |
306 // The d'tor should execute in the IO thread. Post the quit task to the | |
307 // current thread. | |
308 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
309 } | |
310 }; | |
311 | |
312 class CancelTestURLRequestContextGetter | |
313 : public TestURLRequestContextGetter { | |
314 public: | |
315 CancelTestURLRequestContextGetter( | |
316 base::MessageLoopProxy* io_message_loop_proxy, | |
317 const GURL& throttle_for_url) | |
318 : TestURLRequestContextGetter(io_message_loop_proxy), | |
319 io_message_loop_proxy_(io_message_loop_proxy), | |
320 context_created_(false, false), | |
321 throttle_for_url_(throttle_for_url) { | |
322 } | |
323 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | |
324 if (!context_.get()) { | |
325 context_.reset(new CancelTestURLRequestContext()); | |
326 DCHECK(context_->throttler_manager()); | |
327 | |
328 // Registers an entry for test url. The backoff time is calculated by: | |
329 // new_backoff = 2.0 * old_backoff + 0 | |
330 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | |
331 // Maximum retries allowed is set to 2. | |
332 scoped_refptr<net::URLRequestThrottlerEntry> entry( | |
333 new net::URLRequestThrottlerEntry( | |
334 context_->throttler_manager(), | |
335 "", 200, 3, 2000, 2.0, 0.0, 4000)); | |
336 context_->throttler_manager()->OverrideEntryForTests( | |
337 throttle_for_url_, entry); | |
338 | |
339 context_created_.Signal(); | |
340 } | |
341 return context_.get(); | |
342 } | |
343 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | |
344 return io_message_loop_proxy_; | |
345 } | |
346 void WaitForContextCreation() { | |
347 context_created_.Wait(); | |
348 } | |
349 | |
350 protected: | |
351 virtual ~CancelTestURLRequestContextGetter() {} | |
352 | |
353 private: | |
354 scoped_ptr<TestURLRequestContext> context_; | |
355 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
356 base::WaitableEvent context_created_; | |
357 GURL throttle_for_url_; | |
358 }; | |
359 | |
360 // Version of URLFetcherTest that tests retying the same request twice. | |
361 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | |
362 public: | |
363 // net::URLFetcherDelegate | |
364 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
365 private: | |
366 std::string data_; | |
367 }; | |
368 | |
369 class URLFetcherFileTest : public URLFetcherTest { | |
370 public: | |
371 URLFetcherFileTest() : take_ownership_of_file_(false), | |
372 expected_file_error_(base::PLATFORM_FILE_OK) {} | |
373 | |
374 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); | |
375 void CreateFetcherForTempFile(const GURL& url); | |
376 | |
377 // net::URLFetcherDelegate | |
378 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
379 | |
380 protected: | |
381 FilePath expected_file_; | |
382 FilePath file_path_; | |
383 | |
384 // Set by the test. Used in OnURLFetchComplete() to decide if | |
385 // the URLFetcher should own the temp file, so that we can test | |
386 // disowning prevents the file from being deleted. | |
387 bool take_ownership_of_file_; | |
388 | |
389 // Expected file error code for the test. | |
390 // PLATFORM_FILE_OK when expecting success. | |
391 base::PlatformFileError expected_file_error_; | |
392 }; | |
393 | |
394 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | |
395 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::POST, this); | |
396 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
397 io_message_loop_proxy(), request_context())); | |
398 fetcher_->SetUploadData("application/x-www-form-urlencoded", | |
399 "bobsyeruncle"); | |
400 fetcher_->Start(); | |
401 } | |
402 | |
403 void URLFetcherPostTest::OnURLFetchComplete(const net::URLFetcher* source) { | |
404 std::string data; | |
405 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
406 EXPECT_EQ(std::string("bobsyeruncle"), data); | |
407 URLFetcherTest::OnURLFetchComplete(source); | |
408 } | |
409 | |
410 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | |
411 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
412 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
413 io_message_loop_proxy(), request_context())); | |
414 previous_progress_ = 0; | |
415 fetcher_->Start(); | |
416 } | |
417 | |
418 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | |
419 const net::URLFetcher* source, int64 current, int64 total) { | |
420 // Increasing between 0 and total. | |
421 EXPECT_LE(0, current); | |
422 EXPECT_GE(total, current); | |
423 EXPECT_LE(previous_progress_, current); | |
424 previous_progress_ = current; | |
425 EXPECT_EQ(expected_total_, total); | |
426 } | |
427 | |
428 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | |
429 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
430 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
431 io_message_loop_proxy(), request_context())); | |
432 cancelled_ = false; | |
433 fetcher_->Start(); | |
434 } | |
435 | |
436 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( | |
437 const net::URLFetcher* source, int64 current, int64 total) { | |
438 EXPECT_FALSE(cancelled_); | |
439 if (!cancelled_) { | |
440 cancelled_ = true; | |
441 CleanupAfterFetchComplete(); | |
442 } | |
443 } | |
444 | |
445 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete( | |
446 const net::URLFetcher* source) { | |
447 // Should have been cancelled. | |
448 ADD_FAILURE(); | |
449 CleanupAfterFetchComplete(); | |
450 } | |
451 | |
452 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) { | |
453 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::POST, this); | |
454 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
455 io_message_loop_proxy(), request_context())); | |
456 previous_progress_ = 0; | |
457 // Large enough data to require more than one read from UploadDataStream. | |
458 chunk_.assign(1<<16, 'a'); | |
459 // Use chunked upload to wait for a timer event of progress notification. | |
460 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded"); | |
461 fetcher_->Start(); | |
462 number_of_chunks_added_ = 1; | |
463 fetcher_->AppendChunkToUpload(chunk_, false); | |
464 } | |
465 | |
466 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress( | |
467 const net::URLFetcher* source, int64 current, int64 total) { | |
468 // Increasing between 0 and total. | |
469 EXPECT_LE(0, current); | |
470 EXPECT_GE(static_cast<int64>(chunk_.size()) * number_of_chunks_added_, | |
471 current); | |
472 EXPECT_LE(previous_progress_, current); | |
473 previous_progress_ = current; | |
474 EXPECT_EQ(-1, total); | |
475 | |
476 if (number_of_chunks_added_ < 2) { | |
477 number_of_chunks_added_ += 1; | |
478 fetcher_->AppendChunkToUpload(chunk_, true); | |
479 } | |
480 } | |
481 | |
482 void URLFetcherHeadersTest::OnURLFetchComplete( | |
483 const net::URLFetcher* source) { | |
484 std::string header; | |
485 EXPECT_TRUE(source->GetResponseHeaders()->GetNormalizedHeader("cache-control", | |
486 &header)); | |
487 EXPECT_EQ("private", header); | |
488 URLFetcherTest::OnURLFetchComplete(source); | |
489 } | |
490 | |
491 void URLFetcherSocketAddressTest::OnURLFetchComplete( | |
492 const net::URLFetcher* source) { | |
493 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); | |
494 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); | |
495 URLFetcherTest::OnURLFetchComplete(source); | |
496 } | |
497 | |
498 // static | |
499 const char* URLFetcherStopOnRedirectTest::kRedirectTarget = | |
500 "http://redirect.target.com"; | |
501 | |
502 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest() | |
503 : callback_called_(false) { | |
504 } | |
505 | |
506 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() { | |
507 } | |
508 | |
509 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL& url) { | |
510 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
511 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
512 io_message_loop_proxy(), request_context())); | |
513 fetcher_->SetStopOnRedirect(true); | |
514 fetcher_->Start(); | |
515 } | |
516 | |
517 void URLFetcherStopOnRedirectTest::OnURLFetchComplete( | |
518 const net::URLFetcher* source) { | |
519 callback_called_ = true; | |
520 EXPECT_EQ(GURL(kRedirectTarget), source->GetURL()); | |
521 EXPECT_EQ(net::URLRequestStatus::CANCELED, source->GetStatus().status()); | |
522 EXPECT_EQ(301, source->GetResponseCode()); | |
523 CleanupAfterFetchComplete(); | |
524 } | |
525 | |
526 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | |
527 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
528 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
529 io_message_loop_proxy(), request_context())); | |
530 start_time_ = Time::Now(); | |
531 fetcher_->SetMaxRetries(11); | |
532 fetcher_->Start(); | |
533 } | |
534 | |
535 void URLFetcherProtectTest::OnURLFetchComplete(const net::URLFetcher* source) { | |
536 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | |
537 if (source->GetResponseCode() >= 500) { | |
538 // Now running ServerUnavailable test. | |
539 // It takes more than 1 second to finish all 11 requests. | |
540 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | |
541 EXPECT_TRUE(source->GetStatus().is_success()); | |
542 std::string data; | |
543 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
544 EXPECT_FALSE(data.empty()); | |
545 CleanupAfterFetchComplete(); | |
546 } else { | |
547 // Now running Overload test. | |
548 static int count = 0; | |
549 count++; | |
550 if (count < 20) { | |
551 fetcher_->SetRequestContext( | |
552 new ThrottlingTestURLRequestContextGetter( | |
553 io_message_loop_proxy(), request_context())); | |
554 fetcher_->Start(); | |
555 } else { | |
556 // We have already sent 20 requests continuously. And we expect that | |
557 // it takes more than 1 second due to the overload protection settings. | |
558 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | |
559 URLFetcherTest::OnURLFetchComplete(source); | |
560 } | |
561 } | |
562 } | |
563 | |
564 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | |
565 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
566 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
567 io_message_loop_proxy(), request_context())); | |
568 fetcher_->SetAutomaticallyRetryOn5xx(false); | |
569 start_time_ = Time::Now(); | |
570 fetcher_->SetMaxRetries(11); | |
571 fetcher_->Start(); | |
572 } | |
573 | |
574 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | |
575 const net::URLFetcher* source) { | |
576 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); | |
577 if (source->GetResponseCode() >= 500) { | |
578 // Now running ServerUnavailable test. | |
579 // It should get here on the first attempt, so almost immediately and | |
580 // *not* to attempt to execute all 11 requests (2.5 minutes). | |
581 EXPECT_TRUE(Time::Now() - start_time_ < one_minute); | |
582 EXPECT_TRUE(source->GetStatus().is_success()); | |
583 // Check that suggested back off time is bigger than 0. | |
584 EXPECT_GT(fetcher_->GetBackoffDelay().InMicroseconds(), 0); | |
585 std::string data; | |
586 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
587 EXPECT_FALSE(data.empty()); | |
588 } else { | |
589 // We should not get here! | |
590 ADD_FAILURE(); | |
591 } | |
592 | |
593 CleanupAfterFetchComplete(); | |
594 } | |
595 | |
596 | |
597 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { | |
598 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | |
599 cert_dir_ = cert_dir_.AppendASCII("chrome"); | |
600 cert_dir_ = cert_dir_.AppendASCII("test"); | |
601 cert_dir_ = cert_dir_.AppendASCII("data"); | |
602 cert_dir_ = cert_dir_.AppendASCII("ssl"); | |
603 cert_dir_ = cert_dir_.AppendASCII("certificates"); | |
604 } | |
605 | |
606 // The "server certificate expired" error should result in automatic | |
607 // cancellation of the request by | |
608 // net::URLRequest::Delegate::OnSSLCertificateError. | |
609 void URLFetcherBadHTTPSTest::OnURLFetchComplete( | |
610 const net::URLFetcher* source) { | |
611 // This part is different from URLFetcherTest::OnURLFetchComplete | |
612 // because this test expects the request to be cancelled. | |
613 EXPECT_EQ(net::URLRequestStatus::CANCELED, source->GetStatus().status()); | |
614 EXPECT_EQ(net::ERR_ABORTED, source->GetStatus().error()); | |
615 EXPECT_EQ(-1, source->GetResponseCode()); | |
616 EXPECT_TRUE(source->GetCookies().empty()); | |
617 std::string data; | |
618 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
619 EXPECT_TRUE(data.empty()); | |
620 CleanupAfterFetchComplete(); | |
621 } | |
622 | |
623 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | |
624 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
625 CancelTestURLRequestContextGetter* context_getter = | |
626 new CancelTestURLRequestContextGetter(io_message_loop_proxy(), | |
627 url); | |
628 fetcher_->SetRequestContext(context_getter); | |
629 fetcher_->SetMaxRetries(2); | |
630 fetcher_->Start(); | |
631 // We need to wait for the creation of the net::URLRequestContext, since we | |
632 // rely on it being destroyed as a signal to end the test. | |
633 context_getter->WaitForContextCreation(); | |
634 CancelRequest(); | |
635 } | |
636 | |
637 void URLFetcherCancelTest::OnURLFetchComplete( | |
638 const net::URLFetcher* source) { | |
639 // We should have cancelled the request before completion. | |
640 ADD_FAILURE(); | |
641 CleanupAfterFetchComplete(); | |
642 } | |
643 | |
644 void URLFetcherCancelTest::CancelRequest() { | |
645 delete fetcher_; | |
646 // The URLFetcher's test context will post a Quit task once it is | |
647 // deleted. So if this test simply hangs, it means cancellation | |
648 // did not work. | |
649 } | |
650 | |
651 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( | |
652 const net::URLFetcher* source) { | |
653 EXPECT_TRUE(source->GetStatus().is_success()); | |
654 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | |
655 std::string data; | |
656 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
657 EXPECT_FALSE(data.empty()); | |
658 if (!data.empty() && data_.empty()) { | |
659 data_ = data; | |
660 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
661 io_message_loop_proxy(), request_context())); | |
662 fetcher_->Start(); | |
663 } else { | |
664 EXPECT_EQ(data, data_); | |
665 CleanupAfterFetchComplete(); | |
666 } | |
667 } | |
668 | |
669 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url, | |
670 const FilePath& file_path) { | |
671 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
672 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
673 io_message_loop_proxy(), request_context())); | |
674 | |
675 // Use the IO message loop to do the file operations in this test. | |
676 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy()); | |
677 fetcher_->Start(); | |
678 } | |
679 | |
680 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) { | |
681 fetcher_ = new URLFetcherImpl(url, net::URLFetcher::GET, this); | |
682 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
683 io_message_loop_proxy(), request_context())); | |
684 | |
685 // Use the IO message loop to do the file operations in this test. | |
686 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | |
687 fetcher_->Start(); | |
688 } | |
689 | |
690 void URLFetcherFileTest::OnURLFetchComplete(const net::URLFetcher* source) { | |
691 if (expected_file_error_ == base::PLATFORM_FILE_OK) { | |
692 EXPECT_TRUE(source->GetStatus().is_success()); | |
693 EXPECT_EQ(source->GetResponseCode(), 200); | |
694 | |
695 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | |
696 EXPECT_FALSE(fetcher_->FileErrorOccurred(&error_code)); | |
697 | |
698 EXPECT_TRUE(source->GetResponseAsFilePath( | |
699 take_ownership_of_file_, &file_path_)); | |
700 | |
701 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, file_path_)); | |
702 } else { | |
703 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | |
704 EXPECT_TRUE(fetcher_->FileErrorOccurred(&error_code)); | |
705 EXPECT_EQ(expected_file_error_, error_code); | |
706 } | |
707 CleanupAfterFetchComplete(); | |
708 } | |
709 | |
710 TEST_F(URLFetcherTest, SameThreadsTest) { | |
711 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
712 net::TestServer::kLocalhost, | |
713 FilePath(kDocRoot)); | |
714 ASSERT_TRUE(test_server.Start()); | |
715 | |
716 // Create the fetcher on the main thread. Since IO will happen on the main | |
717 // thread, this will test URLFetcher's ability to do everything on one | |
718 // thread. | |
719 CreateFetcher(test_server.GetURL("defaultresponse")); | |
720 | |
721 MessageLoop::current()->Run(); | |
722 } | |
723 | |
724 #if defined(OS_MACOSX) | |
725 // SIGSEGV on Mac: http://crbug.com/60426 | |
726 TEST_F(URLFetcherTest, DISABLED_DifferentThreadsTest) { | |
727 #else | |
728 TEST_F(URLFetcherTest, DifferentThreadsTest) { | |
729 #endif | |
730 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
731 net::TestServer::kLocalhost, | |
732 FilePath(kDocRoot)); | |
733 ASSERT_TRUE(test_server.Start()); | |
734 | |
735 // Create a separate thread that will create the URLFetcher. The current | |
736 // (main) thread will do the IO, and when the fetch is complete it will | |
737 // terminate the main thread's message loop; then the other thread's | |
738 // message loop will be shut down automatically as the thread goes out of | |
739 // scope. | |
740 base::Thread t("URLFetcher test thread"); | |
741 ASSERT_TRUE(t.Start()); | |
742 t.message_loop()->PostTask( | |
743 FROM_HERE, | |
744 base::Bind(&URLFetcherTest::CreateFetcher, | |
745 base::Unretained(this), | |
746 test_server.GetURL("defaultresponse"))); | |
747 | |
748 MessageLoop::current()->Run(); | |
749 } | |
750 | |
751 void CancelAllOnIO() { | |
752 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); | |
753 URLFetcherImpl::CancelAll(); | |
754 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); | |
755 } | |
756 | |
757 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | |
758 TEST_F(URLFetcherTest, CancelAll) { | |
759 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
760 net::TestServer::kLocalhost, | |
761 FilePath(kDocRoot)); | |
762 ASSERT_TRUE(test_server.Start()); | |
763 EXPECT_EQ(0, GetNumFetcherCores()); | |
764 | |
765 CreateFetcher(test_server.GetURL("defaultresponse")); | |
766 io_message_loop_proxy()->PostTaskAndReply( | |
767 FROM_HERE, | |
768 base::Bind(&CancelAllOnIO), | |
769 MessageLoop::QuitClosure()); | |
770 MessageLoop::current()->Run(); | |
771 EXPECT_EQ(0, GetNumFetcherCores()); | |
772 delete fetcher_; | |
773 } | |
774 | |
775 #if defined(OS_MACOSX) | |
776 // SIGSEGV on Mac: http://crbug.com/60426 | |
777 TEST_F(URLFetcherPostTest, DISABLED_Basic) { | |
778 #else | |
779 TEST_F(URLFetcherPostTest, Basic) { | |
780 #endif | |
781 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
782 net::TestServer::kLocalhost, | |
783 FilePath(kDocRoot)); | |
784 ASSERT_TRUE(test_server.Start()); | |
785 | |
786 CreateFetcher(test_server.GetURL("echo")); | |
787 MessageLoop::current()->Run(); | |
788 } | |
789 | |
790 #if defined(OS_MACOSX) | |
791 // SIGSEGV on Mac: http://crbug.com/60426 | |
792 TEST_F(URLFetcherUploadProgressTest, DISABLED_Basic) { | |
793 #else | |
794 TEST_F(URLFetcherUploadProgressTest, Basic) { | |
795 #endif | |
796 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
797 net::TestServer::kLocalhost, | |
798 FilePath(kDocRoot)); | |
799 ASSERT_TRUE(test_server.Start()); | |
800 | |
801 CreateFetcher(test_server.GetURL("echo")); | |
802 MessageLoop::current()->Run(); | |
803 } | |
804 | |
805 TEST_F(URLFetcherDownloadProgressTest, Basic) { | |
806 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
807 net::TestServer::kLocalhost, | |
808 FilePath(kDocRoot)); | |
809 ASSERT_TRUE(test_server.Start()); | |
810 | |
811 // Get a file large enough to require more than one read into | |
812 // URLFetcher::Core's IOBuffer. | |
813 static const char kFileToFetch[] = "animate1.gif"; | |
814 file_util::GetFileSize(test_server.document_root().AppendASCII(kFileToFetch), | |
815 &expected_total_); | |
816 CreateFetcher(test_server.GetURL( | |
817 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
818 | |
819 MessageLoop::current()->Run(); | |
820 } | |
821 | |
822 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { | |
823 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
824 net::TestServer::kLocalhost, | |
825 FilePath(kDocRoot)); | |
826 ASSERT_TRUE(test_server.Start()); | |
827 | |
828 // Get a file large enough to require more than one read into | |
829 // URLFetcher::Core's IOBuffer. | |
830 static const char kFileToFetch[] = "animate1.gif"; | |
831 CreateFetcher(test_server.GetURL( | |
832 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
833 | |
834 MessageLoop::current()->Run(); | |
835 } | |
836 | |
837 TEST_F(URLFetcherHeadersTest, Headers) { | |
838 net::TestServer test_server( | |
839 net::TestServer::TYPE_HTTP, | |
840 net::TestServer::kLocalhost, | |
841 FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
842 ASSERT_TRUE(test_server.Start()); | |
843 | |
844 CreateFetcher(test_server.GetURL("files/with-headers.html")); | |
845 MessageLoop::current()->Run(); | |
846 // The actual tests are in the URLFetcherHeadersTest fixture. | |
847 } | |
848 | |
849 TEST_F(URLFetcherSocketAddressTest, SocketAddress) { | |
850 net::TestServer test_server( | |
851 net::TestServer::TYPE_HTTP, | |
852 net::TestServer::kLocalhost, | |
853 FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
854 ASSERT_TRUE(test_server.Start()); | |
855 expected_port_ = test_server.host_port_pair().port(); | |
856 | |
857 // Reusing "with-headers.html" but doesn't really matter. | |
858 CreateFetcher(test_server.GetURL("files/with-headers.html")); | |
859 MessageLoop::current()->Run(); | |
860 // The actual tests are in the URLFetcherSocketAddressTest fixture. | |
861 } | |
862 | |
863 TEST_F(URLFetcherStopOnRedirectTest, StopOnRedirect) { | |
864 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
865 net::TestServer::kLocalhost, | |
866 FilePath(kDocRoot)); | |
867 ASSERT_TRUE(test_server.Start()); | |
868 | |
869 CreateFetcher( | |
870 test_server.GetURL(std::string("server-redirect?") + kRedirectTarget)); | |
871 MessageLoop::current()->Run(); | |
872 EXPECT_TRUE(callback_called_); | |
873 } | |
874 | |
875 TEST_F(URLFetcherProtectTest, Overload) { | |
876 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
877 net::TestServer::kLocalhost, | |
878 FilePath(kDocRoot)); | |
879 ASSERT_TRUE(test_server.Start()); | |
880 | |
881 GURL url(test_server.GetURL("defaultresponse")); | |
882 | |
883 // Registers an entry for test url. It only allows 3 requests to be sent | |
884 // in 200 milliseconds. | |
885 scoped_refptr<net::URLRequestThrottlerEntry> entry( | |
886 new net::URLRequestThrottlerEntry( | |
887 request_context()->throttler_manager(), | |
888 "", 200, 3, 1, 2.0, 0.0, 256)); | |
889 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | |
890 | |
891 CreateFetcher(url); | |
892 | |
893 MessageLoop::current()->Run(); | |
894 } | |
895 | |
896 TEST_F(URLFetcherProtectTest, ServerUnavailable) { | |
897 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
898 net::TestServer::kLocalhost, | |
899 FilePath(kDocRoot)); | |
900 ASSERT_TRUE(test_server.Start()); | |
901 | |
902 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
903 | |
904 // Registers an entry for test url. The backoff time is calculated by: | |
905 // new_backoff = 2.0 * old_backoff + 0 | |
906 // and maximum backoff time is 256 milliseconds. | |
907 // Maximum retries allowed is set to 11. | |
908 scoped_refptr<net::URLRequestThrottlerEntry> entry( | |
909 new net::URLRequestThrottlerEntry( | |
910 request_context()->throttler_manager(), | |
911 "", 200, 3, 1, 2.0, 0.0, 256)); | |
912 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | |
913 | |
914 CreateFetcher(url); | |
915 | |
916 MessageLoop::current()->Run(); | |
917 } | |
918 | |
919 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { | |
920 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
921 net::TestServer::kLocalhost, | |
922 FilePath(kDocRoot)); | |
923 ASSERT_TRUE(test_server.Start()); | |
924 | |
925 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
926 | |
927 // Registers an entry for test url. The backoff time is calculated by: | |
928 // new_backoff = 2.0 * old_backoff + 0 | |
929 // and maximum backoff time is 150000 milliseconds. | |
930 // Maximum retries allowed is set to 11. | |
931 scoped_refptr<net::URLRequestThrottlerEntry> entry( | |
932 new net::URLRequestThrottlerEntry( | |
933 request_context()->throttler_manager(), | |
934 "", 200, 3, 100, 2.0, 0.0, 150000)); | |
935 // Total time if *not* for not doing automatic backoff would be 150s. | |
936 // In reality it should be "as soon as server responds". | |
937 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | |
938 | |
939 CreateFetcher(url); | |
940 | |
941 MessageLoop::current()->Run(); | |
942 } | |
943 | |
944 #if defined(OS_MACOSX) | |
945 // SIGSEGV on Mac: http://crbug.com/60426 | |
946 TEST_F(URLFetcherBadHTTPSTest, DISABLED_BadHTTPSTest) { | |
947 #else | |
948 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { | |
949 #endif | |
950 net::TestServer::HTTPSOptions https_options( | |
951 net::TestServer::HTTPSOptions::CERT_EXPIRED); | |
952 net::TestServer test_server(https_options, FilePath(kDocRoot)); | |
953 ASSERT_TRUE(test_server.Start()); | |
954 | |
955 CreateFetcher(test_server.GetURL("defaultresponse")); | |
956 MessageLoop::current()->Run(); | |
957 } | |
958 | |
959 #if defined(OS_MACOSX) | |
960 // SIGSEGV on Mac: http://crbug.com/60426 | |
961 TEST_F(URLFetcherCancelTest, DISABLED_ReleasesContext) { | |
962 #else | |
963 TEST_F(URLFetcherCancelTest, ReleasesContext) { | |
964 #endif | |
965 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
966 net::TestServer::kLocalhost, | |
967 FilePath(kDocRoot)); | |
968 ASSERT_TRUE(test_server.Start()); | |
969 | |
970 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
971 | |
972 // Create a separate thread that will create the URLFetcher. The current | |
973 // (main) thread will do the IO, and when the fetch is complete it will | |
974 // terminate the main thread's message loop; then the other thread's | |
975 // message loop will be shut down automatically as the thread goes out of | |
976 // scope. | |
977 base::Thread t("URLFetcher test thread"); | |
978 ASSERT_TRUE(t.Start()); | |
979 t.message_loop()->PostTask( | |
980 FROM_HERE, | |
981 base::Bind(&URLFetcherCancelTest::CreateFetcher, | |
982 base::Unretained(this), url)); | |
983 | |
984 MessageLoop::current()->Run(); | |
985 } | |
986 | |
987 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { | |
988 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
989 net::TestServer::kLocalhost, | |
990 FilePath(kDocRoot)); | |
991 ASSERT_TRUE(test_server.Start()); | |
992 | |
993 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
994 | |
995 // Register an entry for test url. | |
996 // Using a sliding window of 4 seconds, and max of 1 request, under a fast | |
997 // run we expect to have a 4 second delay when posting the Start task. | |
998 scoped_refptr<net::URLRequestThrottlerEntry> entry( | |
999 new net::URLRequestThrottlerEntry( | |
1000 request_context()->throttler_manager(), | |
1001 "", 4000, 1, 2000, 2.0, 0.0, 4000)); | |
1002 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | |
1003 // Fake that a request has just started. | |
1004 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); | |
1005 | |
1006 // The next request we try to send will be delayed by ~4 seconds. | |
1007 // The slower the test runs, the less the delay will be (since it takes the | |
1008 // time difference from now). | |
1009 | |
1010 base::Thread t("URLFetcher test thread"); | |
1011 ASSERT_TRUE(t.Start()); | |
1012 t.message_loop()->PostTask( | |
1013 FROM_HERE, | |
1014 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); | |
1015 | |
1016 MessageLoop::current()->Run(); | |
1017 } | |
1018 | |
1019 TEST_F(URLFetcherMultipleAttemptTest, SameData) { | |
1020 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1021 net::TestServer::kLocalhost, | |
1022 FilePath(kDocRoot)); | |
1023 ASSERT_TRUE(test_server.Start()); | |
1024 | |
1025 // Create the fetcher on the main thread. Since IO will happen on the main | |
1026 // thread, this will test URLFetcher's ability to do everything on one | |
1027 // thread. | |
1028 CreateFetcher(test_server.GetURL("defaultresponse")); | |
1029 | |
1030 MessageLoop::current()->Run(); | |
1031 } | |
1032 | |
1033 TEST_F(URLFetcherFileTest, SmallGet) { | |
1034 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1035 net::TestServer::kLocalhost, | |
1036 FilePath(kDocRoot)); | |
1037 ASSERT_TRUE(test_server.Start()); | |
1038 | |
1039 ScopedTempDir temp_dir; | |
1040 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
1041 | |
1042 // Get a small file. | |
1043 static const char kFileToFetch[] = "simple.html"; | |
1044 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1045 CreateFetcherForFile( | |
1046 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
1047 temp_dir.path().AppendASCII(kFileToFetch)); | |
1048 | |
1049 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1050 | |
1051 ASSERT_FALSE(file_util::PathExists(file_path_)) | |
1052 << file_path_.value() << " not removed."; | |
1053 } | |
1054 | |
1055 TEST_F(URLFetcherFileTest, LargeGet) { | |
1056 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1057 net::TestServer::kLocalhost, | |
1058 FilePath(kDocRoot)); | |
1059 ASSERT_TRUE(test_server.Start()); | |
1060 | |
1061 ScopedTempDir temp_dir; | |
1062 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
1063 | |
1064 // Get a file large enough to require more than one read into | |
1065 // URLFetcher::Core's IOBuffer. | |
1066 static const char kFileToFetch[] = "animate1.gif"; | |
1067 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1068 CreateFetcherForFile( | |
1069 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
1070 temp_dir.path().AppendASCII(kFileToFetch)); | |
1071 | |
1072 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1073 } | |
1074 | |
1075 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { | |
1076 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1077 net::TestServer::kLocalhost, | |
1078 FilePath(kDocRoot)); | |
1079 ASSERT_TRUE(test_server.Start()); | |
1080 | |
1081 ScopedTempDir temp_dir; | |
1082 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
1083 | |
1084 // Get a small file. | |
1085 static const char kFileToFetch[] = "simple.html"; | |
1086 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1087 CreateFetcherForFile( | |
1088 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
1089 temp_dir.path().AppendASCII(kFileToFetch)); | |
1090 | |
1091 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1092 | |
1093 MessageLoop::current()->RunAllPending(); | |
1094 ASSERT_FALSE(file_util::PathExists(file_path_)) | |
1095 << file_path_.value() << " not removed."; | |
1096 } | |
1097 | |
1098 | |
1099 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { | |
1100 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1101 net::TestServer::kLocalhost, | |
1102 FilePath(kDocRoot)); | |
1103 ASSERT_TRUE(test_server.Start()); | |
1104 | |
1105 ScopedTempDir temp_dir; | |
1106 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
1107 | |
1108 // Create a file before trying to fetch. | |
1109 static const char kFileToFetch[] = "simple.html"; | |
1110 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; | |
1111 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | |
1112 const int data_size = arraysize(kData); | |
1113 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); | |
1114 ASSERT_TRUE(file_util::PathExists(file_path_)); | |
1115 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1116 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); | |
1117 | |
1118 // Get a small file. | |
1119 CreateFetcherForFile( | |
1120 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
1121 file_path_); | |
1122 | |
1123 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1124 } | |
1125 | |
1126 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { | |
1127 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1128 net::TestServer::kLocalhost, | |
1129 FilePath(kDocRoot)); | |
1130 ASSERT_TRUE(test_server.Start()); | |
1131 | |
1132 ScopedTempDir temp_dir; | |
1133 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
1134 | |
1135 // Create a directory before trying to fetch. | |
1136 static const char kFileToFetch[] = "simple.html"; | |
1137 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | |
1138 ASSERT_TRUE(file_util::CreateDirectory(file_path_)); | |
1139 ASSERT_TRUE(file_util::PathExists(file_path_)); | |
1140 | |
1141 // Get a small file. | |
1142 expected_file_error_ = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | |
1143 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1144 CreateFetcherForFile( | |
1145 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
1146 file_path_); | |
1147 | |
1148 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1149 | |
1150 MessageLoop::current()->RunAllPending(); | |
1151 } | |
1152 | |
1153 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { | |
1154 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1155 net::TestServer::kLocalhost, | |
1156 FilePath(kDocRoot)); | |
1157 ASSERT_TRUE(test_server.Start()); | |
1158 | |
1159 // Get a small file. | |
1160 static const char kFileToFetch[] = "simple.html"; | |
1161 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1162 CreateFetcherForTempFile( | |
1163 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | |
1164 | |
1165 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1166 | |
1167 ASSERT_FALSE(file_util::PathExists(file_path_)) | |
1168 << file_path_.value() << " not removed."; | |
1169 } | |
1170 | |
1171 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { | |
1172 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1173 net::TestServer::kLocalhost, | |
1174 FilePath(kDocRoot)); | |
1175 ASSERT_TRUE(test_server.Start()); | |
1176 | |
1177 // Get a file large enough to require more than one read into | |
1178 // URLFetcher::Core's IOBuffer. | |
1179 static const char kFileToFetch[] = "animate1.gif"; | |
1180 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1181 CreateFetcherForTempFile(test_server.GetURL( | |
1182 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
1183 | |
1184 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1185 } | |
1186 | |
1187 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { | |
1188 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
1189 net::TestServer::kLocalhost, | |
1190 FilePath(kDocRoot)); | |
1191 ASSERT_TRUE(test_server.Start()); | |
1192 | |
1193 // Get a small file. | |
1194 static const char kFileToFetch[] = "simple.html"; | |
1195 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
1196 CreateFetcherForTempFile(test_server.GetURL( | |
1197 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
1198 | |
1199 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
1200 | |
1201 MessageLoop::current()->RunAllPending(); | |
1202 ASSERT_FALSE(file_util::PathExists(file_path_)) | |
1203 << file_path_.value() << " not removed."; | |
1204 } | |
1205 | |
1206 } // namespace. | |
OLD | NEW |