1 // Copyright 2013 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.
5 #ifndef NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
6 #define NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
10 #include "base/file_util.h"
11 #include "base/process/process_handle.h"
12 #include "net/test/spawned_test_server/base_test_server.h"
15 #include "base/win/scoped_handle.h"
22 // The LocalTestServer runs an external Python-based test server in the
23 // same machine in which the LocalTestServer runs.
24 class LocalTestServer : public BaseTestServer {
26 // Initialize a TestServer listening on a specific host (IP or hostname).
27 // |document_root| must be a relative path under the root tree.
28 LocalTestServer(Type type,
29 const std::string& host,
30 const base::FilePath& document_root);
32 // Initialize a TestServer with a specific set of SSLOptions.
33 // |document_root| must be a relative path under the root tree.
34 LocalTestServer(Type type,
35 const SSLOptions& ssl_options,
36 const base::FilePath& document_root);
38 virtual ~LocalTestServer();
40 // Start the test server and block until it's ready. Returns true on success.
41 bool Start() WARN_UNUSED_RESULT;
43 // Start the test server without blocking. Use this if you need multiple test
44 // servers (such as WebSockets and HTTP, or HTTP and HTTPS). You must call
45 // BlockUntilStarted on all servers your test requires before executing the
48 // // Start the servers in parallel.
49 // ASSERT_TRUE(http_server.StartInBackground());
50 // ASSERT_TRUE(websocket_server.StartInBackground());
51 // // Wait for both servers to be ready.
52 // ASSERT_TRUE(http_server.BlockUntilStarted());
53 // ASSERT_TRUE(websocket_server.BlockUntilStarted());
56 // Returns true on success.
57 bool StartInBackground() WARN_UNUSED_RESULT;
59 // Block until ths test server is ready. Returns true on success. See
60 // StartInBackground() documentation for more information.
61 bool BlockUntilStarted() WARN_UNUSED_RESULT;
63 // Stop the server started by Start().
66 // Modify PYTHONPATH to contain libraries we need.
67 virtual bool SetPythonPath() const WARN_UNUSED_RESULT;
69 // Returns true if the base::FilePath for the testserver python script is
70 // successfully stored in |*testserver_path|.
71 virtual bool GetTestServerPath(base::FilePath* testserver_path) const
74 // Adds the command line arguments for the Python test server to
75 // |command_line|. Returns true on success.
76 virtual bool AddCommandLineArguments(CommandLine* command_line) const
79 // Returns the actual path of document root for test cases. This function
80 // should be called by test cases to retrieve the actual document root path.
81 base::FilePath GetDocumentRoot() const { return document_root(); };
84 bool Init(const base::FilePath& document_root);
86 // Launches the Python test server. Returns true on success.
87 bool LaunchPython(const base::FilePath& testserver_path) WARN_UNUSED_RESULT;
89 // Waits for the server to start. Returns true on success.
90 bool WaitToStart() WARN_UNUSED_RESULT;
92 // Handle of the Python process running the test server.
93 base::ProcessHandle process_handle_;
96 // JobObject used to clean up orphaned child processes.
97 base::win::ScopedHandle job_handle_;
99 // The pipe file handle we read from.
100 base::win::ScopedHandle child_read_fd_;
102 // The pipe file handle the child and we write to.
103 base::win::ScopedHandle child_write_fd_;
106 #if defined(OS_POSIX)
107 // The file descriptor the child writes to when it starts.
109 file_util::ScopedFD child_fd_closer_;
112 DISALLOW_COPY_AND_ASSIGN(LocalTestServer);
117 #endif // NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_