Index: chrome/browser/chromeos/drive/test_servers/http_test_server.h |
diff --git a/chrome/browser/chromeos/drive/test_servers/http_test_server.h b/chrome/browser/chromeos/drive/test_servers/http_test_server.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0966e7c87e40d7643400b108bb2fd22ab3920059 |
--- /dev/null |
+++ b/chrome/browser/chromeos/drive/test_servers/http_test_server.h |
@@ -0,0 +1,156 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_TEST_SERVER_H_ |
+#define CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_TEST_SERVER_H_ |
+ |
+#include <string> |
+#include <vector> |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/file_path.h" |
+#include "base/memory/ref_counted.h" |
+#include "chrome/browser/chromeos/drive/test_servers/http_connection.h" |
+#include "chrome/browser/chromeos/drive/test_servers/http_response.h" |
+#include "chrome/browser/google_apis/gdata_test_util.h" |
+#include "net/base/tcp_listen_socket.h" |
+ |
+namespace drive { |
+namespace test_servers { |
+ |
+// This class is required to be able to have composition instead of inheritance, |
+class HttpListenSocket: public net::TCPListenSocket { |
+ public: |
+ HttpListenSocket(const SocketDescriptor socket_descriptor, |
+ net::StreamListenSocket::Delegate* delegate) |
+ : net::TCPListenSocket(socket_descriptor, delegate) { |
+ } |
+ |
+ void Listen() { |
+ net::TCPListenSocket::Listen(); |
+ } |
+}; |
+ |
+// Class providing a HTTP server for testing purpose. This is a basic server |
+// providing only an essential subset of HTTP/1.1 protocol. Especially, |
+// it assumes that the request syntax is correct. It *does not* support |
+// a Chunked Transfer Encoding. |
+// |
+// The common use case is below: |
+// |
+// scoped_ptr<HttpTestServer> test_server_; |
+// GURL hello_world_url_; |
+// GURL file_url_; |
+// (...) |
+// void SetUp() { |
+// test_server_.reset(new HttpTestServer()); |
+// hello_world_url = test_server->RegisterTextResponse("<b>Hello world!</b>", |
satorux1
2012/11/12 06:34:15
So the URL is automatically generated? I thought i
mtomasz
2012/11/12 12:17:44
It can be done either way. In line #50, there is a
|
+// "text/html"); |
+// metadata_url = test_server->RegisterFileResponse( |
+// "metadata/file.doc") |
+// "testing/data/metadata.xml", |
+// "text/xml", |
+// 200); |
+// } |
+class HttpTestServer : private net::StreamListenSocket::Delegate { |
satorux1
2012/11/12 06:43:47
TestHttpServer?
mtomasz
2012/11/12 12:17:44
How about just HttpServer? It is already in test_s
satorux1
2012/11/13 05:28:34
Speaking of the namespace, do you expect to add mo
mtomasz
2012/11/13 12:23:07
Done.
|
+ public: |
+ typedef base::Callback<void(HttpTestServer* server)> CreateCallback; |
+ typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest* request)> |
+ HandleRequestCallback; |
+ |
+ virtual ~HttpTestServer(); |
+ |
+ // Factory method. Created a server instance and returns a pointer or NULL in |
+ // case of failure. Can be run from any thread. It is blocking. |
+ static scoped_ptr<HttpTestServer> CreateForTesting(); |
satorux1
2012/11/12 06:43:47
CreateForTesting() sounded a bit awkward as the se
satorux1
2012/11/13 05:28:34
did you miss this comment?
mtomasz
2012/11/13 12:23:07
I have some questions about this comment.
|
+ |
+ // Factory method. Created a server instance and returns a pointer to it |
+ // through the |callback|. Returns NULL in case of failure. Must be called |
+ // from IO thread. |
+ static void CreateOnIOThread(const CreateCallback& callback); |
satorux1
2012/11/12 06:43:47
Move it anonymous namespace in .cc file? The funct
mtomasz
2012/11/12 12:17:44
The constructor is private, so we need it here.
satorux1
2012/11/13 05:28:34
Then we can at least make it private?
mtomasz
2012/11/13 12:23:07
Sure! Done.
|
+ |
+ // Provides URL to the server which is useful when general purpose provider |
+ // is registered. |
+ virtual GURL GetBaseURL(); |
+ |
+ // The most general purpose method. Any request processing can be added using |
+ // this method. Takes ownership of the object. The |callback| is called |
+ // on UI thread. |
+ virtual void RegisterRequestHandler(const HandleRequestCallback& callback); |
+ |
+ // Used to provide the same predefined response for the requests matching |
+ // the |relative_path|. Should be used if any custom data, such as additional |
+ // headers should be send from the server. |
+ virtual GURL RegisterDefaultResponse( |
+ const std::string& relative_path, |
+ const HttpResponse& default_response); |
+ |
+ // Registers a simple text response. |
+ virtual GURL RegisterTextResponse( |
+ const std::string& relative_path, |
+ const std::string& content, |
+ const std::string& content_type, |
+ const ResponseCode response_code); |
+ // Registers a simple text response with automatically generated unique url. |
+ // The response code is always 200. |
+ virtual GURL RegisterTextResponse(const std::string& content, |
+ const std::string& content_type); |
+ |
+ // Registers a simple file response. The file is loaded into memory. |
+ virtual GURL RegisterFileResponse( |
+ const std::string& relative_path, |
+ const FilePath& file_path, |
+ const std::string& content_type, |
+ const ResponseCode response_code); |
+ // Registers a simple file response with automatically generated unique url. |
+ // The response code is always 200. |
+ virtual GURL RegisterFileResponse( |
+ const FilePath& file_path, |
+ const std::string& content_type); |
+ |
+ private: |
+ // Creates and starts server and attaches it to |socket_descriptor| socket. |
+ // Passed |port| should be a valid port name, on which the socket is |
+ // listening. Use factory methods to create an instance of the server. |
+ HttpTestServer(int port, const SocketDescriptor& socket_descriptor); |
+ |
+ // Handles a request when it is parsed. It passes the request to registed |
+ // request handlers and sends a http response. |
+ virtual void HandleRequest(HttpConnection* connection, |
+ scoped_ptr<HttpRequest> request); |
+ |
+ // Generates unique identifier to make unique URL addresses. |
+ virtual std::string GenerateUniqueIdentifier(); |
+ |
+ // |server| is the original listening Socket, connection is the new |
+ // Socket that was created. Ownership of |connection| is transferred |
+ // to the delegate with this call. |
+ virtual void DidAccept(net::StreamListenSocket* server, |
+ net::StreamListenSocket* connection) OVERRIDE; |
+ virtual void DidRead(net::StreamListenSocket* connection, |
+ const char* data, |
+ int length) OVERRIDE; |
+ virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE; |
+ |
+ virtual HttpConnection* FindConnection(net::StreamListenSocket* socket); |
+ |
+ scoped_refptr<HttpListenSocket> listen_socket_; |
+ int port_; |
+ GURL base_url_; |
+ int last_unique_id_; |
+ |
+ // Owns the HttpConnection objects. |
+ std::map<net::StreamListenSocket*, HttpConnection*> connections_; |
+ |
+ // Vector of registered request handlers. |
+ std::vector<HandleRequestCallback> request_handlers_; |
+ |
+ base::WeakPtrFactory<HttpTestServer> weak_factory_; |
+ DISALLOW_COPY_AND_ASSIGN(HttpTestServer); |
+}; |
+ |
+} // namespace test_servers |
+} // namespace drive |
+ |
+#endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_TEST_SERVER_H_ |