Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: net/test/embedded_test_server/http_server.h

Issue 15069003: Rename the embedded test server to EmbeddedTestServer in net::test_server namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nit Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP_SERVER_H_
6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_SERVER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "googleurl/src/gurl.h"
18 #include "net/socket/tcp_listen_socket.h"
19
20 namespace google_apis {
21 namespace test_server {
22
23 class HttpConnection;
24 class HttpResponse;
25 struct HttpRequest;
26
27 // This class is required to be able to have composition instead of inheritance,
28 class HttpListenSocket : public net::TCPListenSocket {
29 public:
30 HttpListenSocket(const SocketDescriptor socket_descriptor,
31 net::StreamListenSocket::Delegate* delegate);
32 virtual void Listen();
33
34 private:
35 virtual ~HttpListenSocket();
36
37 base::ThreadChecker thread_checker_;
38 };
39
40 // Class providing an HTTP server for testing purpose. This is a basic server
41 // providing only an essential subset of HTTP/1.1 protocol. Especially,
42 // it assumes that the request syntax is correct. It *does not* support
43 // a Chunked Transfer Encoding.
44 //
45 // The common use case is below:
46 //
47 // base::Thread io_thread_;
48 // scoped_ptr<HttpServer> test_server_;
49 //
50 // void SetUp() {
51 // base::Thread::Options thread_options;
52 // thread_options.message_loop_type = MessageLoop::TYPE_IO;
53 // ASSERT_TRUE(io_thread_.StartWithOptions(thread_options));
54 //
55 // test_server_.reset(new HttpServer(io_thread_.message_loop_proxy()));
56 // ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady());
57 // test_server_->RegisterRequestHandler(
58 // base::Bind(&FooTest::HandleRequest, base::Unretained(this)));
59 // }
60 //
61 // scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
62 // GURL absolute_url = test_server_->GetURL(request.relative_url);
63 // if (absolute_url.path() != "/test")
64 // return scoped_ptr<HttpResponse>();
65 //
66 // scoped_ptr<HttpResponse> http_response(new HttpResponse());
67 // http_response->set_code(test_server::SUCCESS);
68 // http_response->set_content("hello");
69 // http_response->set_content_type("text/plain");
70 // return http_response.Pass();
71 // }
72 //
73 class HttpServer : public net::StreamListenSocket::Delegate {
74 public:
75 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)>
76 HandleRequestCallback;
77
78 // Creates a http test server. |io_thread| is a task runner
79 // with IO message loop, used as a backend thread.
80 // InitializeAndWaitUntilReady() must be called to start the server.
81 explicit HttpServer(
82 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread);
83 virtual ~HttpServer();
84
85 // Initializes and waits until the server is ready to accept requests.
86 bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT;
87
88 // Shuts down the http server and waits until the shutdown is complete.
89 bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT;
90
91 // Checks if the server is started.
92 bool Started() const {
93 return listen_socket_.get() != NULL;
94 }
95
96 // Returns the base URL to the server, which looks like
97 // http://127.0.0.1:<port>/, where <port> is the actual port number used by
98 // the server.
99 const GURL& base_url() const { return base_url_; }
100
101 // Returns a URL to the server based on the given relative URL, which
102 // should start with '/'. For example: GetURL("/path?query=foo") =>
103 // http://127.0.0.1:<port>/path?query=foo.
104 GURL GetURL(const std::string& relative_url) const;
105
106 // Returns the port number used by the server.
107 int port() const { return port_; }
108
109 // The most general purpose method. Any request processing can be added using
110 // this method. Takes ownership of the object. The |callback| is called
111 // on UI thread.
112 void RegisterRequestHandler(const HandleRequestCallback& callback);
113
114 private:
115 // Initializes and starts the server. If initialization succeeds, Starts()
116 // will return true.
117 void InitializeOnIOThread();
118
119 // Shuts down the server.
120 void ShutdownOnIOThread();
121
122 // Handles a request when it is parsed. It passes the request to registed
123 // request handlers and sends a http response.
124 void HandleRequest(HttpConnection* connection,
125 scoped_ptr<HttpRequest> request);
126
127 // net::StreamListenSocket::Delegate overrides:
128 virtual void DidAccept(net::StreamListenSocket* server,
129 net::StreamListenSocket* connection) OVERRIDE;
130 virtual void DidRead(net::StreamListenSocket* connection,
131 const char* data,
132 int length) OVERRIDE;
133 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE;
134
135 HttpConnection* FindConnection(net::StreamListenSocket* socket);
136
137 scoped_refptr<base::SingleThreadTaskRunner> io_thread_;
138
139 scoped_refptr<HttpListenSocket> listen_socket_;
140 int port_;
141 GURL base_url_;
142
143 // Owns the HttpConnection objects.
144 std::map<net::StreamListenSocket*, HttpConnection*> connections_;
145
146 // Vector of registered request handlers.
147 std::vector<HandleRequestCallback> request_handlers_;
148
149 // Note: This should remain the last member so it'll be destroyed and
150 // invalidate its weak pointers before any other members are destroyed.
151 base::WeakPtrFactory<HttpServer> weak_factory_;
152
153 base::ThreadChecker thread_checker_;
154
155 DISALLOW_COPY_AND_ASSIGN(HttpServer);
156 };
157
158 } // namespace test_servers
159 } // namespace google_apis
160
161 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_SERVER_H_
OLDNEW
« no previous file with comments | « net/test/embedded_test_server/http_response_unittest.cc ('k') | net/test/embedded_test_server/http_server.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698