summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-29 21:44:12 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-29 21:44:12 +0000
commit3985ba8a1d1e396d98c4dbec84077d6a67109db9 (patch)
treeaa092101b582f5a9973fc68cce625834a9436c86 /net
parent4ada238a76785ee78396cfb8e5992044c302de1e (diff)
downloadchromium_src-3985ba8a1d1e396d98c4dbec84077d6a67109db9.zip
chromium_src-3985ba8a1d1e396d98c4dbec84077d6a67109db9.tar.gz
chromium_src-3985ba8a1d1e396d98c4dbec84077d6a67109db9.tar.bz2
GTTF: Move more test server code from net/url_request/url_request_unittest.h
to net/test/test_server.h No code changes, just a move. TEST=none BUG=49680 Review URL: http://codereview.chromium.org/3034038 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54201 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/proxy/proxy_resolver_perftest.cc11
-rw-r--r--net/test/test_server.h215
-rw-r--r--net/tools/testserver/run_testserver.cc14
-rw-r--r--net/url_request/url_request_unittest.cc4
-rw-r--r--net/url_request/url_request_unittest.h219
5 files changed, 229 insertions, 234 deletions
diff --git a/net/proxy/proxy_resolver_perftest.cc b/net/proxy/proxy_resolver_perftest.cc
index c7f9c7a..14c4818 100644
--- a/net/proxy/proxy_resolver_perftest.cc
+++ b/net/proxy/proxy_resolver_perftest.cc
@@ -2,12 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/base_paths.h"
+#include "base/file_util.h"
+#include "base/path_service.h"
#include "base/perftimer.h"
#include "base/string_util.h"
#include "net/base/mock_host_resolver.h"
+#include "net/base/net_errors.h"
+#include "net/proxy/proxy_info.h"
#include "net/proxy/proxy_resolver_js_bindings.h"
#include "net/proxy/proxy_resolver_v8.h"
-#include "net/url_request/url_request_unittest.h"
+#include "net/test/test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
@@ -144,7 +149,7 @@ class PacPerfSuiteRunner {
void InitHttpServer() {
DCHECK(!resolver_->expects_pac_bytes());
if (!server_) {
- server_ = HTTPTestServer::CreateServer(
+ server_ = net::HTTPTestServer::CreateServer(
L"net/data/proxy_resolver_perftest");
}
ASSERT_TRUE(server_.get() != NULL);
@@ -175,7 +180,7 @@ class PacPerfSuiteRunner {
net::ProxyResolver* resolver_;
std::string resolver_name_;
- scoped_refptr<HTTPTestServer> server_;
+ scoped_refptr<net::HTTPTestServer> server_;
};
#if defined(OS_WIN)
diff --git a/net/test/test_server.h b/net/test/test_server.h
index 8c3f2f3..20fda58 100644
--- a/net/test/test_server.h
+++ b/net/test/test_server.h
@@ -13,24 +13,27 @@
#include "base/compiler_specific.h"
#include "base/file_path.h"
#include "base/process_util.h"
+#include "base/ref_counted.h"
+#include "base/string_util.h"
+#include "googleurl/src/gurl.h"
#if defined(OS_WIN)
#include "base/scoped_handle_win.h"
#endif
#if defined(USE_NSS)
-#include "base/ref_counted.h"
#include "net/base/x509_certificate.h"
#endif
namespace net {
+const int kHTTPDefaultPort = 1337;
+const int kFTPDefaultPort = 1338;
+
+const char kDefaultHostName[] = "localhost";
+
// This object bounds the lifetime of an external python-based HTTP/HTTPS/FTP
// server that can provide various responses useful for testing.
-// A few basic convenience methods are provided, but no
-// URL handling methods (those belong at a higher layer, e.g. in
-// url_request_unittest.h).
-
class TestServerLauncher {
public:
TestServerLauncher();
@@ -131,6 +134,208 @@ bool LaunchTestServerAsJob(const std::wstring& cmdline,
ScopedHandle* job_handle);
#endif
+// This object bounds the lifetime of an external python-based HTTP/FTP server
+// that can provide various responses useful for testing.
+class BaseTestServer : public base::RefCounted<BaseTestServer> {
+ protected:
+ BaseTestServer() {}
+
+ public:
+ bool WaitToFinish(int milliseconds) {
+ return launcher_.WaitToFinish(milliseconds);
+ }
+
+ bool Stop() {
+ return launcher_.Stop();
+ }
+
+ GURL TestServerPage(const std::string& base_address,
+ const std::string& path) {
+ return GURL(base_address + path);
+ }
+
+ GURL TestServerPage(const std::string& path) {
+ // TODO(phajdan.jr): Check for problems with IPv6.
+ return GURL(scheme_ + "://" + host_name_ + ":" + port_str_ + "/" + path);
+ }
+
+ GURL TestServerPage(const std::string& path,
+ const std::string& user,
+ const std::string& password) {
+ // TODO(phajdan.jr): Check for problems with IPv6.
+
+ if (password.empty())
+ return GURL(scheme_ + "://" + user + "@" +
+ host_name_ + ":" + port_str_ + "/" + path);
+
+ return GURL(scheme_ + "://" + user + ":" + password +
+ "@" + host_name_ + ":" + port_str_ + "/" + path);
+ }
+
+ FilePath GetDataDirectory() {
+ return launcher_.GetDocumentRootPath();
+ }
+
+ protected:
+ friend class base::RefCounted<BaseTestServer>;
+ virtual ~BaseTestServer() { }
+
+ bool Start(net::TestServerLauncher::Protocol protocol,
+ const std::string& host_name, int port,
+ const FilePath& document_root,
+ const FilePath& cert_path,
+ const std::wstring& file_root_url) {
+ if (!launcher_.Start(protocol,
+ host_name, port, document_root, cert_path, file_root_url))
+ return false;
+
+ if (protocol == net::TestServerLauncher::ProtoFTP)
+ scheme_ = "ftp";
+ else
+ scheme_ = "http";
+ if (!cert_path.empty())
+ scheme_.push_back('s');
+
+ host_name_ = host_name;
+ port_str_ = IntToString(port);
+ return true;
+ }
+
+ net::TestServerLauncher launcher_;
+ std::string scheme_;
+ std::string host_name_;
+ std::string port_str_;
+};
+
+class HTTPTestServer : public BaseTestServer {
+ protected:
+ HTTPTestServer() {}
+
+ public:
+ // Creates and returns a new HTTPTestServer.
+ static scoped_refptr<HTTPTestServer> CreateServer(
+ const std::wstring& document_root) {
+ return CreateServerWithFileRootURL(document_root, std::wstring());
+ }
+
+ static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL(
+ const std::wstring& document_root,
+ const std::wstring& file_root_url) {
+ scoped_refptr<HTTPTestServer> test_server(new HTTPTestServer());
+ FilePath no_cert;
+ FilePath docroot = FilePath::FromWStringHack(document_root);
+ if (!StartTestServer(test_server.get(), docroot, no_cert, file_root_url))
+ return NULL;
+ return test_server;
+ }
+
+ static bool StartTestServer(HTTPTestServer* server,
+ const FilePath& document_root,
+ const FilePath& cert_path,
+ const std::wstring& file_root_url) {
+ return server->Start(net::TestServerLauncher::ProtoHTTP, kDefaultHostName,
+ kHTTPDefaultPort, document_root, cert_path,
+ file_root_url);
+ }
+};
+
+class HTTPSTestServer : public HTTPTestServer {
+ protected:
+ HTTPSTestServer() {}
+
+ public:
+ // Create a server with a valid certificate
+ // TODO(dkegel): HTTPSTestServer should not require an instance to specify
+ // stock test certificates
+ static scoped_refptr<HTTPSTestServer> CreateGoodServer(
+ const std::wstring& document_root) {
+ scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
+ FilePath docroot = FilePath::FromWStringHack(document_root);
+ FilePath certpath = test_server->launcher_.GetOKCertPath();
+ if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
+ net::TestServerLauncher::kHostName,
+ net::TestServerLauncher::kOKHTTPSPort,
+ docroot, certpath, std::wstring())) {
+ return NULL;
+ }
+ return test_server;
+ }
+
+ // Create a server with an up to date certificate for the wrong hostname
+ // for this host
+ static scoped_refptr<HTTPSTestServer> CreateMismatchedServer(
+ const std::wstring& document_root) {
+ scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
+ FilePath docroot = FilePath::FromWStringHack(document_root);
+ FilePath certpath = test_server->launcher_.GetOKCertPath();
+ if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
+ net::TestServerLauncher::kMismatchedHostName,
+ net::TestServerLauncher::kOKHTTPSPort,
+ docroot, certpath, std::wstring())) {
+ return NULL;
+ }
+ return test_server;
+ }
+
+ // Create a server with an expired certificate
+ static scoped_refptr<HTTPSTestServer> CreateExpiredServer(
+ const std::wstring& document_root) {
+ scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
+ FilePath docroot = FilePath::FromWStringHack(document_root);
+ FilePath certpath = test_server->launcher_.GetExpiredCertPath();
+ if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
+ net::TestServerLauncher::kHostName,
+ net::TestServerLauncher::kBadHTTPSPort,
+ docroot, certpath, std::wstring())) {
+ return NULL;
+ }
+ return test_server;
+ }
+
+ // Create a server with an arbitrary certificate
+ static scoped_refptr<HTTPSTestServer> CreateServer(
+ const std::string& host_name, int port,
+ const std::wstring& document_root,
+ const std::wstring& cert_path) {
+ scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
+ FilePath docroot = FilePath::FromWStringHack(document_root);
+ FilePath certpath = FilePath::FromWStringHack(cert_path);
+ if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
+ host_name, port, docroot, certpath, std::wstring())) {
+ return NULL;
+ }
+ return test_server;
+ }
+
+ protected:
+ std::wstring cert_path_;
+
+ private:
+ virtual ~HTTPSTestServer() {}
+};
+
+class FTPTestServer : public BaseTestServer {
+ public:
+ FTPTestServer() {
+ }
+
+ static scoped_refptr<FTPTestServer> CreateServer(
+ const std::wstring& document_root) {
+ scoped_refptr<FTPTestServer> test_server = new FTPTestServer();
+ FilePath docroot = FilePath::FromWStringHack(document_root);
+ FilePath no_cert;
+ if (!test_server->Start(net::TestServerLauncher::ProtoFTP,
+ kDefaultHostName, kFTPDefaultPort, docroot, no_cert, std::wstring())) {
+ return NULL;
+ }
+ return test_server;
+ }
+
+ private:
+ ~FTPTestServer() {}
+};
+
+
} // namespace net
#endif // NET_TEST_TEST_SERVER_H_
diff --git a/net/tools/testserver/run_testserver.cc b/net/tools/testserver/run_testserver.cc
index dc16a16..edfecb7 100644
--- a/net/tools/testserver/run_testserver.cc
+++ b/net/tools/testserver/run_testserver.cc
@@ -8,7 +8,7 @@
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop.h"
-#include "net/url_request/url_request_unittest.h"
+#include "net/test/test_server.h"
static void PrintUsage() {
printf("run_testserver --doc-root=relpath [--http|--https|--ftp]\n");
@@ -36,10 +36,10 @@ int main(int argc, const char* argv[]) {
port = net::TestServerLauncher::kOKHTTPSPort;
} else if (command_line->HasSwitch("ftp")) {
protocol = "ftp";
- port = kFTPDefaultPort;
+ port = net::kFTPDefaultPort;
} else {
protocol = "http";
- port = kHTTPDefaultPort;
+ port = net::kHTTPDefaultPort;
}
std::wstring doc_root = command_line->GetSwitchValue("doc-root");
if (doc_root.empty()) {
@@ -49,13 +49,13 @@ int main(int argc, const char* argv[]) {
}
// Launch testserver
- scoped_refptr<BaseTestServer> test_server;
+ scoped_refptr<net::BaseTestServer> test_server;
if (protocol == "https") {
- test_server = HTTPSTestServer::CreateGoodServer(doc_root);
+ test_server = net::HTTPSTestServer::CreateGoodServer(doc_root);
} else if (protocol == "ftp") {
- test_server = FTPTestServer::CreateServer(doc_root);
+ test_server = net::FTPTestServer::CreateServer(doc_root);
} else if (protocol == "http") {
- test_server = HTTPTestServer::CreateServer(doc_root);
+ test_server = net::HTTPTestServer::CreateServer(doc_root);
} else {
NOTREACHED();
}
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc
index bfbd290..b232db4 100644
--- a/net/url_request/url_request_unittest.cc
+++ b/net/url_request/url_request_unittest.cc
@@ -48,6 +48,10 @@
#include "testing/platform_test.h"
using base::Time;
+using net::kHTTPDefaultPort;
+using net::FTPTestServer;
+using net::HTTPTestServer;
+using net::HTTPSTestServer;
namespace {
diff --git a/net/url_request/url_request_unittest.h b/net/url_request/url_request_unittest.h
index 92f527f..1a6faf6 100644
--- a/net/url_request/url_request_unittest.h
+++ b/net/url_request/url_request_unittest.h
@@ -41,11 +41,6 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "googleurl/src/url_util.h"
-const int kHTTPDefaultPort = 1337;
-const int kFTPDefaultPort = 1338;
-
-const std::string kDefaultHostName("localhost");
-
using base::TimeDelta;
//-----------------------------------------------------------------------------
@@ -396,218 +391,4 @@ class TestDelegate : public URLRequest::Delegate {
scoped_refptr<net::IOBuffer> buf_;
};
-//-----------------------------------------------------------------------------
-
-// This object bounds the lifetime of an external python-based HTTP/FTP server
-// that can provide various responses useful for testing.
-class BaseTestServer : public base::RefCounted<BaseTestServer> {
- protected:
- BaseTestServer() {}
-
- public:
- bool WaitToFinish(int milliseconds) {
- return launcher_.WaitToFinish(milliseconds);
- }
-
- bool Stop() {
- return launcher_.Stop();
- }
-
- GURL TestServerPage(const std::string& base_address,
- const std::string& path) {
- return GURL(base_address + path);
- }
-
- GURL TestServerPage(const std::string& path) {
- // TODO(phajdan.jr): Check for problems with IPv6.
- return GURL(scheme_ + "://" + host_name_ + ":" + port_str_ + "/" + path);
- }
-
- GURL TestServerPage(const std::string& path,
- const std::string& user,
- const std::string& password) {
- // TODO(phajdan.jr): Check for problems with IPv6.
-
- if (password.empty())
- return GURL(scheme_ + "://" + user + "@" +
- host_name_ + ":" + port_str_ + "/" + path);
-
- return GURL(scheme_ + "://" + user + ":" + password +
- "@" + host_name_ + ":" + port_str_ + "/" + path);
- }
-
- FilePath GetDataDirectory() {
- return launcher_.GetDocumentRootPath();
- }
-
- protected:
- friend class base::RefCounted<BaseTestServer>;
- virtual ~BaseTestServer() { }
-
- bool Start(net::TestServerLauncher::Protocol protocol,
- const std::string& host_name, int port,
- const FilePath& document_root,
- const FilePath& cert_path,
- const std::wstring& file_root_url) {
- if (!launcher_.Start(protocol,
- host_name, port, document_root, cert_path, file_root_url))
- return false;
-
- if (protocol == net::TestServerLauncher::ProtoFTP)
- scheme_ = "ftp";
- else
- scheme_ = "http";
- if (!cert_path.empty())
- scheme_.push_back('s');
-
- host_name_ = host_name;
- port_str_ = IntToString(port);
- return true;
- }
-
- net::TestServerLauncher launcher_;
- std::string scheme_;
- std::string host_name_;
- std::string port_str_;
-};
-
-//-----------------------------------------------------------------------------
-
-// HTTP
-class HTTPTestServer : public BaseTestServer {
- protected:
- HTTPTestServer() {}
-
- public:
- // Creates and returns a new HTTPTestServer. If |loop| is non-null, requests
- // are serviced on it, otherwise a new thread and message loop are created.
- static scoped_refptr<HTTPTestServer> CreateServer(
- const std::wstring& document_root) {
- return CreateServerWithFileRootURL(document_root, std::wstring());
- }
-
- static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL(
- const std::wstring& document_root,
- const std::wstring& file_root_url) {
- scoped_refptr<HTTPTestServer> test_server(new HTTPTestServer());
- FilePath no_cert;
- FilePath docroot = FilePath::FromWStringHack(document_root);
- if (!StartTestServer(test_server.get(), docroot, no_cert, file_root_url))
- return NULL;
- return test_server;
- }
-
- static bool StartTestServer(HTTPTestServer* server,
- const FilePath& document_root,
- const FilePath& cert_path,
- const std::wstring& file_root_url) {
- return server->Start(net::TestServerLauncher::ProtoHTTP, kDefaultHostName,
- kHTTPDefaultPort, document_root, cert_path,
- file_root_url);
- }
-
- virtual std::string scheme() { return "http"; }
-};
-
-//-----------------------------------------------------------------------------
-
-class HTTPSTestServer : public HTTPTestServer {
- protected:
- explicit HTTPSTestServer() {
- }
-
- public:
- // Create a server with a valid certificate
- // TODO(dkegel): HTTPSTestServer should not require an instance to specify
- // stock test certificates
- static scoped_refptr<HTTPSTestServer> CreateGoodServer(
- const std::wstring& document_root) {
- scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
- FilePath docroot = FilePath::FromWStringHack(document_root);
- FilePath certpath = test_server->launcher_.GetOKCertPath();
- if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
- net::TestServerLauncher::kHostName,
- net::TestServerLauncher::kOKHTTPSPort,
- docroot, certpath, std::wstring())) {
- return NULL;
- }
- return test_server;
- }
-
- // Create a server with an up to date certificate for the wrong hostname
- // for this host
- static scoped_refptr<HTTPSTestServer> CreateMismatchedServer(
- const std::wstring& document_root) {
- scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
- FilePath docroot = FilePath::FromWStringHack(document_root);
- FilePath certpath = test_server->launcher_.GetOKCertPath();
- if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
- net::TestServerLauncher::kMismatchedHostName,
- net::TestServerLauncher::kOKHTTPSPort,
- docroot, certpath, std::wstring())) {
- return NULL;
- }
- return test_server;
- }
-
- // Create a server with an expired certificate
- static scoped_refptr<HTTPSTestServer> CreateExpiredServer(
- const std::wstring& document_root) {
- scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
- FilePath docroot = FilePath::FromWStringHack(document_root);
- FilePath certpath = test_server->launcher_.GetExpiredCertPath();
- if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
- net::TestServerLauncher::kHostName,
- net::TestServerLauncher::kBadHTTPSPort,
- docroot, certpath, std::wstring())) {
- return NULL;
- }
- return test_server;
- }
-
- // Create a server with an arbitrary certificate
- static scoped_refptr<HTTPSTestServer> CreateServer(
- const std::string& host_name, int port,
- const std::wstring& document_root,
- const std::wstring& cert_path) {
- scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
- FilePath docroot = FilePath::FromWStringHack(document_root);
- FilePath certpath = FilePath::FromWStringHack(cert_path);
- if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
- host_name, port, docroot, certpath, std::wstring())) {
- return NULL;
- }
- return test_server;
- }
-
- protected:
- std::wstring cert_path_;
-
- private:
- virtual ~HTTPSTestServer() {}
-};
-
-//-----------------------------------------------------------------------------
-
-class FTPTestServer : public BaseTestServer {
- public:
- FTPTestServer() {
- }
-
- static scoped_refptr<FTPTestServer> CreateServer(
- const std::wstring& document_root) {
- scoped_refptr<FTPTestServer> test_server = new FTPTestServer();
- FilePath docroot = FilePath::FromWStringHack(document_root);
- FilePath no_cert;
- if (!test_server->Start(net::TestServerLauncher::ProtoFTP,
- kDefaultHostName, kFTPDefaultPort, docroot, no_cert, std::wstring())) {
- return NULL;
- }
- return test_server;
- }
-
- private:
- ~FTPTestServer() {}
-};
-
#endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_