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

Side by Side Diff: chrome/browser/google_apis/test_server/http_server.h

Issue 11088073: HTTP server for testing Google Drive. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed most comments. Created 8 years, 1 month 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
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 CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
7
8 #include <string>
9 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/google_apis/test_server/http_connection.h"
15 #include "chrome/browser/google_apis/test_server/http_response.h"
16 #include "chrome/browser/google_apis/gdata_test_util.h"
17 #include "net/base/tcp_listen_socket.h"
18
19 namespace drive {
20 namespace test_server {
21
22 // This class is required to be able to have composition instead of inheritance,
23 class HttpListenSocket: public net::TCPListenSocket {
24 public:
25 HttpListenSocket(const SocketDescriptor socket_descriptor,
26 net::StreamListenSocket::Delegate* delegate);
27 virtual void Listen();
28
29 private:
30 virtual ~HttpListenSocket();
31 };
32
33 // Class providing a HTTP server for testing purpose. This is a basic server
34 // providing only an essential subset of HTTP/1.1 protocol. Especially,
35 // it assumes that the request syntax is correct. It *does not* support
36 // a Chunked Transfer Encoding.
37 //
38 // The common use case is below:
39 //
40 // scoped_ptr<HttpServer> test_server_;
41 // GURL hello_world_url_;
42 // GURL file_url_;
43 // (...)
44 // void SetUp() {
45 // test_server_.reset(new HttpServer());
46 // DCHECK(test_server_.InitializeAndWaitUntilReady());
47 // hello_world_url = test_server->RegisterTextResponse(
48 // "/abc",
49 // "<b>Hello world!</b>",
50 // "text/html");
51 // metadata_url = test_server->RegisterFileResponse(
52 // "metadata/file.doc")
53 // "testing/data/metadata.xml",
54 // "text/xml",
55 // 200);
56 // }
57 class HttpServer : private net::StreamListenSocket::Delegate {
58 public:
59 typedef base::Callback<void(bool success)> InitializeCallback;
60 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)>
61 HandleRequestCallback;
62
63 // Creates a http test server. InitializeAndWaitUntilReady() must be called
64 // to start the server.
65 HttpServer();
66 ~HttpServer();
67
68 // Initializes and starts the http server. Returns true on success.
69 // It is blocking the current thread.
70 bool InitializeAndWaitUntilReady();
71
72 // Checks if the server is started.
73 bool Started() {
satorux1 2012/11/14 03:33:57 nit: bool Started() const
satorux1 2012/11/14 04:43:36 Done.
74 return listen_socket_.get() != NULL;
75 }
76
77 // Provides URL to the server which is useful when general purpose provider
78 // is registered.
79 GURL GetBaseURL();
80
81 // The most general purpose method. Any request processing can be added using
82 // this method. Takes ownership of the object. The |callback| is called
83 // on UI thread.
84 void RegisterRequestHandler(const HandleRequestCallback& callback);
85
86 // Used to provide the same predefined response for the requests matching
87 // the |relative_path|. Should be used if any custom data, such as additional
88 // headers should be send from the server.
89 GURL RegisterDefaultResponse(
90 const std::string& relative_path,
91 const HttpResponse& default_response);
92
93 // Registers a simple text response.
94 GURL RegisterTextResponse(
95 const std::string& relative_path,
96 const std::string& content,
97 const std::string& content_type,
98 const ResponseCode response_code);
99
100 // Registers a simple file response. The file is loaded into memory.
101 GURL RegisterFileResponse(
102 const std::string& relative_path,
103 const FilePath& file_path,
104 const std::string& content_type,
105 const ResponseCode response_code);
106
107 private:
108 // Initializes and starts the server. Result is passed via |callback|.
109 void InitializeOnIOThread(const InitializeCallback& callback);
110
111 // Handles a request when it is parsed. It passes the request to registed
112 // request handlers and sends a http response.
113 void HandleRequest(HttpConnection* connection,
114 scoped_ptr<HttpRequest> request);
115
116 // |server| is the original listening Socket, connection is the new
117 // Socket that was created. Ownership of |connection| is transferred
118 // to the delegate with this call.
119 void DidAccept(net::StreamListenSocket* server,
120 net::StreamListenSocket* connection) OVERRIDE;
121 void DidRead(net::StreamListenSocket* connection,
122 const char* data,
123 int length) OVERRIDE;
124 void DidClose(net::StreamListenSocket* connection) OVERRIDE;
125
126 HttpConnection* FindConnection(net::StreamListenSocket* socket);
127
128 scoped_refptr<HttpListenSocket> listen_socket_;
129 int port_;
130 GURL base_url_;
131
132 // Owns the HttpConnection objects.
133 std::map<net::StreamListenSocket*, HttpConnection*> connections_;
134
135 // Vector of registered request handlers.
136 std::vector<HandleRequestCallback> request_handlers_;
137
138 // Note: This should remain the last member so it'll be destroyed and
139 // invalidate its weak pointers before any other members are destroyed.
140 base::WeakPtrFactory<HttpServer> weak_factory_;
141 DISALLOW_COPY_AND_ASSIGN(HttpServer);
142 };
143
144 } // namespace test_servers
145 } // namespace drive
146
147 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698