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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 // This class is required to be able to have composition instead of inheritance, | 33 // This class is required to be able to have composition instead of inheritance, |
34 class HttpListenSocket : public TCPListenSocket { | 34 class HttpListenSocket : public TCPListenSocket { |
35 public: | 35 public: |
36 HttpListenSocket(const SocketDescriptor socket_descriptor, | 36 HttpListenSocket(const SocketDescriptor socket_descriptor, |
37 StreamListenSocket::Delegate* delegate); | 37 StreamListenSocket::Delegate* delegate); |
38 virtual ~HttpListenSocket(); | 38 virtual ~HttpListenSocket(); |
39 virtual void Listen(); | 39 virtual void Listen(); |
40 | 40 |
41 private: | 41 private: |
| 42 friend class EmbeddedTestServer; |
| 43 |
| 44 // Detaches the current from |thread_checker_|. |
| 45 void DetachFromThread(); |
42 | 46 |
43 base::ThreadChecker thread_checker_; | 47 base::ThreadChecker thread_checker_; |
44 }; | 48 }; |
45 | 49 |
46 // Class providing an HTTP server for testing purpose. This is a basic server | 50 // Class providing an HTTP server for testing purpose. This is a basic server |
47 // providing only an essential subset of HTTP/1.1 protocol. Especially, | 51 // providing only an essential subset of HTTP/1.1 protocol. Especially, |
48 // it assumes that the request syntax is correct. It *does not* support | 52 // it assumes that the request syntax is correct. It *does not* support |
49 // a Chunked Transfer Encoding. | 53 // a Chunked Transfer Encoding. |
50 // | 54 // |
51 // The common use case is below: | 55 // The common use case for unit tests is below: |
52 // | |
53 // base::Thread io_thread_; | |
54 // scoped_ptr<EmbeddedTestServer> test_server_; | |
55 // | 56 // |
56 // void SetUp() { | 57 // void SetUp() { |
57 // base::Thread::Options thread_options; | 58 // test_server_.reset(new EmbeddedTestServer()); |
58 // thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | |
59 // ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); | |
60 // | |
61 // test_server_.reset( | |
62 // new EmbeddedTestServer(io_thread_.message_loop_proxy())); | |
63 // ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 59 // ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
64 // test_server_->RegisterRequestHandler( | 60 // test_server_->RegisterRequestHandler( |
65 // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); | 61 // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); |
66 // } | 62 // } |
67 // | 63 // |
68 // scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { | 64 // scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { |
69 // GURL absolute_url = test_server_->GetURL(request.relative_url); | 65 // GURL absolute_url = test_server_->GetURL(request.relative_url); |
70 // if (absolute_url.path() != "/test") | 66 // if (absolute_url.path() != "/test") |
71 // return scoped_ptr<HttpResponse>(); | 67 // return scoped_ptr<HttpResponse>(); |
72 // | 68 // |
73 // scoped_ptr<HttpResponse> http_response(new HttpResponse()); | 69 // scoped_ptr<HttpResponse> http_response(new HttpResponse()); |
74 // http_response->set_code(test_server::SUCCESS); | 70 // http_response->set_code(test_server::SUCCESS); |
75 // http_response->set_content("hello"); | 71 // http_response->set_content("hello"); |
76 // http_response->set_content_type("text/plain"); | 72 // http_response->set_content_type("text/plain"); |
77 // return http_response.Pass(); | 73 // return http_response.Pass(); |
78 // } | 74 // } |
79 // | 75 // |
| 76 // For a test that spawns another process such as browser_tests, you |
| 77 // need to stop the server's thread so that there is no no other |
| 78 // threads running while spawning the process. To do so, please follow |
| 79 // the following example: |
| 80 // |
| 81 // void SetUp() { |
| 82 // test_server_.reset(new EmbeddedTestServer()); |
| 83 // // EmbeddedTestServer spawns a thread to initialize socket. |
| 84 // // Stop the thread in preparation for fork and exec. |
| 85 // test_server_->StopThread(); |
| 86 // ... |
| 87 // InProcessBrowserTest::SetUp(); |
| 88 // } |
| 89 // |
| 90 // void SetUpOnMainThread() { |
| 91 // test_server_->RestartThreadAndListen(); |
| 92 // } |
| 93 // |
80 class EmbeddedTestServer : public StreamListenSocket::Delegate { | 94 class EmbeddedTestServer : public StreamListenSocket::Delegate { |
81 public: | 95 public: |
82 typedef base::Callback<scoped_ptr<HttpResponse>( | 96 typedef base::Callback<scoped_ptr<HttpResponse>( |
83 const HttpRequest& request)> HandleRequestCallback; | 97 const HttpRequest& request)> HandleRequestCallback; |
84 | 98 |
85 // Creates a http test server. InitializeAndWaitUntilReady() must be called | 99 // Creates a http test server. InitializeAndWaitUntilReady() must be called |
86 // to start the server. | 100 // to start the server. |
87 EmbeddedTestServer(); | 101 EmbeddedTestServer(); |
88 virtual ~EmbeddedTestServer(); | 102 virtual ~EmbeddedTestServer(); |
89 | 103 |
(...skipping 25 matching lines...) Expand all Loading... |
115 // For instance, a request to "/foo.html" is served by "foo.html" under | 129 // For instance, a request to "/foo.html" is served by "foo.html" under |
116 // |directory|. Files under sub directories are also handled in the same way | 130 // |directory|. Files under sub directories are also handled in the same way |
117 // (i.e. "/foo/bar.html" is served by "foo/bar.html" under |directory|). | 131 // (i.e. "/foo/bar.html" is served by "foo/bar.html" under |directory|). |
118 void ServeFilesFromDirectory(const base::FilePath& directory); | 132 void ServeFilesFromDirectory(const base::FilePath& directory); |
119 | 133 |
120 // The most general purpose method. Any request processing can be added using | 134 // The most general purpose method. Any request processing can be added using |
121 // this method. Takes ownership of the object. The |callback| is called | 135 // this method. Takes ownership of the object. The |callback| is called |
122 // on UI thread. | 136 // on UI thread. |
123 void RegisterRequestHandler(const HandleRequestCallback& callback); | 137 void RegisterRequestHandler(const HandleRequestCallback& callback); |
124 | 138 |
| 139 // Stops IO thread that handles http requests. |
| 140 void StopThread(); |
| 141 |
| 142 // Restarts IO thread and listen on the socket. |
| 143 void RestartThreadAndListen(); |
| 144 |
125 private: | 145 private: |
| 146 void StartThread(); |
| 147 |
126 // Initializes and starts the server. If initialization succeeds, Starts() | 148 // Initializes and starts the server. If initialization succeeds, Starts() |
127 // will return true. | 149 // will return true. |
128 void InitializeOnIOThread(); | 150 void InitializeOnIOThread(); |
| 151 void ListenOnIOThread(); |
129 | 152 |
130 // Shuts down the server. | 153 // Shuts down the server. |
131 void ShutdownOnIOThread(); | 154 void ShutdownOnIOThread(); |
132 | 155 |
133 // Handles a request when it is parsed. It passes the request to registed | 156 // Handles a request when it is parsed. It passes the request to registed |
134 // request handlers and sends a http response. | 157 // request handlers and sends a http response. |
135 void HandleRequest(HttpConnection* connection, | 158 void HandleRequest(HttpConnection* connection, |
136 scoped_ptr<HttpRequest> request); | 159 scoped_ptr<HttpRequest> request); |
137 | 160 |
138 // StreamListenSocket::Delegate overrides: | 161 // StreamListenSocket::Delegate overrides: |
(...skipping 28 matching lines...) Expand all Loading... |
167 | 190 |
168 base::ThreadChecker thread_checker_; | 191 base::ThreadChecker thread_checker_; |
169 | 192 |
170 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); | 193 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); |
171 }; | 194 }; |
172 | 195 |
173 } // namespace test_servers | 196 } // namespace test_servers |
174 } // namespace net | 197 } // namespace net |
175 | 198 |
176 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 199 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
OLD | NEW |