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> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/threading/thread.h" |
17 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
18 #include "net/socket/tcp_listen_socket.h" | 19 #include "net/socket/tcp_listen_socket.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 class FilePath; | 23 class FilePath; |
23 } | 24 } |
24 | 25 |
25 namespace net { | 26 namespace net { |
26 namespace test_server { | 27 namespace test_server { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // http_response->set_content("hello"); | 75 // http_response->set_content("hello"); |
75 // http_response->set_content_type("text/plain"); | 76 // http_response->set_content_type("text/plain"); |
76 // return http_response.Pass(); | 77 // return http_response.Pass(); |
77 // } | 78 // } |
78 // | 79 // |
79 class EmbeddedTestServer : public StreamListenSocket::Delegate { | 80 class EmbeddedTestServer : public StreamListenSocket::Delegate { |
80 public: | 81 public: |
81 typedef base::Callback<scoped_ptr<HttpResponse>( | 82 typedef base::Callback<scoped_ptr<HttpResponse>( |
82 const HttpRequest& request)> HandleRequestCallback; | 83 const HttpRequest& request)> HandleRequestCallback; |
83 | 84 |
84 // Creates a http test server. |io_thread| is a task runner | 85 // Creates a http test server. InitializeAndWaitUntilReady() must be called |
85 // with IO message loop, used as a backend thread. | 86 // to start the server. |
86 // InitializeAndWaitUntilReady() must be called to start the server. | 87 EmbeddedTestServer(); |
87 explicit EmbeddedTestServer( | |
88 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread); | |
89 virtual ~EmbeddedTestServer(); | 88 virtual ~EmbeddedTestServer(); |
90 | 89 |
91 // Initializes and waits until the server is ready to accept requests. | 90 // Initializes and waits until the server is ready to accept requests. |
92 bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT; | 91 bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT; |
93 | 92 |
94 // Shuts down the http server and waits until the shutdown is complete. | 93 // Shuts down the http server and waits until the shutdown is complete. |
95 bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT; | 94 bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT; |
96 | 95 |
97 // Checks if the server is started. | 96 // Checks if the server is started. |
98 bool Started() const { | 97 bool Started() const { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 const char* data, | 142 const char* data, |
144 int length) OVERRIDE; | 143 int length) OVERRIDE; |
145 virtual void DidClose(StreamListenSocket* connection) OVERRIDE; | 144 virtual void DidClose(StreamListenSocket* connection) OVERRIDE; |
146 | 145 |
147 HttpConnection* FindConnection(StreamListenSocket* socket); | 146 HttpConnection* FindConnection(StreamListenSocket* socket); |
148 | 147 |
149 // Posts a task to the |io_thread_| and waits for a reply. | 148 // Posts a task to the |io_thread_| and waits for a reply. |
150 bool PostTaskToIOThreadAndWait( | 149 bool PostTaskToIOThreadAndWait( |
151 const base::Closure& closure) WARN_UNUSED_RESULT; | 150 const base::Closure& closure) WARN_UNUSED_RESULT; |
152 | 151 |
153 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; | 152 base::Thread io_thread_; |
154 | 153 |
155 scoped_ptr<HttpListenSocket> listen_socket_; | 154 scoped_ptr<HttpListenSocket> listen_socket_; |
156 int port_; | 155 int port_; |
157 GURL base_url_; | 156 GURL base_url_; |
158 | 157 |
159 // Owns the HttpConnection objects. | 158 // Owns the HttpConnection objects. |
160 std::map<StreamListenSocket*, HttpConnection*> connections_; | 159 std::map<StreamListenSocket*, HttpConnection*> connections_; |
161 | 160 |
162 // Vector of registered request handlers. | 161 // Vector of registered request handlers. |
163 std::vector<HandleRequestCallback> request_handlers_; | 162 std::vector<HandleRequestCallback> request_handlers_; |
164 | 163 |
165 // Note: This should remain the last member so it'll be destroyed and | 164 // Note: This should remain the last member so it'll be destroyed and |
166 // invalidate its weak pointers before any other members are destroyed. | 165 // invalidate its weak pointers before any other members are destroyed. |
167 base::WeakPtrFactory<EmbeddedTestServer> weak_factory_; | 166 base::WeakPtrFactory<EmbeddedTestServer> weak_factory_; |
168 | 167 |
169 base::ThreadChecker thread_checker_; | 168 base::ThreadChecker thread_checker_; |
170 | 169 |
171 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); | 170 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); |
172 }; | 171 }; |
173 | 172 |
174 } // namespace test_servers | 173 } // namespace test_servers |
175 } // namespace net | 174 } // namespace net |
176 | 175 |
177 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 176 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
OLD | NEW |