OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
7 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 bool Stop() { | 256 bool Stop() { |
257 return launcher_.Stop(); | 257 return launcher_.Stop(); |
258 } | 258 } |
259 | 259 |
260 GURL TestServerPage(const std::string& base_address, | 260 GURL TestServerPage(const std::string& base_address, |
261 const std::string& path) { | 261 const std::string& path) { |
262 return GURL(base_address + path); | 262 return GURL(base_address + path); |
263 } | 263 } |
264 | 264 |
265 GURL TestServerPage(const std::string& path) { | 265 GURL TestServerPage(const std::string& path) { |
266 return GURL(base_address_ + path); | 266 return GURL(scheme_ + "://" + host_name_ + ":" + port_str_ + "/" + path); |
wtc
2009/08/31 20:38:44
Is there a GURL method that combines these compone
| |
267 } | 267 } |
268 | 268 |
269 GURL TestServerPage(const std::string& path, | |
270 const std::string& user, const std::string& password) { | |
wtc
2009/08/31 20:38:44
This is the second form in the Style Guide:
http:/
| |
271 if (password.empty()) | |
272 return GURL(scheme_ + "://" + user + "@" + | |
273 host_name_ + ":" + port_str_ + "/" + path); | |
274 else | |
wtc
2009/08/31 20:38:44
Nit: don't use else after a return statement.
| |
275 return GURL(scheme_ + "://" + user + ":" + password + | |
276 "@" + host_name_ + ":" + port_str_ + "/" + path); | |
277 } | |
278 | |
279 // Deprecated in favor of TestServerPage. | |
280 // TODO(phajdan.jr): Remove TestServerPageW. | |
269 GURL TestServerPageW(const std::wstring& path) { | 281 GURL TestServerPageW(const std::wstring& path) { |
270 return GURL(base_address_ + WideToUTF8(path)); | 282 return TestServerPage(WideToUTF8(path)); |
271 } | 283 } |
272 | 284 |
273 virtual bool MakeGETRequest(const std::string& page_name) = 0; | 285 virtual bool MakeGETRequest(const std::string& page_name) = 0; |
274 | 286 |
275 std::wstring GetDataDirectory() { | 287 std::wstring GetDataDirectory() { |
276 return launcher_.GetDocumentRootPath().ToWStringHack(); | 288 return launcher_.GetDocumentRootPath().ToWStringHack(); |
277 } | 289 } |
278 | 290 |
279 protected: | 291 protected: |
280 bool Start(net::TestServerLauncher::Protocol protocol, | 292 bool Start(net::TestServerLauncher::Protocol protocol, |
281 const std::string& host_name, int port, | 293 const std::string& host_name, int port, |
282 const FilePath& document_root, | 294 const FilePath& document_root, |
283 const FilePath& cert_path, | 295 const FilePath& cert_path, |
284 const std::wstring& file_root_url) { | 296 const std::wstring& file_root_url) { |
285 std::string blank; | |
286 return Start(protocol, host_name, port, document_root, cert_path, | |
287 file_root_url, blank, blank); | |
288 } | |
289 | |
290 bool Start(net::TestServerLauncher::Protocol protocol, | |
291 const std::string& host_name, int port, | |
292 const FilePath& document_root, | |
293 const FilePath& cert_path, | |
294 const std::wstring& file_root_url, | |
295 const std::string& url_user, | |
296 const std::string& url_password) { | |
297 if (!launcher_.Start(protocol, | 297 if (!launcher_.Start(protocol, |
298 host_name, port, document_root, cert_path, file_root_url)) | 298 host_name, port, document_root, cert_path, file_root_url)) |
299 return false; | 299 return false; |
300 | 300 |
301 std::string scheme; | |
302 if (protocol == net::TestServerLauncher::ProtoFTP) | 301 if (protocol == net::TestServerLauncher::ProtoFTP) |
303 scheme = "ftp"; | 302 scheme_ = "ftp"; |
304 else | 303 else |
305 scheme = "http"; | 304 scheme_ = "http"; |
306 if (!cert_path.empty()) | 305 if (!cert_path.empty()) |
307 scheme.push_back('s'); | 306 scheme_.push_back('s'); |
308 | 307 |
309 std::string port_str = IntToString(port); | 308 host_name_ = host_name; |
310 if (url_user.empty()) { | 309 port_str_ = IntToString(port); |
311 base_address_ = scheme + "://" + host_name + ":" + port_str + "/"; | |
312 } else { | |
313 if (url_password.empty()) | |
314 base_address_ = scheme + "://" + url_user + "@" + | |
315 host_name + ":" + port_str + "/"; | |
316 else | |
317 base_address_ = scheme + "://" + url_user + ":" + url_password + | |
318 "@" + host_name + ":" + port_str + "/"; | |
319 } | |
320 return true; | 310 return true; |
321 } | 311 } |
322 | 312 |
323 // Used by MakeGETRequest to implement sync load behavior. | 313 // Used by MakeGETRequest to implement sync load behavior. |
324 class SyncTestDelegate : public TestDelegate { | 314 class SyncTestDelegate : public TestDelegate { |
325 public: | 315 public: |
326 SyncTestDelegate() : event_(false, false), success_(false) { | 316 SyncTestDelegate() : event_(false, false), success_(false) { |
327 } | 317 } |
328 virtual void OnResponseCompleted(URLRequest* request) { | 318 virtual void OnResponseCompleted(URLRequest* request) { |
329 MessageLoop::current()->DeleteSoon(FROM_HERE, request); | 319 MessageLoop::current()->DeleteSoon(FROM_HERE, request); |
330 success_ = request->status().is_success(); | 320 success_ = request->status().is_success(); |
331 event_.Signal(); | 321 event_.Signal(); |
332 } | 322 } |
333 bool Wait(int64 secs) { | 323 bool Wait(int64 secs) { |
334 TimeDelta td = TimeDelta::FromSeconds(secs); | 324 TimeDelta td = TimeDelta::FromSeconds(secs); |
335 if (event_.TimedWait(td)) | 325 if (event_.TimedWait(td)) |
336 return true; | 326 return true; |
337 return false; | 327 return false; |
338 } | 328 } |
339 bool did_succeed() const { return success_; } | 329 bool did_succeed() const { return success_; } |
340 private: | 330 private: |
341 base::WaitableEvent event_; | 331 base::WaitableEvent event_; |
342 bool success_; | 332 bool success_; |
343 DISALLOW_COPY_AND_ASSIGN(SyncTestDelegate); | 333 DISALLOW_COPY_AND_ASSIGN(SyncTestDelegate); |
344 }; | 334 }; |
345 | 335 |
346 net::TestServerLauncher launcher_; | 336 net::TestServerLauncher launcher_; |
347 std::string base_address_; | 337 std::string scheme_; |
338 std::string host_name_; | |
339 std::string port_str_; | |
348 }; | 340 }; |
349 | 341 |
350 | 342 |
351 // HTTP | 343 // HTTP |
352 class HTTPTestServer : public BaseTestServer { | 344 class HTTPTestServer : public BaseTestServer { |
353 protected: | 345 protected: |
354 explicit HTTPTestServer() : loop_(NULL) { | 346 explicit HTTPTestServer() : loop_(NULL) { |
355 } | 347 } |
356 | 348 |
357 explicit HTTPTestServer(int connection_attempts, int connection_timeout) | 349 explicit HTTPTestServer(int connection_attempts, int connection_timeout) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
412 return NULL; | 404 return NULL; |
413 return test_server; | 405 return test_server; |
414 } | 406 } |
415 | 407 |
416 static bool StartTestServer(HTTPTestServer* server, | 408 static bool StartTestServer(HTTPTestServer* server, |
417 const FilePath& document_root, | 409 const FilePath& document_root, |
418 const FilePath& cert_path, | 410 const FilePath& cert_path, |
419 const std::wstring& file_root_url) { | 411 const std::wstring& file_root_url) { |
420 return server->Start(net::TestServerLauncher::ProtoHTTP, kDefaultHostName, | 412 return server->Start(net::TestServerLauncher::ProtoHTTP, kDefaultHostName, |
421 kHTTPDefaultPort, document_root, cert_path, | 413 kHTTPDefaultPort, document_root, cert_path, |
422 file_root_url, "", ""); | 414 file_root_url); |
423 } | 415 } |
424 | 416 |
425 // A subclass may wish to send the request in a different manner | 417 // A subclass may wish to send the request in a different manner |
426 virtual bool MakeGETRequest(const std::string& page_name) { | 418 virtual bool MakeGETRequest(const std::string& page_name) { |
427 const GURL& url = TestServerPage(page_name); | 419 const GURL& url = TestServerPage(page_name); |
428 | 420 |
429 // Spin up a background thread for this request so that we have access to | 421 // Spin up a background thread for this request so that we have access to |
430 // an IO message loop, and in cases where this thread already has an IO | 422 // an IO message loop, and in cases where this thread already has an IO |
431 // message loop, we also want to avoid spinning a nested message loop. | 423 // message loop, we also want to avoid spinning a nested message loop. |
432 SyncTestDelegate d; | 424 SyncTestDelegate d; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 }; | 562 }; |
571 | 563 |
572 | 564 |
573 class FTPTestServer : public BaseTestServer { | 565 class FTPTestServer : public BaseTestServer { |
574 public: | 566 public: |
575 FTPTestServer() { | 567 FTPTestServer() { |
576 } | 568 } |
577 | 569 |
578 static scoped_refptr<FTPTestServer> CreateServer( | 570 static scoped_refptr<FTPTestServer> CreateServer( |
579 const std::wstring& document_root) { | 571 const std::wstring& document_root) { |
580 std::string blank; | |
581 return CreateServer(document_root, blank, blank); | |
582 } | |
583 | |
584 static scoped_refptr<FTPTestServer> CreateServer( | |
585 const std::wstring& document_root, | |
586 const std::string& url_user, | |
587 const std::string& url_password) { | |
588 scoped_refptr<FTPTestServer> test_server = new FTPTestServer(); | 572 scoped_refptr<FTPTestServer> test_server = new FTPTestServer(); |
589 FilePath docroot = FilePath::FromWStringHack(document_root); | 573 FilePath docroot = FilePath::FromWStringHack(document_root); |
590 FilePath no_cert; | 574 FilePath no_cert; |
591 if (!test_server->Start(net::TestServerLauncher::ProtoFTP, | 575 if (!test_server->Start(net::TestServerLauncher::ProtoFTP, |
592 kDefaultHostName, kFTPDefaultPort, docroot, no_cert, std::wstring(), | 576 kDefaultHostName, kFTPDefaultPort, docroot, no_cert, std::wstring())) { |
593 url_user, url_password)) { | |
594 return NULL; | 577 return NULL; |
595 } | 578 } |
596 return test_server; | 579 return test_server; |
597 } | 580 } |
598 | 581 |
599 virtual bool MakeGETRequest(const std::string& page_name) { | 582 virtual bool MakeGETRequest(const std::string& page_name) { |
600 const GURL& url = TestServerPage(base_address_, page_name); | 583 const GURL& url = TestServerPage(page_name); |
601 TestDelegate d; | 584 TestDelegate d; |
602 URLRequest request(url, &d); | 585 URLRequest request(url, &d); |
603 request.set_context(new TestURLRequestContext()); | 586 request.set_context(new TestURLRequestContext()); |
604 request.set_method("GET"); | 587 request.set_method("GET"); |
605 request.Start(); | 588 request.Start(); |
606 EXPECT_TRUE(request.is_pending()); | 589 EXPECT_TRUE(request.is_pending()); |
607 | 590 |
608 MessageLoop::current()->Run(); | 591 MessageLoop::current()->Run(); |
609 if (request.is_pending()) | 592 if (request.is_pending()) |
610 return false; | 593 return false; |
611 | 594 |
612 return true; | 595 return true; |
613 } | 596 } |
614 }; | 597 }; |
615 | 598 |
616 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 599 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
OLD | NEW |