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

Side by Side Diff: chrome/browser/chromeos/drive/test_servers/http_server.h

Issue 11088073: HTTP server for testing Google Drive. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed response generation. 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_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_SERVER_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_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/chromeos/drive/test_servers/http_connection.h"
15 #include "chrome/browser/chromeos/drive/test_servers/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_servers {
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 // hello_world_url = test_server->RegisterSimpleTextResponse(
47 // "<b>Hello world!</b>",
48 // "text/html");
49 // metadata_url = test_server->RegisterFileResponse(
50 // "metadata/file.doc")
51 // "testing/data/metadata.xml",
52 // "text/xml",
53 // 200);
54 // }
55 class HttpServer : private net::StreamListenSocket::Delegate {
56 public:
57 typedef base::Callback<void(HttpServer* server)> CreateCallback;
58 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest* request)>
59 HandleRequestCallback;
60
61 ~HttpServer();
62
63 // Factory method. Created a server instance and returns a pointer or NULL in
64 // case of failure. Can be run from any thread. It is blocking.
65 static scoped_ptr<HttpServer> CreateForTesting();
66
67 // Factory method. Created a server instance and returns a pointer to it
68 // through the |callback|. Returns NULL in case of failure. Must be called
69 // from IO thread.
70 static void CreateOnIOThread(const CreateCallback& callback);
71
72 // Provides URL to the server which is useful when general purpose provider
73 // is registered.
74 GURL GetBaseURL();
75
76 // The most general purpose method. Any request processing can be added using
77 // this method. Takes ownership of the object. The |callback| is called
78 // on UI thread.
79 void RegisterRequestHandler(const HandleRequestCallback& callback);
satorux1 2012/11/13 05:28:34 Hmm, I think this function should take |relative_p
mtomasz 2012/11/13 12:23:07 Callback decides if it will handle that request. I
satorux1 2012/11/14 01:34:32 I think it's good to make it clear what code handl
mtomasz 2012/11/14 03:23:35 I agree that your idea is simpler. But I don't thi
80
81 // Used to provide the same predefined response for the requests matching
82 // the |relative_path|. Should be used if any custom data, such as additional
83 // headers should be send from the server.
84 GURL RegisterDefaultResponse(
85 const std::string& relative_path,
86 const HttpResponse& default_response);
87
88 // Registers a simple text response.
89 GURL RegisterTextResponse(
90 const std::string& relative_path,
91 const std::string& content,
92 const std::string& content_type,
93 const ResponseCode response_code);
satorux1 2012/11/13 05:13:18 let's add a blank line.
mtomasz 2012/11/13 12:23:07 Done.
94 // Registers a simple text response with automatically generated unique url.
95 // The response code is always 200.
96 GURL RegisterSimpleTextResponse(const std::string& content,
satorux1 2012/11/13 05:28:34 I think the "simple" version is unnecessary. I'd r
mtomasz 2012/11/13 12:23:07 The reason was that in most cases (I think in all
97 const std::string& content_type);
satorux1 2012/11/13 05:13:18 indentation is off.
mtomasz 2012/11/13 12:23:07 Done.
98
99 // Registers a simple file response. The file is loaded into memory.
100 GURL RegisterFileResponse(
101 const std::string& relative_path,
102 const FilePath& file_path,
103 const std::string& content_type,
104 const ResponseCode response_code);
satorux1 2012/11/13 05:13:18 blank line.
mtomasz 2012/11/13 12:23:07 Done.
105 // Registers a simple file response with automatically generated unique url.
106 // The response code is always 200.
107 GURL RegisterSimpleFileResponse(
satorux1 2012/11/13 05:28:34 Likewise, I think we don't need the simple version
mtomasz 2012/11/13 12:23:07 Done.
108 const FilePath& file_path,
109 const std::string& content_type);
110
111 private:
112 // Creates and starts server and attaches it to |socket_descriptor| socket.
113 // Passed |port| should be a valid port name, on which the socket is
114 // listening. Use factory methods to create an instance of the server.
115 HttpServer(int port, const SocketDescriptor& socket_descriptor);
116
117 // Handles a request when it is parsed. It passes the request to registed
118 // request handlers and sends a http response.
119 void HandleRequest(HttpConnection* connection,
120 scoped_ptr<HttpRequest> request);
121
122 // Generates unique identifier to make unique URL addresses.
123 std::string GenerateUniqueIdentifier();
satorux1 2012/11/13 05:28:34 We can remove this if we get rid of the simple ver
mtomasz 2012/11/13 12:23:07 Done.
124
125 // |server| is the original listening Socket, connection is the new
126 // Socket that was created. Ownership of |connection| is transferred
127 // to the delegate with this call.
128 void DidAccept(net::StreamListenSocket* server,
129 net::StreamListenSocket* connection) OVERRIDE;
130 void DidRead(net::StreamListenSocket* connection,
131 const char* data,
132 int length) OVERRIDE;
133 void DidClose(net::StreamListenSocket* connection) OVERRIDE;
134
135 HttpConnection* FindConnection(net::StreamListenSocket* socket);
136
137 scoped_refptr<HttpListenSocket> listen_socket_;
138 int port_;
139 GURL base_url_;
140 int last_unique_id_;
141
142 // Owns the HttpConnection objects.
143 std::map<net::StreamListenSocket*, HttpConnection*> connections_;
144
145 // Vector of registered request handlers.
146 std::vector<HandleRequestCallback> request_handlers_;
147
148 base::WeakPtrFactory<HttpServer> weak_factory_;
satorux1 2012/11/13 05:28:34 weak_ptr_factory_ Please also add: // Note: Th
mtomasz 2012/11/13 12:23:07 Done.
149 DISALLOW_COPY_AND_ASSIGN(HttpServer);
150 };
151
152 } // namespace test_servers
153 } // namespace drive
154
155 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698