Chromium Code Reviews| 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: |
|
oshima
2013/11/26 02:26:02
Forgot to mention. I'll update the comment and add
| |
| 52 // | 56 // |
| 53 // base::Thread io_thread_; | 57 // base::Thread io_thread_; |
| 54 // scoped_ptr<EmbeddedTestServer> test_server_; | 58 // scoped_ptr<EmbeddedTestServer> test_server_; |
| 55 // | 59 // |
| 56 // void SetUp() { | 60 // void SetUp() { |
| 57 // base::Thread::Options thread_options; | 61 // base::Thread::Options thread_options; |
| 58 // thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 62 // thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 59 // ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); | 63 // ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); |
| 60 // | 64 // |
| 61 // test_server_.reset( | 65 // test_server_.reset( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 // For instance, a request to "/foo.html" is served by "foo.html" under | 119 // 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 | 120 // |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|). | 121 // (i.e. "/foo/bar.html" is served by "foo/bar.html" under |directory|). |
| 118 void ServeFilesFromDirectory(const base::FilePath& directory); | 122 void ServeFilesFromDirectory(const base::FilePath& directory); |
| 119 | 123 |
| 120 // The most general purpose method. Any request processing can be added using | 124 // The most general purpose method. Any request processing can be added using |
| 121 // this method. Takes ownership of the object. The |callback| is called | 125 // this method. Takes ownership of the object. The |callback| is called |
| 122 // on UI thread. | 126 // on UI thread. |
| 123 void RegisterRequestHandler(const HandleRequestCallback& callback); | 127 void RegisterRequestHandler(const HandleRequestCallback& callback); |
| 124 | 128 |
| 129 // Starts/Stops IO thread that handles http requests. | |
| 130 void StartThread(); | |
| 131 void StopThread(); | |
| 132 | |
| 125 private: | 133 private: |
| 126 // Initializes and starts the server. If initialization succeeds, Starts() | 134 // Initializes and starts the server. If initialization succeeds, Starts() |
| 127 // will return true. | 135 // will return true. |
| 128 void InitializeOnIOThread(); | 136 void InitializeOnIOThread(); |
| 129 | 137 |
| 130 // Shuts down the server. | 138 // Shuts down the server. |
| 131 void ShutdownOnIOThread(); | 139 void ShutdownOnIOThread(); |
| 132 | 140 |
| 133 // Handles a request when it is parsed. It passes the request to registed | 141 // Handles a request when it is parsed. It passes the request to registed |
| 134 // request handlers and sends a http response. | 142 // request handlers and sends a http response. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 | 175 |
| 168 base::ThreadChecker thread_checker_; | 176 base::ThreadChecker thread_checker_; |
| 169 | 177 |
| 170 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); | 178 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); |
| 171 }; | 179 }; |
| 172 | 180 |
| 173 } // namespace test_servers | 181 } // namespace test_servers |
| 174 } // namespace net | 182 } // namespace net |
| 175 | 183 |
| 176 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 184 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
| OLD | NEW |