OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
6 #define NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 6 #define NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 // ... | 88 // ... |
89 // InProcessBrowserTest::SetUp(); | 89 // InProcessBrowserTest::SetUp(); |
90 // } | 90 // } |
91 // | 91 // |
92 // void SetUpOnMainThread() { | 92 // void SetUpOnMainThread() { |
93 // embedded_test_server()->RestartThreadAndListen(); | 93 // embedded_test_server()->RestartThreadAndListen(); |
94 // } | 94 // } |
95 // | 95 // |
96 class EmbeddedTestServer : public StreamListenSocket::Delegate { | 96 class EmbeddedTestServer : public StreamListenSocket::Delegate { |
97 public: | 97 public: |
98 typedef base::Callback<scoped_ptr<HttpResponse>( | 98 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)> |
99 const HttpRequest& request)> HandleRequestCallback; | 99 HandleRequestCallback; |
100 | 100 |
101 // Creates a http test server. InitializeAndWaitUntilReady() must be called | 101 // Creates a http test server. InitializeAndWaitUntilReady() must be called |
102 // to start the server. | 102 // to start the server. |
103 EmbeddedTestServer(); | 103 EmbeddedTestServer(); |
104 virtual ~EmbeddedTestServer(); | 104 virtual ~EmbeddedTestServer(); |
105 | 105 |
106 // Initializes and waits until the server is ready to accept requests. | 106 // Initializes and waits until the server is ready to accept requests. |
107 bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT; | 107 bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT; |
108 | 108 |
109 // Shuts down the http server and waits until the shutdown is complete. | 109 // Shuts down the http server and waits until the shutdown is complete. |
110 bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT; | 110 bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT; |
111 | 111 |
112 // Checks if the server is started. | 112 // Checks if the server is started. |
113 bool Started() const { | 113 bool Started() const { return listen_socket_.get() != NULL; } |
114 return listen_socket_.get() != NULL; | |
115 } | |
116 | 114 |
117 // Returns the base URL to the server, which looks like | 115 // Returns the base URL to the server, which looks like |
118 // http://127.0.0.1:<port>/, where <port> is the actual port number used by | 116 // http://127.0.0.1:<port>/, where <port> is the actual port number used by |
119 // the server. | 117 // the server. |
120 const GURL& base_url() const { return base_url_; } | 118 const GURL& base_url() const { return base_url_; } |
121 | 119 |
122 // Returns a URL to the server based on the given relative URL, which | 120 // Returns a URL to the server based on the given relative URL, which |
123 // should start with '/'. For example: GetURL("/path?query=foo") => | 121 // should start with '/'. For example: GetURL("/path?query=foo") => |
124 // http://127.0.0.1:<port>/path?query=foo. | 122 // http://127.0.0.1:<port>/path?query=foo. |
125 GURL GetURL(const std::string& relative_url) const; | 123 GURL GetURL(const std::string& relative_url) const; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 virtual void DidAccept(StreamListenSocket* server, | 162 virtual void DidAccept(StreamListenSocket* server, |
165 scoped_ptr<StreamListenSocket> connection) OVERRIDE; | 163 scoped_ptr<StreamListenSocket> connection) OVERRIDE; |
166 virtual void DidRead(StreamListenSocket* connection, | 164 virtual void DidRead(StreamListenSocket* connection, |
167 const char* data, | 165 const char* data, |
168 int length) OVERRIDE; | 166 int length) OVERRIDE; |
169 virtual void DidClose(StreamListenSocket* connection) OVERRIDE; | 167 virtual void DidClose(StreamListenSocket* connection) OVERRIDE; |
170 | 168 |
171 HttpConnection* FindConnection(StreamListenSocket* socket); | 169 HttpConnection* FindConnection(StreamListenSocket* socket); |
172 | 170 |
173 // Posts a task to the |io_thread_| and waits for a reply. | 171 // Posts a task to the |io_thread_| and waits for a reply. |
174 bool PostTaskToIOThreadAndWait( | 172 bool PostTaskToIOThreadAndWait(const base::Closure& closure) |
175 const base::Closure& closure) WARN_UNUSED_RESULT; | 173 WARN_UNUSED_RESULT; |
176 | 174 |
177 scoped_ptr<base::Thread> io_thread_; | 175 scoped_ptr<base::Thread> io_thread_; |
178 | 176 |
179 scoped_ptr<HttpListenSocket> listen_socket_; | 177 scoped_ptr<HttpListenSocket> listen_socket_; |
180 int port_; | 178 int port_; |
181 GURL base_url_; | 179 GURL base_url_; |
182 | 180 |
183 // Owns the HttpConnection objects. | 181 // Owns the HttpConnection objects. |
184 std::map<StreamListenSocket*, HttpConnection*> connections_; | 182 std::map<StreamListenSocket*, HttpConnection*> connections_; |
185 | 183 |
186 // Vector of registered request handlers. | 184 // Vector of registered request handlers. |
187 std::vector<HandleRequestCallback> request_handlers_; | 185 std::vector<HandleRequestCallback> request_handlers_; |
188 | 186 |
189 // Note: This should remain the last member so it'll be destroyed and | 187 // Note: This should remain the last member so it'll be destroyed and |
190 // invalidate its weak pointers before any other members are destroyed. | 188 // invalidate its weak pointers before any other members are destroyed. |
191 base::WeakPtrFactory<EmbeddedTestServer> weak_factory_; | 189 base::WeakPtrFactory<EmbeddedTestServer> weak_factory_; |
192 | 190 |
193 base::ThreadChecker thread_checker_; | 191 base::ThreadChecker thread_checker_; |
194 | 192 |
195 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); | 193 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); |
196 }; | 194 }; |
197 | 195 |
198 } // namespace test_servers | 196 } // namespace test_servers |
199 } // namespace net | 197 } // namespace net |
200 | 198 |
201 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 199 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
OLD | NEW |