local_test_server.h revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#ifndef NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
6#define NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
7
8#include <string>
9
10#include "base/file_util.h"
11#include "base/files/scoped_file.h"
12#include "base/process/process_handle.h"
13#include "net/test/spawned_test_server/base_test_server.h"
14
15#if defined(OS_WIN)
16#include "base/win/scoped_handle.h"
17#endif
18
19namespace base {
20class CommandLine;
21}
22
23namespace net {
24
25// The LocalTestServer runs an external Python-based test server in the
26// same machine in which the LocalTestServer runs.
27class LocalTestServer : public BaseTestServer {
28 public:
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);
34
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);
40
41  virtual ~LocalTestServer();
42
43  // Start the test server and block until it's ready. Returns true on success.
44  bool Start() WARN_UNUSED_RESULT;
45
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
49  // test. For example:
50  //
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());
57  //   RunMyTest();
58  //
59  // Returns true on success.
60  bool StartInBackground() WARN_UNUSED_RESULT;
61
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;
65
66  // Stop the server started by Start().
67  bool Stop();
68
69  // Modify PYTHONPATH to contain libraries we need.
70  virtual bool SetPythonPath() const WARN_UNUSED_RESULT;
71
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
75      WARN_UNUSED_RESULT;
76
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
80      WARN_UNUSED_RESULT;
81
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(); };
85
86 private:
87  bool Init(const base::FilePath& document_root);
88
89  // Launches the Python test server. Returns true on success.
90  bool LaunchPython(const base::FilePath& testserver_path) WARN_UNUSED_RESULT;
91
92  // Waits for the server to start. Returns true on success.
93  bool WaitToStart() WARN_UNUSED_RESULT;
94
95  // Handle of the Python process running the test server.
96  base::ProcessHandle process_handle_;
97
98#if defined(OS_WIN)
99  // The pipe file handle we read from.
100  base::win::ScopedHandle child_read_fd_;
101
102  // The pipe file handle the child and we write to.
103  base::win::ScopedHandle child_write_fd_;
104#endif
105
106#if defined(OS_POSIX)
107  // The file descriptor the child writes to when it starts.
108  base::ScopedFD child_fd_;
109#endif
110
111  DISALLOW_COPY_AND_ASSIGN(LocalTestServer);
112};
113
114}  // namespace net
115
116#endif  // NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
117