| 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 <sstream> | 8 #include <sstream> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/platform_thread.h" | 14 #include "base/platform_thread.h" |
| 15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/thread.h" | 17 #include "base/thread.h" |
| 18 #include "base/time.h" |
| 18 #include "base/waitable_event.h" | 19 #include "base/waitable_event.h" |
| 19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 20 #include "net/http/http_network_layer.h" | 21 #include "net/http/http_network_layer.h" |
| 21 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "googleurl/src/url_util.h" |
| 23 | 25 |
| 24 const int kDefaultPort = 1337; | 26 const int kDefaultPort = 1337; |
| 25 const std::string kDefaultHostName("localhost"); | 27 const std::string kDefaultHostName("localhost"); |
| 26 | 28 |
| 27 // This URLRequestContext does not use a local cache. | 29 // This URLRequestContext does not use a local cache. |
| 28 class TestURLRequestContext : public URLRequestContext { | 30 class TestURLRequestContext : public URLRequestContext { |
| 29 public: | 31 public: |
| 30 TestURLRequestContext() { | 32 TestURLRequestContext() { |
| 31 http_transaction_factory_ = net::HttpNetworkLayer::CreateFactory(NULL); | 33 http_transaction_factory_ = net::HttpNetworkLayer::CreateFactory(NULL); |
| 32 } | 34 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 Shutdown(); | 197 Shutdown(); |
| 196 } | 198 } |
| 197 | 199 |
| 198 // Implementation of ProcessFilter | 200 // Implementation of ProcessFilter |
| 199 virtual bool Includes(uint32 pid, uint32 parent_pid) const { | 201 virtual bool Includes(uint32 pid, uint32 parent_pid) const { |
| 200 // This function may be called after Shutdown(), in which process_handle_ is | 202 // This function may be called after Shutdown(), in which process_handle_ is |
| 201 // set to NULL. Since no process handle is set, it can't be included in the | 203 // set to NULL. Since no process handle is set, it can't be included in the |
| 202 // filter. | 204 // filter. |
| 203 if (!process_handle_) | 205 if (!process_handle_) |
| 204 return false; | 206 return false; |
| 205 return pid == process_util::GetProcId(process_handle_); | 207 // TODO(port): rationalize return value of GetProcId |
| 208 return pid == (uint32)process_util::GetProcId(process_handle_); |
| 206 } | 209 } |
| 207 | 210 |
| 208 GURL TestServerPage(const std::string& path) { | 211 GURL TestServerPage(const std::string& path) { |
| 209 return GURL(base_address_ + path); | 212 return GURL(base_address_ + path); |
| 210 } | 213 } |
| 211 | 214 |
| 212 GURL TestServerPageW(const std::wstring& path) { | 215 GURL TestServerPageW(const std::wstring& path) { |
| 213 return GURL(UTF8ToWide(base_address_) + path); | 216 return GURL(base_address_ + WideToUTF8(path)); |
| 214 } | 217 } |
| 215 | 218 |
| 216 // A subclass may wish to send the request in a different manner | 219 // A subclass may wish to send the request in a different manner |
| 217 virtual bool MakeGETRequest(const std::string& page_name) { | 220 virtual bool MakeGETRequest(const std::string& page_name) { |
| 218 const GURL& url = TestServerPage(page_name); | 221 const GURL& url = TestServerPage(page_name); |
| 219 | 222 |
| 220 // Spin up a background thread for this request so that we have access to | 223 // Spin up a background thread for this request so that we have access to |
| 221 // an IO message loop, and in cases where this thread already has an IO | 224 // an IO message loop, and in cases where this thread already has an IO |
| 222 // message loop, we also want to avoid spinning a nested message loop. | 225 // message loop, we also want to avoid spinning a nested message loop. |
| 223 | 226 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 247 | 250 |
| 248 virtual std::string scheme() { return std::string("http"); } | 251 virtual std::string scheme() { return std::string("http"); } |
| 249 | 252 |
| 250 // This is in a separate function so that we can have assertions and so that | 253 // This is in a separate function so that we can have assertions and so that |
| 251 // subclasses can call this later. | 254 // subclasses can call this later. |
| 252 void Init(const std::string& host_name, int port, | 255 void Init(const std::string& host_name, int port, |
| 253 const std::wstring& document_root, | 256 const std::wstring& document_root, |
| 254 const std::wstring& cert_path) { | 257 const std::wstring& cert_path) { |
| 255 std::stringstream ss; | 258 std::stringstream ss; |
| 256 std::string port_str; | 259 std::string port_str; |
| 257 ss << port ? port : kDefaultPort; | 260 ss << (port ? port : kDefaultPort); |
| 258 ss >> port_str; | 261 ss >> port_str; |
| 259 base_address_ = scheme() + "://" + host_name + ":" + port_str + "/"; | 262 base_address_ = scheme() + "://" + host_name + ":" + port_str + "/"; |
| 260 | 263 |
| 261 std::wstring testserver_path; | 264 std::wstring testserver_path; |
| 262 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)); | 265 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)); |
| 263 file_util::AppendToPath(&testserver_path, L"net"); | 266 file_util::AppendToPath(&testserver_path, L"net"); |
| 264 file_util::AppendToPath(&testserver_path, L"tools"); | 267 file_util::AppendToPath(&testserver_path, L"tools"); |
| 265 file_util::AppendToPath(&testserver_path, L"testserver"); | 268 file_util::AppendToPath(&testserver_path, L"testserver"); |
| 266 file_util::AppendToPath(&testserver_path, L"testserver.py"); | 269 file_util::AppendToPath(&testserver_path, L"testserver.py"); |
| 267 | 270 |
| 268 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &python_runtime_)); | 271 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &python_runtime_)); |
| 269 file_util::AppendToPath(&python_runtime_, L"third_party"); | 272 file_util::AppendToPath(&python_runtime_, L"third_party"); |
| 270 file_util::AppendToPath(&python_runtime_, L"python_24"); | 273 file_util::AppendToPath(&python_runtime_, L"python_24"); |
| 271 file_util::AppendToPath(&python_runtime_, L"python.exe"); | 274 file_util::AppendToPath(&python_runtime_, L"python.exe"); |
| 272 | 275 |
| 273 std::wstring test_data_directory; | 276 std::wstring test_data_directory; |
| 274 PathService::Get(base::DIR_SOURCE_ROOT, &test_data_directory); | 277 PathService::Get(base::DIR_SOURCE_ROOT, &test_data_directory); |
| 275 std::wstring normalized_document_root = document_root; | 278 std::wstring normalized_document_root = document_root; |
| 276 std::replace(normalized_document_root.begin(), | 279 std::replace(normalized_document_root.begin(), |
| 277 normalized_document_root.end(), | 280 normalized_document_root.end(), |
| 278 L'/', file_util::kPathSeparator); | 281 L'/', file_util::kPathSeparator); |
| 279 file_util::AppendToPath(&test_data_directory, normalized_document_root); | 282 file_util::AppendToPath(&test_data_directory, normalized_document_root); |
| 280 | 283 |
| 284 #if defined(OS_WIN) |
| 281 std::wstring command_line = | 285 std::wstring command_line = |
| 282 L"\"" + python_runtime_ + L"\" " + L"\"" + testserver_path + | 286 L"\"" + python_runtime_ + L"\" " + L"\"" + testserver_path + |
| 283 L"\" --port=" + UTF8ToWide(port_str) + L" --data-dir=\"" + | 287 L"\" --port=" + UTF8ToWide(port_str) + L" --data-dir=\"" + |
| 284 test_data_directory + L"\""; | 288 test_data_directory + L"\""; |
| 285 if (!cert_path.empty()) { | 289 if (!cert_path.empty()) { |
| 286 command_line.append(L" --https=\""); | 290 command_line.append(L" --https=\""); |
| 287 command_line.append(cert_path); | 291 command_line.append(cert_path); |
| 288 command_line.append(L"\""); | 292 command_line.append(L"\""); |
| 289 } | 293 } |
| 290 | 294 |
| 291 ASSERT_TRUE( | 295 ASSERT_TRUE( |
| 292 process_util::LaunchApp(command_line, false, true, &process_handle_)) << | 296 process_util::LaunchApp(command_line, false, true, &process_handle_)) << |
| 293 "Failed to launch " << command_line; | 297 "Failed to launch " << command_line; |
| 298 #elif defined(OS_LINUX) |
| 299 bool tlslite_installed = !access("/usr/bin/tls.py", X_OK); |
| 300 ASSERT_TRUE(tlslite_installed) << "tlslite not installed? Please run 'pytho
n setup.py install' in third_party/tlslite."; |
| 301 |
| 302 std::vector<std::string> command_line; |
| 303 command_line.push_back("python"); |
| 304 command_line.push_back(WideToUTF8(testserver_path)); |
| 305 command_line.push_back("--port=" + port_str); |
| 306 command_line.push_back("--data-dir=" + WideToUTF8(test_data_directory)); |
| 307 if (!cert_path.empty()) |
| 308 command_line.push_back("--https=" + WideToUTF8(cert_path)); |
| 309 |
| 310 ASSERT_TRUE( |
| 311 process_util::LaunchApp(command_line, false, &process_handle_)) << |
| 312 "Failed to launch " << command_line[0] << " ..."; |
| 313 #endif |
| 294 | 314 |
| 295 // Verify that the webserver is actually started. | 315 // Verify that the webserver is actually started. |
| 296 // Otherwise tests can fail if they run faster than Python can start. | 316 // Otherwise tests can fail if they run faster than Python can start. |
| 297 int retries = 10; | 317 int retries = 10; |
| 298 bool success; | 318 bool success; |
| 299 while ((success = MakeGETRequest("hello.html")) == false && retries > 0) { | 319 while ((success = MakeGETRequest("hello.html")) == false && retries > 0) { |
| 300 retries--; | 320 retries--; |
| 301 PlatformThread::Sleep(500); | 321 PlatformThread::Sleep(500); |
| 302 } | 322 } |
| 303 ASSERT_TRUE(success) << "Webserver not starting properly."; | 323 ASSERT_TRUE(success) << "Webserver not starting properly. (On Linux, you ne
ed to install third_party/tlslite.)"; |
| 304 | 324 |
| 305 is_shutdown_ = false; | 325 is_shutdown_ = false; |
| 306 } | 326 } |
| 307 | 327 |
| 308 void Shutdown() { | 328 void Shutdown() { |
| 309 if (is_shutdown_) | 329 if (is_shutdown_) |
| 310 return; | 330 return; |
| 311 | 331 |
| 312 // here we append the time to avoid problems where the kill page | 332 // here we append the time to avoid problems where the kill page |
| 313 // is being cached rather than being executed on the server | 333 // is being cached rather than being executed on the server |
| 314 std::ostringstream page_name; | 334 std::ostringstream page_name; |
| 315 page_name << "kill?" << GetTickCount(); | 335 page_name << "kill?" << (unsigned int)(base::Time::Now().ToInternalValue()); |
| 316 int retry_count = 5; | 336 int retry_count = 5; |
| 317 while (retry_count > 0) { | 337 while (retry_count > 0) { |
| 318 bool r = MakeGETRequest(page_name.str()); | 338 bool r = MakeGETRequest(page_name.str()); |
| 319 // BUG #1048625 causes the kill GET to fail. For now we just retry. | 339 // BUG #1048625 causes the kill GET to fail. For now we just retry. |
| 320 // Once the bug is fixed, we should remove the while loop and put back | 340 // Once the bug is fixed, we should remove the while loop and put back |
| 321 // the following DCHECK. | 341 // the following DCHECK. |
| 322 // DCHECK(r); | 342 // DCHECK(r); |
| 323 if (r) | 343 if (r) |
| 324 break; | 344 break; |
| 325 retry_count--; | 345 retry_count--; |
| 326 } | 346 } |
| 327 // Make sure we were successfull in stopping the testserver. | 347 // Make sure we were successfull in stopping the testserver. |
| 328 DCHECK(retry_count > 0); | 348 DCHECK(retry_count > 0); |
| 329 | 349 |
| 330 if (process_handle_) { | 350 if (process_handle_) { |
| 351 #if defined(OS_WIN) |
| 331 CloseHandle(process_handle_); | 352 CloseHandle(process_handle_); |
| 353 #endif |
| 332 process_handle_ = NULL; | 354 process_handle_ = NULL; |
| 333 } | 355 } |
| 334 | 356 |
| 335 // Make sure we don't leave any stray testserver processes laying around. | 357 // Make sure we don't leave any stray testserver processes laying around. |
| 336 std::wstring testserver_name = | 358 std::wstring testserver_name = |
| 337 file_util::GetFilenameFromPath(python_runtime_); | 359 file_util::GetFilenameFromPath(python_runtime_); |
| 338 process_util::CleanupProcesses(testserver_name, 10000, 1, this); | 360 process_util::CleanupProcesses(testserver_name, 10000, 1, this); |
| 339 EXPECT_EQ(0, process_util::GetProcessCount(testserver_name, this)); | 361 EXPECT_EQ(0, process_util::GetProcessCount(testserver_name, this)); |
| 340 | 362 |
| 341 is_shutdown_ = true; | 363 is_shutdown_ = true; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 362 static void StartGETRequest(const GURL& url, URLRequest::Delegate* delegate) { | 384 static void StartGETRequest(const GURL& url, URLRequest::Delegate* delegate) { |
| 363 URLRequest* request = new URLRequest(url, delegate); | 385 URLRequest* request = new URLRequest(url, delegate); |
| 364 request->set_context(new TestURLRequestContext()); | 386 request->set_context(new TestURLRequestContext()); |
| 365 request->set_method("GET"); | 387 request->set_method("GET"); |
| 366 request->Start(); | 388 request->Start(); |
| 367 EXPECT_TRUE(request->is_pending()); | 389 EXPECT_TRUE(request->is_pending()); |
| 368 } | 390 } |
| 369 | 391 |
| 370 std::string base_address_; | 392 std::string base_address_; |
| 371 std::wstring python_runtime_; | 393 std::wstring python_runtime_; |
| 372 HANDLE process_handle_; | 394 ProcessHandle process_handle_; |
| 373 bool is_shutdown_; | 395 bool is_shutdown_; |
| 374 }; | 396 }; |
| 375 | 397 |
| 376 class HTTPSTestServer : public TestServer { | 398 class HTTPSTestServer : public TestServer { |
| 377 public: | 399 public: |
| 378 HTTPSTestServer(const std::string& host_name, int port, | 400 HTTPSTestServer(const std::string& host_name, int port, |
| 379 const std::wstring& document_root, | 401 const std::wstring& document_root, |
| 380 const std::wstring& cert_path) : TestServer(ManualInit()) { | 402 const std::wstring& cert_path) : TestServer(ManualInit()) { |
| 381 Init(host_name, port, document_root, cert_path); | 403 Init(host_name, port, document_root, cert_path); |
| 382 } | 404 } |
| 383 | 405 |
| 384 virtual std::string scheme() { return std::string("https"); } | 406 virtual std::string scheme() { return std::string("https"); } |
| 385 }; | 407 }; |
| 386 | 408 |
| 387 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 409 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| 388 | 410 |
| OLD | NEW |