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

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

Issue 11088073: HTTP server for testing Google Drive. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed for clang. 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_TEST_SERVER_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_TEST_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<HttpTestServer> test_server_;
41 // GURL hello_world_url_;
42 // GURL file_url_;
43 // (...)
44 // void SetUp() {
45 // test_server_.reset(new HttpTestServer());
46 // hello_world_url = test_server->RegisterTextResponse("<b>Hello world!</b>",
47 // "text/html");
48 // metadata_url = test_server->RegisterFileResponse(
49 // "metadata/file.doc")
50 // "testing/data/metadata.xml",
51 // "text/xml",
52 // 200);
53 // }
54 class HttpTestServer : private net::StreamListenSocket::Delegate {
55 public:
56 typedef base::Callback<void(HttpTestServer* server)> CreateCallback;
57 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest* request)>
58 HandleRequestCallback;
59
60 virtual ~HttpTestServer();
61
62 // Factory method. Created a server instance and returns a pointer or NULL in
63 // case of failure. Can be run from any thread. It is blocking.
64 static scoped_ptr<HttpTestServer> CreateForTesting();
65
66 // Factory method. Created a server instance and returns a pointer to it
67 // through the |callback|. Returns NULL in case of failure. Must be called
68 // from IO thread.
69 static void CreateOnIOThread(const CreateCallback& callback);
70
71 // Provides URL to the server which is useful when general purpose provider
72 // is registered.
73 virtual GURL GetBaseURL();
74
75 // The most general purpose method. Any request processing can be added using
76 // this method. Takes ownership of the object. The |callback| is called
77 // on UI thread.
78 virtual void RegisterRequestHandler(const HandleRequestCallback& callback);
79
80 // Used to provide the same predefined response for the requests matching
81 // the |relative_path|. Should be used if any custom data, such as additional
82 // headers should be send from the server.
83 virtual GURL RegisterDefaultResponse(
84 const std::string& relative_path,
85 const HttpResponse& default_response);
86
87 // Registers a simple text response.
88 virtual GURL RegisterTextResponse(
89 const std::string& relative_path,
90 const std::string& content,
91 const std::string& content_type,
92 const ResponseCode response_code);
93 // Registers a simple text response with automatically generated unique url.
94 // The response code is always 200.
95 virtual GURL RegisterTextResponse(const std::string& content,
96 const std::string& content_type);
97
98 // Registers a simple file response. The file is loaded into memory.
99 virtual GURL RegisterFileResponse(
100 const std::string& relative_path,
101 const FilePath& file_path,
102 const std::string& content_type,
103 const ResponseCode response_code);
104 // Registers a simple file response with automatically generated unique url.
105 // The response code is always 200.
106 virtual GURL RegisterFileResponse(
107 const FilePath& file_path,
108 const std::string& content_type);
109
110 private:
111 // Creates and starts server and attaches it to |socket_descriptor| socket.
112 // Passed |port| should be a valid port name, on which the socket is
113 // listening. Use factory methods to create an instance of the server.
114 HttpTestServer(int port, const SocketDescriptor& socket_descriptor);
115
116 // Handles a request when it is parsed. It passes the request to registed
117 // request handlers and sends a http response.
118 virtual void HandleRequest(HttpConnection* connection,
119 scoped_ptr<HttpRequest> request);
120
121 // Generates unique identifier to make unique URL addresses.
122 virtual std::string GenerateUniqueIdentifier();
123
124 // |server| is the original listening Socket, connection is the new
125 // Socket that was created. Ownership of |connection| is transferred
126 // to the delegate with this call.
127 virtual void DidAccept(net::StreamListenSocket* server,
128 net::StreamListenSocket* connection) OVERRIDE;
129 virtual void DidRead(net::StreamListenSocket* connection,
130 const char* data,
131 int length) OVERRIDE;
132 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE;
133
134 virtual HttpConnection* FindConnection(net::StreamListenSocket* socket);
135
136 scoped_refptr<HttpListenSocket> listen_socket_;
137 int port_;
138 GURL base_url_;
139 int last_unique_id_;
140
141 // Owns the HttpConnection objects.
142 std::map<net::StreamListenSocket*, HttpConnection*> connections_;
143
144 // Vector of registered request handlers.
145 std::vector<HandleRequestCallback> request_handlers_;
146
147 base::WeakPtrFactory<HttpTestServer> weak_factory_;
148 DISALLOW_COPY_AND_ASSIGN(HttpTestServer);
149 };
150
151 } // namespace test_servers
152 } // namespace drive
153
154 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_TEST_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698