| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | |
| 13 | 12 |
| 14 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 15 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 16 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 17 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 18 #include "base/platform_thread.h" | 17 #include "base/platform_thread.h" |
| 19 #include "base/process_util.h" | 18 #include "base/process_util.h" |
| 20 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 21 #include "base/thread.h" | 20 #include "base/thread.h" |
| 22 #include "base/time.h" | 21 #include "base/time.h" |
| 23 #include "base/waitable_event.h" | 22 #include "base/waitable_event.h" |
| 24 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 25 #include "net/http/http_network_layer.h" | 24 #include "net/http/http_network_layer.h" |
| 26 #include "net/url_request/url_request.h" | 25 #include "net/url_request/url_request.h" |
| 27 #include "net/proxy/proxy_service.h" | 26 #include "net/proxy/proxy_service.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "googleurl/src/url_util.h" | 28 #include "googleurl/src/url_util.h" |
| 30 | 29 |
| 31 const int kHTTPDefaultPort = 1337; | 30 const int kDefaultPort = 1337; |
| 32 const int kFTPDefaultPort = 1338; | |
| 33 | |
| 34 const std::string kDefaultHostName("localhost"); | 31 const std::string kDefaultHostName("localhost"); |
| 35 | 32 |
| 36 // This URLRequestContext does not use a local cache. | 33 // This URLRequestContext does not use a local cache. |
| 37 class TestURLRequestContext : public URLRequestContext { | 34 class TestURLRequestContext : public URLRequestContext { |
| 38 public: | 35 public: |
| 39 TestURLRequestContext() { | 36 TestURLRequestContext() { |
| 40 proxy_service_ = net::ProxyService::CreateNull(); | 37 proxy_service_ = net::ProxyService::CreateNull(); |
| 41 http_transaction_factory_ = | 38 http_transaction_factory_ = |
| 42 net::HttpNetworkLayer::CreateFactory(proxy_service_); | 39 net::HttpNetworkLayer::CreateFactory(proxy_service_); |
| 43 } | 40 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 int received_bytes_count_; | 183 int received_bytes_count_; |
| 187 int received_redirect_count_; | 184 int received_redirect_count_; |
| 188 bool received_data_before_response_; | 185 bool received_data_before_response_; |
| 189 bool request_failed_; | 186 bool request_failed_; |
| 190 std::string data_received_; | 187 std::string data_received_; |
| 191 | 188 |
| 192 // our read buffer | 189 // our read buffer |
| 193 char buf_[4096]; | 190 char buf_[4096]; |
| 194 }; | 191 }; |
| 195 | 192 |
| 196 // This object bounds the lifetime of an external python-based HTTP/FTP server | 193 // This object bounds the lifetime of an external python-based HTTP server |
| 197 // that can provide various responses useful for testing. | 194 // that can provide various responses useful for testing. |
| 198 class BaseTestServer : public base::ProcessFilter, | 195 class TestServer : public base::ProcessFilter { |
| 199 public base::RefCounted<BaseTestServer> { | 196 public: |
| 200 protected: | 197 TestServer(const std::wstring& document_root) |
| 201 BaseTestServer() | 198 : process_handle_(NULL), |
| 202 : process_handle_(NULL) { | 199 is_shutdown_(true) { |
| 200 Init(kDefaultHostName, kDefaultPort, document_root, std::wstring()); |
| 203 } | 201 } |
| 204 | 202 |
| 205 public: | 203 virtual ~TestServer() { |
| 206 virtual ~BaseTestServer() { | 204 Shutdown(); |
| 207 if (process_handle_) { | |
| 208 #if defined(OS_WIN) | |
| 209 CloseHandle(process_handle_); | |
| 210 #endif | |
| 211 process_handle_ = NULL; | |
| 212 } | |
| 213 // Make sure we don't leave any stray testserver processes laying around. | |
| 214 std::wstring testserver_name = | |
| 215 file_util::GetFilenameFromPath(python_runtime_); | |
| 216 base::CleanupProcesses(testserver_name, 10000, 1, this); | |
| 217 EXPECT_EQ(0, base::GetProcessCount(testserver_name, this)); | |
| 218 } | 205 } |
| 219 | 206 |
| 220 // Implementation of ProcessFilter | 207 // Implementation of ProcessFilter |
| 221 virtual bool Includes(uint32 pid, uint32 parent_pid) const { | 208 virtual bool Includes(uint32 pid, uint32 parent_pid) const { |
| 222 // Since no process handle is set, it can't be included in the filter. | 209 // This function may be called after Shutdown(), in which process_handle_ is |
| 210 // set to NULL. Since no process handle is set, it can't be included in the |
| 211 // filter. |
| 223 if (!process_handle_) | 212 if (!process_handle_) |
| 224 return false; | 213 return false; |
| 225 // TODO(port): rationalize return value of GetProcId | 214 // TODO(port): rationalize return value of GetProcId |
| 226 return pid == static_cast<uint32>(base::GetProcId(process_handle_)); | 215 return pid == (uint32)base::GetProcId(process_handle_); |
| 227 } | 216 } |
| 228 | 217 |
| 229 GURL TestServerPage(const std::string& path) { | 218 GURL TestServerPage(const std::string& path) { |
| 230 return GURL(base_address_ + path); | 219 return GURL(base_address_ + path); |
| 231 } | 220 } |
| 232 | 221 |
| 233 GURL TestServerPageW(const std::wstring& path) { | 222 GURL TestServerPageW(const std::wstring& path) { |
| 234 return GURL(base_address_ + WideToUTF8(path)); | 223 return GURL(base_address_ + WideToUTF8(path)); |
| 235 } | 224 } |
| 236 | 225 |
| 237 void SetPythonPaths() { | 226 // A subclass may wish to send the request in a different manner |
| 227 virtual bool MakeGETRequest(const std::string& page_name) { |
| 228 const GURL& url = TestServerPage(page_name); |
| 229 |
| 230 // Spin up a background thread for this request so that we have access to |
| 231 // an IO message loop, and in cases where this thread already has an IO |
| 232 // message loop, we also want to avoid spinning a nested message loop. |
| 233 |
| 234 SyncTestDelegate d; |
| 235 { |
| 236 base::Thread io_thread("MakeGETRequest"); |
| 237 base::Thread::Options options; |
| 238 options.message_loop_type = MessageLoop::TYPE_IO; |
| 239 io_thread.StartWithOptions(options); |
| 240 io_thread.message_loop()->PostTask(FROM_HERE, NewRunnableFunction( |
| 241 &TestServer::StartGETRequest, url, &d)); |
| 242 d.Wait(); |
| 243 } |
| 244 return d.did_succeed(); |
| 245 } |
| 246 |
| 247 bool init_successful() const { return init_successful_; } |
| 248 |
| 249 protected: |
| 250 struct ManualInit {}; |
| 251 |
| 252 // Used by subclasses that need to defer initialization until they are fully |
| 253 // constructed. The subclass should call Init once it is ready (usually in |
| 254 // its constructor). |
| 255 TestServer(ManualInit) |
| 256 : process_handle_(NULL), |
| 257 init_successful_(false), |
| 258 is_shutdown_(true) { |
| 259 } |
| 260 |
| 261 virtual std::string scheme() { return std::string("http"); } |
| 262 |
| 263 // This is in a separate function so that we can have assertions and so that |
| 264 // subclasses can call this later. |
| 265 void Init(const std::string& host_name, int port, |
| 266 const std::wstring& document_root, |
| 267 const std::wstring& cert_path) { |
| 268 std::stringstream ss; |
| 269 std::string port_str; |
| 270 ss << (port ? port : kDefaultPort); |
| 271 ss >> port_str; |
| 272 base_address_ = scheme() + "://" + host_name + ":" + port_str + "/"; |
| 273 |
| 274 std::wstring testserver_path; |
| 275 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)); |
| 276 file_util::AppendToPath(&testserver_path, L"net"); |
| 277 file_util::AppendToPath(&testserver_path, L"tools"); |
| 278 file_util::AppendToPath(&testserver_path, L"testserver"); |
| 279 file_util::AppendToPath(&testserver_path, L"testserver.py"); |
| 280 |
| 281 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &python_runtime_)); |
| 282 file_util::AppendToPath(&python_runtime_, L"third_party"); |
| 283 file_util::AppendToPath(&python_runtime_, L"python_24"); |
| 284 file_util::AppendToPath(&python_runtime_, L"python.exe"); |
| 285 |
| 286 std::wstring test_data_directory; |
| 287 PathService::Get(base::DIR_SOURCE_ROOT, &test_data_directory); |
| 288 std::wstring normalized_document_root = document_root; |
| 238 #if defined(OS_WIN) | 289 #if defined(OS_WIN) |
| 239 // Set up PYTHONPATH so that Python is able to find the in-tree copy of | 290 std::replace(normalized_document_root.begin(), |
| 240 // pyftpdlib. | 291 normalized_document_root.end(), |
| 241 static bool set_python_path = false; | 292 L'/', FilePath::kSeparators[0]); |
| 242 if (!set_python_path) { | 293 #endif |
| 243 FilePath pyftpdlib_path; | 294 file_util::AppendToPath(&test_data_directory, normalized_document_root); |
| 244 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &pyftpdlib_path)); | |
| 245 pyftpdlib_path = pyftpdlib_path.Append(L"third_party"); | |
| 246 pyftpdlib_path = pyftpdlib_path.Append(L"pyftpdlib"); | |
| 247 | 295 |
| 248 const wchar_t kPythonPath[] = L"PYTHONPATH"; | 296 #if defined(OS_WIN) |
| 249 wchar_t python_path_c[1024]; | 297 std::wstring command_line = |
| 250 if (GetEnvironmentVariable(kPythonPath, python_path_c, 1023) > 0) { | 298 L"\"" + python_runtime_ + L"\" " + L"\"" + testserver_path + |
| 251 // PYTHONPATH is already set, append to it. | 299 L"\" --port=" + UTF8ToWide(port_str) + L" --data-dir=\"" + |
| 252 std::wstring python_path(python_path_c); | 300 test_data_directory + L"\""; |
| 253 python_path.append(L":"); | 301 if (!cert_path.empty()) { |
| 254 python_path.append(pyftpdlib_path.value()); | 302 command_line.append(L" --https=\""); |
| 255 SetEnvironmentVariableW(kPythonPath, python_path.c_str()); | 303 command_line.append(cert_path); |
| 256 } else { | 304 command_line.append(L"\""); |
| 257 SetEnvironmentVariableW(kPythonPath, pyftpdlib_path.value().c_str()); | 305 } |
| 258 } | |
| 259 | 306 |
| 260 set_python_path = true; | 307 ASSERT_TRUE( |
| 261 } | 308 base::LaunchApp(command_line, false, true, &process_handle_)) << |
| 309 "Failed to launch " << command_line; |
| 262 #elif defined(OS_POSIX) | 310 #elif defined(OS_POSIX) |
| 263 // Set up PYTHONPATH so that Python is able to find the in-tree copy of | 311 // Set up PYTHONPATH so that Python is able to find the in-tree copy of |
| 264 // tlslite and pyftpdlib. | 312 // tlslite. |
| 313 |
| 265 static bool set_python_path = false; | 314 static bool set_python_path = false; |
| 266 if (!set_python_path) { | 315 if (!set_python_path) { |
| 267 FilePath tlslite_path; | 316 FilePath tlslite_path; |
| 268 FilePath pyftpdlib_path; | |
| 269 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &tlslite_path)); | 317 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &tlslite_path)); |
| 270 tlslite_path = tlslite_path.Append("third_party"); | 318 tlslite_path = tlslite_path.Append("third_party"); |
| 271 tlslite_path = tlslite_path.Append("tlslite"); | 319 tlslite_path = tlslite_path.Append("tlslite"); |
| 272 | 320 |
| 273 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &pyftpdlib_path)); | |
| 274 pyftpdlib_path = pyftpdlib_path.Append("third_party"); | |
| 275 pyftpdlib_path = pyftpdlib_path.Append("pyftpdlib"); | |
| 276 | |
| 277 const char kPythonPath[] = "PYTHONPATH"; | 321 const char kPythonPath[] = "PYTHONPATH"; |
| 278 char* python_path_c = getenv(kPythonPath); | 322 char* python_path_c = getenv(kPythonPath); |
| 279 if (python_path_c) { | 323 if (python_path_c) { |
| 280 // PYTHONPATH is already set, append to it. | 324 // PYTHONPATH is already set, append to it. |
| 281 std::string python_path(python_path_c); | 325 std::string python_path(python_path_c); |
| 282 python_path.append(":"); | 326 python_path.append(":"); |
| 283 python_path.append(tlslite_path.value()); | 327 python_path.append(tlslite_path.value()); |
| 284 python_path.append(":"); | |
| 285 python_path.append(pyftpdlib_path.value()); | |
| 286 setenv(kPythonPath, python_path.c_str(), 1); | 328 setenv(kPythonPath, python_path.c_str(), 1); |
| 287 } else { | 329 } else { |
| 288 std::string python_path = tlslite_path.value().c_str(); | 330 setenv(kPythonPath, tlslite_path.value().c_str(), 1); |
| 289 python_path.append(":"); | |
| 290 python_path.append(pyftpdlib_path.value()); | |
| 291 setenv(kPythonPath, python_path.c_str(), 1); | |
| 292 } | 331 } |
| 332 |
| 293 set_python_path = true; | 333 set_python_path = true; |
| 294 } | 334 } |
| 295 #endif | |
| 296 } | |
| 297 | 335 |
| 298 void SetAppPath(const std::string& host_name, int port, | 336 std::vector<std::string> command_line; |
| 299 const std::wstring& document_root, const std::string& scheme, | 337 command_line.push_back("python"); |
| 300 std::wstring* testserver_path, std::wstring* test_data_directory) { | 338 command_line.push_back(WideToUTF8(testserver_path)); |
| 301 port_str_ = IntToString(port); | 339 command_line.push_back("--port=" + port_str); |
| 302 if (url_user_.empty()) { | 340 command_line.push_back("--data-dir=" + WideToUTF8(test_data_directory)); |
| 303 base_address_ = scheme + "://" + host_name + ":" + port_str_ + "/"; | 341 if (!cert_path.empty()) |
| 304 } else { | 342 command_line.push_back("--https=" + WideToUTF8(cert_path)); |
| 305 if (url_password_.empty()) | |
| 306 base_address_ = scheme + "://" + url_user_ + "@" + | |
| 307 host_name + ":" + port_str_ + "/"; | |
| 308 else | |
| 309 base_address_ = scheme + "://" + url_user_ + ":" + url_password_ + | |
| 310 "@" + host_name + ":" + port_str_ + "/"; | |
| 311 } | |
| 312 | 343 |
| 313 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, testserver_path)); | 344 base::file_handle_mapping_vector no_mappings; |
| 314 file_util::AppendToPath(testserver_path, L"net"); | 345 ASSERT_TRUE( |
| 315 file_util::AppendToPath(testserver_path, L"tools"); | 346 base::LaunchApp(command_line, no_mappings, false, &process_handle_)) << |
| 316 file_util::AppendToPath(testserver_path, L"testserver"); | 347 "Failed to launch " << command_line[0] << " ..."; |
| 317 file_util::AppendToPath(testserver_path, L"testserver.py"); | |
| 318 | |
| 319 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &python_runtime_)); | |
| 320 file_util::AppendToPath(&python_runtime_, L"third_party"); | |
| 321 file_util::AppendToPath(&python_runtime_, L"python_24"); | |
| 322 file_util::AppendToPath(&python_runtime_, L"python.exe"); | |
| 323 | |
| 324 PathService::Get(base::DIR_SOURCE_ROOT, test_data_directory); | |
| 325 std::wstring normalized_document_root = document_root; | |
| 326 | |
| 327 #if defined(OS_WIN) | |
| 328 // It is just for windows only and have no effect on other OS | |
| 329 std::replace(normalized_document_root.begin(), | |
| 330 normalized_document_root.end(), | |
| 331 L'/', FilePath::kSeparators[0]); | |
| 332 #endif | |
| 333 if (!normalized_document_root.empty()) | |
| 334 file_util::AppendToPath(test_data_directory, normalized_document_root); | |
| 335 | |
| 336 } | |
| 337 | |
| 338 #if defined(OS_WIN) | |
| 339 void LaunchApp(const std::wstring& command_line) { | |
| 340 ASSERT_TRUE(base::LaunchApp(command_line, false, true, &process_handle_)) << | |
| 341 "Failed to launch " << command_line; | |
| 342 } | |
| 343 #elif defined(OS_POSIX) | |
| 344 void LaunchApp(const std::vector<std::string>& command_line) { | |
| 345 ASSERT_TRUE(base::LaunchApp(command_line, false, true, &process_handle_)) << | |
| 346 "Failed to launch " << command_line[0] << " ..."; | |
| 347 } | |
| 348 #endif | 348 #endif |
| 349 | 349 |
| 350 virtual bool MakeGETRequest(const std::string& page_name) = 0; | 350 // Verify that the webserver is actually started. |
| 351 | 351 // Otherwise tests can fail if they run faster than Python can start. |
| 352 // Verify that the Server is actually started. | |
| 353 // Otherwise tests can fail if they run faster than Python can start. | |
| 354 bool VerifyLaunchApp(const std::string& page_name) { | |
| 355 int retries = 10; | 352 int retries = 10; |
| 356 bool success; | 353 bool success; |
| 357 while ((success = MakeGETRequest(page_name)) == false && retries > 0) { | 354 while ((success = MakeGETRequest("hello.html")) == false && retries > 0) { |
| 358 retries--; | 355 retries--; |
| 359 PlatformThread::Sleep(500); | 356 PlatformThread::Sleep(500); |
| 360 } | 357 } |
| 361 if (!success) | 358 ASSERT_TRUE(success) << "Webserver not starting properly."; |
| 362 return false; | 359 |
| 363 return true; | 360 init_successful_ = true; |
| 361 is_shutdown_ = false; |
| 364 } | 362 } |
| 365 | 363 |
| 366 protected: | 364 void Shutdown() { |
| 365 if (is_shutdown_) |
| 366 return; |
| 367 |
| 368 // here we append the time to avoid problems where the kill page |
| 369 // is being cached rather than being executed on the server |
| 370 std::ostringstream page_name; |
| 371 page_name << "kill?" << (unsigned int)(base::Time::Now().ToInternalValue()); |
| 372 int retry_count = 5; |
| 373 while (retry_count > 0) { |
| 374 bool r = MakeGETRequest(page_name.str()); |
| 375 // BUG #1048625 causes the kill GET to fail. For now we just retry. |
| 376 // Once the bug is fixed, we should remove the while loop and put back |
| 377 // the following DCHECK. |
| 378 // DCHECK(r); |
| 379 if (r) |
| 380 break; |
| 381 retry_count--; |
| 382 } |
| 383 // Make sure we were successfull in stopping the testserver. |
| 384 DCHECK(retry_count > 0); |
| 385 |
| 386 if (process_handle_) { |
| 387 #if defined(OS_WIN) |
| 388 CloseHandle(process_handle_); |
| 389 #endif |
| 390 process_handle_ = NULL; |
| 391 } |
| 392 |
| 393 // Make sure we don't leave any stray testserver processes laying around. |
| 394 std::wstring testserver_name = |
| 395 file_util::GetFilenameFromPath(python_runtime_); |
| 396 base::CleanupProcesses(testserver_name, 10000, 1, this); |
| 397 EXPECT_EQ(0, base::GetProcessCount(testserver_name, this)); |
| 398 |
| 399 is_shutdown_ = true; |
| 400 } |
| 401 |
| 402 private: |
| 367 // Used by MakeGETRequest to implement sync load behavior. | 403 // Used by MakeGETRequest to implement sync load behavior. |
| 368 class SyncTestDelegate : public TestDelegate { | 404 class SyncTestDelegate : public TestDelegate { |
| 369 public: | 405 public: |
| 370 SyncTestDelegate() : event_(false, false), success_(false) { | 406 SyncTestDelegate() : event_(false, false), success_(false) { |
| 371 } | 407 } |
| 372 virtual void OnResponseCompleted(URLRequest* request) { | 408 virtual void OnResponseCompleted(URLRequest* request) { |
| 373 MessageLoop::current()->DeleteSoon(FROM_HERE, request); | 409 MessageLoop::current()->DeleteSoon(FROM_HERE, request); |
| 374 success_ = request->status().is_success(); | 410 success_ = request->status().is_success(); |
| 375 event_.Signal(); | 411 event_.Signal(); |
| 376 } | 412 } |
| 377 void Wait() { event_.Wait(); } | 413 void Wait() { event_.Wait(); } |
| 378 bool did_succeed() const { return success_; } | 414 bool did_succeed() const { return success_; } |
| 379 private: | 415 private: |
| 380 base::WaitableEvent event_; | 416 base::WaitableEvent event_; |
| 381 bool success_; | 417 bool success_; |
| 382 DISALLOW_COPY_AND_ASSIGN(SyncTestDelegate); | 418 DISALLOW_COPY_AND_ASSIGN(SyncTestDelegate); |
| 383 }; | 419 }; |
| 384 | |
| 385 std::string base_address_; | |
| 386 std::string url_user_; | |
| 387 std::string url_password_; | |
| 388 std::wstring python_runtime_; | |
| 389 base::ProcessHandle process_handle_; | |
| 390 std::string port_str_; | |
| 391 }; | |
| 392 | |
| 393 class HTTPTestServer : public BaseTestServer { | |
| 394 protected: | |
| 395 HTTPTestServer() { | |
| 396 } | |
| 397 | |
| 398 public: | |
| 399 static HTTPTestServer* CreateServer(const std::wstring& document_root) { | |
| 400 HTTPTestServer* test_server = new HTTPTestServer(); | |
| 401 if (!test_server->Init(kDefaultHostName, kHTTPDefaultPort, document_root)) { | |
| 402 delete test_server; | |
| 403 return NULL; | |
| 404 } | |
| 405 return test_server; | |
| 406 } | |
| 407 | |
| 408 bool Init(const std::string& host_name, int port, | |
| 409 const std::wstring& document_root) { | |
| 410 std::wstring testserver_path; | |
| 411 std::wstring test_data_directory; | |
| 412 #if defined(OS_WIN) | |
| 413 std::wstring command_line; | |
| 414 #elif defined(OS_POSIX) | |
| 415 std::vector<std::string> command_line; | |
| 416 #endif | |
| 417 | |
| 418 // Set PYTHONPATH for tlslite and pyftpdlib | |
| 419 SetPythonPaths(); | |
| 420 SetAppPath(kDefaultHostName, port, document_root, scheme(), | |
| 421 &testserver_path, &test_data_directory); | |
| 422 SetCommandLineOption(testserver_path, test_data_directory, &command_line); | |
| 423 LaunchApp(command_line); | |
| 424 if (!VerifyLaunchApp("hello.html")) { | |
| 425 LOG(ERROR) << "Webserver not starting properly"; | |
| 426 return false; | |
| 427 } | |
| 428 return true; | |
| 429 } | |
| 430 | |
| 431 // A subclass may wish to send the request in a different manner | |
| 432 virtual bool MakeGETRequest(const std::string& page_name) { | |
| 433 const GURL& url = TestServerPage(page_name); | |
| 434 | |
| 435 // Spin up a background thread for this request so that we have access to | |
| 436 // an IO message loop, and in cases where this thread already has an IO | |
| 437 // message loop, we also want to avoid spinning a nested message loop. | |
| 438 SyncTestDelegate d; | |
| 439 { | |
| 440 base::Thread io_thread("MakeGETRequest"); | |
| 441 base::Thread::Options options; | |
| 442 options.message_loop_type = MessageLoop::TYPE_IO; | |
| 443 io_thread.StartWithOptions(options); | |
| 444 io_thread.message_loop()->PostTask(FROM_HERE, NewRunnableFunction( | |
| 445 &HTTPTestServer::StartGETRequest, url, &d)); | |
| 446 d.Wait(); | |
| 447 } | |
| 448 return d.did_succeed(); | |
| 449 } | |
| 450 | |
| 451 static void StartGETRequest(const GURL& url, URLRequest::Delegate* delegate) { | 420 static void StartGETRequest(const GURL& url, URLRequest::Delegate* delegate) { |
| 452 URLRequest* request = new URLRequest(url, delegate); | 421 URLRequest* request = new URLRequest(url, delegate); |
| 453 request->set_context(new TestURLRequestContext()); | 422 request->set_context(new TestURLRequestContext()); |
| 454 request->set_method("GET"); | 423 request->set_method("GET"); |
| 455 request->Start(); | 424 request->Start(); |
| 456 EXPECT_TRUE(request->is_pending()); | 425 EXPECT_TRUE(request->is_pending()); |
| 457 } | 426 } |
| 458 | 427 |
| 459 virtual ~HTTPTestServer() { | 428 std::string base_address_; |
| 460 // here we append the time to avoid problems where the kill page | 429 std::wstring python_runtime_; |
| 461 // is being cached rather than being executed on the server | 430 base::ProcessHandle process_handle_; |
| 462 std::string page_name = StringPrintf("kill?%u", | 431 bool init_successful_; |
| 463 static_cast<int>(base::Time::Now().ToInternalValue())); | 432 bool is_shutdown_; |
| 464 int retry_count = 5; | 433 }; |
| 465 while (retry_count > 0) { | 434 |
| 466 bool r = MakeGETRequest(page_name); | 435 class HTTPSTestServer : public TestServer { |
| 467 // BUG #1048625 causes the kill GET to fail. For now we just retry. | 436 public: |
| 468 // Once the bug is fixed, we should remove the while loop and put back | 437 HTTPSTestServer(const std::string& host_name, int port, |
| 469 // the following DCHECK. | 438 const std::wstring& document_root, |
| 470 // DCHECK(r); | 439 const std::wstring& cert_path) : TestServer(ManualInit()) { |
| 471 if (r) | 440 Init(host_name, port, document_root, cert_path); |
| 472 break; | |
| 473 retry_count--; | |
| 474 } | |
| 475 // Make sure we were successfull in stopping the testserver. | |
| 476 DCHECK(retry_count > 0); | |
| 477 } | 441 } |
| 478 | 442 |
| 479 virtual std::string scheme() { return "http"; } | 443 virtual std::string scheme() { return std::string("https"); } |
| 480 | |
| 481 #if defined(OS_WIN) | |
| 482 virtual void SetCommandLineOption(const std::wstring& testserver_path, | |
| 483 const std::wstring& test_data_directory, | |
| 484 std::wstring* command_line ) { | |
| 485 command_line->append(L"\"" + python_runtime_ + L"\" " + L"\"" + | |
| 486 testserver_path + L"\" --port=" + UTF8ToWide(port_str_) + | |
| 487 L" --data-dir=\"" + test_data_directory + L"\""); | |
| 488 } | |
| 489 #elif defined(OS_POSIX) | |
| 490 virtual void SetCommandLineOption(const std::wstring& testserver_path, | |
| 491 const std::wstring& test_data_directory, | |
| 492 std::vector<std::string>* command_line) { | |
| 493 command_line->push_back("python"); | |
| 494 command_line->push_back(WideToUTF8(testserver_path)); | |
| 495 command_line->push_back("--port=" + port_str_); | |
| 496 command_line->push_back("--data-dir=" + WideToUTF8(test_data_directory)); | |
| 497 } | |
| 498 #endif | |
| 499 }; | |
| 500 | |
| 501 class HTTPSTestServer : public HTTPTestServer { | |
| 502 protected: | |
| 503 explicit HTTPSTestServer(const std::wstring& cert_path) | |
| 504 : cert_path_(cert_path) { | |
| 505 } | |
| 506 | |
| 507 public: | |
| 508 static HTTPSTestServer* CreateServer(const std::string& host_name, int port, | |
| 509 const std::wstring& document_root, | |
| 510 const std::wstring& cert_path) { | |
| 511 HTTPSTestServer* test_server = new HTTPSTestServer(cert_path); | |
| 512 if (!test_server->Init(host_name, port, document_root)) { | |
| 513 delete test_server; | |
| 514 return NULL; | |
| 515 } | |
| 516 return test_server; | |
| 517 } | |
| 518 | |
| 519 #if defined(OS_WIN) | |
| 520 virtual void SetCommandLineOption(const std::wstring& testserver_path, | |
| 521 const std::wstring& test_data_directory, | |
| 522 std::wstring* command_line ) { | |
| 523 command_line->append(L"\"" + python_runtime_ + L"\" " + L"\"" + | |
| 524 testserver_path + L"\"" + L" --port=" + | |
| 525 UTF8ToWide(port_str_) + L" --data-dir=\"" + | |
| 526 test_data_directory + L"\""); | |
| 527 if (!cert_path_.empty()) { | |
| 528 command_line->append(L" --https=\""); | |
| 529 command_line->append(cert_path_); | |
| 530 command_line->append(L"\""); | |
| 531 } | |
| 532 } | |
| 533 #elif defined(OS_POSIX) | |
| 534 virtual void SetCommandLineOption(const std::wstring& testserver_path, | |
| 535 const std::wstring& test_data_directory, | |
| 536 std::vector<std::string>* command_line) { | |
| 537 command_line->push_back("python"); | |
| 538 command_line->push_back(WideToUTF8(testserver_path)); | |
| 539 command_line->push_back("--port=" + port_str_); | |
| 540 command_line->push_back("--data-dir=" + WideToUTF8(test_data_directory)); | |
| 541 if (!cert_path_.empty()) | |
| 542 command_line->push_back("--https=" + WideToUTF8(cert_path_)); | |
| 543 } | |
| 544 #endif | |
| 545 | |
| 546 virtual std::string scheme() { return "https"; } | |
| 547 | |
| 548 virtual ~HTTPSTestServer() { | |
| 549 } | |
| 550 | |
| 551 protected: | |
| 552 std::wstring cert_path_; | |
| 553 }; | |
| 554 | |
| 555 | |
| 556 class FTPTestServer : public BaseTestServer { | |
| 557 protected: | |
| 558 FTPTestServer() { | |
| 559 } | |
| 560 | |
| 561 public: | |
| 562 FTPTestServer(const std::string& url_user, const std::string& url_password) { | |
| 563 url_user_ = url_user; | |
| 564 url_password_ = url_password; | |
| 565 } | |
| 566 | |
| 567 static FTPTestServer* CreateServer(const std::wstring& document_root) { | |
| 568 FTPTestServer* test_server = new FTPTestServer(); | |
| 569 if (!test_server->Init(kDefaultHostName, kFTPDefaultPort, document_root)) { | |
| 570 delete test_server; | |
| 571 return NULL; | |
| 572 } | |
| 573 return test_server; | |
| 574 } | |
| 575 | |
| 576 static FTPTestServer* CreateServer(const std::wstring& document_root, | |
| 577 const std::string& url_user, | |
| 578 const std::string& url_password) { | |
| 579 FTPTestServer* test_server = new FTPTestServer(url_user, url_password); | |
| 580 if (!test_server->Init(kDefaultHostName, kFTPDefaultPort, document_root)) { | |
| 581 delete test_server; | |
| 582 return NULL; | |
| 583 } | |
| 584 return test_server; | |
| 585 } | |
| 586 | |
| 587 bool Init(const std::string& host_name, int port, | |
| 588 const std::wstring& document_root) { | |
| 589 std::wstring testserver_path; | |
| 590 std::wstring test_data_directory; | |
| 591 | |
| 592 #if defined(OS_WIN) | |
| 593 std::wstring command_line; | |
| 594 #elif defined(OS_POSIX) | |
| 595 std::vector<std::string> command_line; | |
| 596 #endif | |
| 597 | |
| 598 // Set PYTHONPATH for tlslite and pyftpdlib | |
| 599 SetPythonPaths(); | |
| 600 SetAppPath(kDefaultHostName, port, document_root, scheme(), | |
| 601 &testserver_path, &test_data_directory); | |
| 602 SetCommandLineOption(testserver_path, test_data_directory, &command_line); | |
| 603 LaunchApp(command_line); | |
| 604 if (!VerifyLaunchApp("/LICENSE")) { | |
| 605 LOG(ERROR) << "FTPServer not starting properly."; | |
| 606 return false; | |
| 607 } | |
| 608 return true; | |
| 609 } | |
| 610 | |
| 611 virtual ~FTPTestServer() { | |
| 612 int retry_count = 5; | |
| 613 while (retry_count > 0) { | |
| 614 bool r = MakeGETRequest("kill"); | |
| 615 // For some reason, this is failing some of the time on the bots. | |
| 616 // It appears to be a different reason than BUG #1048625 (see above), | |
| 617 // which is a Vista + WinHTTP issue. This failure is on XP and our FTP | |
| 618 // code curerntly uses WinInet. | |
| 619 if (r) | |
| 620 break; | |
| 621 retry_count--; | |
| 622 } | |
| 623 // Make sure we were successfull in stopping the testserver. | |
| 624 DCHECK(retry_count > 0); | |
| 625 } | |
| 626 | |
| 627 virtual std::string scheme() { return "ftp"; } | |
| 628 | |
| 629 virtual bool MakeGETRequest(const std::string& page_name) { | |
| 630 const GURL& url = TestServerPage(page_name); | |
| 631 TestDelegate d; | |
| 632 URLRequest request(url, &d); | |
| 633 request.set_context(new TestURLRequestContext()); | |
| 634 request.set_method("GET"); | |
| 635 request.Start(); | |
| 636 EXPECT_TRUE(request.is_pending()); | |
| 637 | |
| 638 MessageLoop::current()->Run(); | |
| 639 if (request.is_pending()) | |
| 640 return false; | |
| 641 | |
| 642 return true; | |
| 643 } | |
| 644 | |
| 645 #if defined(OS_WIN) | |
| 646 virtual void SetCommandLineOption(const std::wstring& testserver_path, | |
| 647 const std::wstring& test_data_directory, | |
| 648 std::wstring* command_line ) { | |
| 649 command_line->append(L"\"" + python_runtime_ + L"\" " + L"\"" + | |
| 650 testserver_path + L"\"" + L" -f " + L" --port=" + | |
| 651 UTF8ToWide(port_str_) + L" --data-dir=\"" + | |
| 652 test_data_directory + L"\""); | |
| 653 } | |
| 654 #elif defined(OS_POSIX) | |
| 655 virtual void SetCommandLineOption(const std::wstring& testserver_path, | |
| 656 const std::wstring& test_data_directory, | |
| 657 std::vector<std::string>* command_line) { | |
| 658 command_line->push_back("python"); | |
| 659 command_line->push_back(" -f "); | |
| 660 command_line->push_back("--data-dir=" + WideToUTF8(test_data_directory)); | |
| 661 command_line->push_back("--port=" + port_str_); | |
| 662 command_line->push_back("--data-dir=" + WideToUTF8(test_data_directory)); | |
| 663 } | |
| 664 #endif | |
| 665 }; | 444 }; |
| 666 | 445 |
| 667 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 446 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| 447 |
| OLD | NEW |