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/files/file_util.h"
11 #include "base/files/scoped_file.h"
12 #include "base/process/process.h"
13 #include "net/test/spawned_test_server/base_test_server.h"
16 #include "base/win/scoped_handle.h"
25 // The LocalTestServer runs an external Python-based test server in the
26 // same machine in which the LocalTestServer runs.
27 class LocalTestServer
: public BaseTestServer
{
29 // Initialize a TestServer listening on a specific host (IP or hostname).
30 // |document_root| must be a relative path under the root tree.
31 LocalTestServer(Type type
,
32 const std::string
& host
,
33 const base::FilePath
& document_root
);
35 // Initialize a TestServer with a specific set of SSLOptions.
36 // |document_root| must be a relative path under the root tree.
37 LocalTestServer(Type type
,
38 const SSLOptions
& ssl_options
,
39 const base::FilePath
& document_root
);
41 ~LocalTestServer() override
;
43 // Start the test server and block until it's ready. Returns true on success.
44 bool Start() WARN_UNUSED_RESULT
;
46 // Start the test server without blocking. Use this if you need multiple test
47 // servers (such as WebSockets and HTTP, or HTTP and HTTPS). You must call
48 // BlockUntilStarted on all servers your test requires before executing the
51 // // Start the servers in parallel.
52 // ASSERT_TRUE(http_server.StartInBackground());
53 // ASSERT_TRUE(websocket_server.StartInBackground());
54 // // Wait for both servers to be ready.
55 // ASSERT_TRUE(http_server.BlockUntilStarted());
56 // ASSERT_TRUE(websocket_server.BlockUntilStarted());
59 // Returns true on success.
60 bool StartInBackground() WARN_UNUSED_RESULT
;
62 // Block until ths test server is ready. Returns true on success. See
63 // StartInBackground() documentation for more information.
64 bool BlockUntilStarted() WARN_UNUSED_RESULT
;
66 // Stop the server started by Start().
69 // Modify PYTHONPATH to contain libraries we need.
70 virtual bool SetPythonPath() const WARN_UNUSED_RESULT
;
72 // Returns true if the base::FilePath for the testserver python script is
73 // successfully stored in |*testserver_path|.
74 virtual bool GetTestServerPath(base::FilePath
* testserver_path
) const
77 // Adds the command line arguments for the Python test server to
78 // |command_line|. Returns true on success.
79 virtual bool AddCommandLineArguments(base::CommandLine
* command_line
) const
82 // Returns the actual path of document root for test cases. This function
83 // should be called by test cases to retrieve the actual document root path.
84 base::FilePath
GetDocumentRoot() const { return document_root(); };
87 bool Init(const base::FilePath
& document_root
);
89 // Launches the Python test server. Returns true on success.
90 bool LaunchPython(const base::FilePath
& testserver_path
) WARN_UNUSED_RESULT
;
92 // Waits for the server to start. Returns true on success.
93 bool WaitToStart() WARN_UNUSED_RESULT
;
95 // The Python process running the test server.
96 base::Process process_
;
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.
108 base::ScopedFD child_fd_
;
111 DISALLOW_COPY_AND_ASSIGN(LocalTestServer
);
116 #endif // NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_