Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 : net::TCPListenSocket(socket_descriptor, delegate) { | |
| 28 } | |
| 29 | |
| 30 void Listen() { | |
| 31 net::TCPListenSocket::Listen(); | |
| 32 } | |
| 33 }; | |
| 34 | |
| 35 // Class providing a HTTP server for testing purpose. This is a basic server | |
| 36 // providing only an essential subset of HTTP/1.1 protocol. Especially, | |
| 37 // it assumes that the request syntax is correct. It *does not* support | |
| 38 // a Chunked Transfer Encoding. | |
| 39 // | |
| 40 // The common use case is below: | |
| 41 // | |
| 42 // scoped_ptr<HttpTestServer> test_server_; | |
| 43 // GURL hello_world_url_; | |
| 44 // GURL file_url_; | |
| 45 // (...) | |
| 46 // void SetUp() { | |
| 47 // test_server_.reset(new HttpTestServer()); | |
| 48 // 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
| |
| 49 // "text/html"); | |
| 50 // metadata_url = test_server->RegisterFileResponse( | |
| 51 // "metadata/file.doc") | |
| 52 // "testing/data/metadata.xml", | |
| 53 // "text/xml", | |
| 54 // 200); | |
| 55 // } | |
| 56 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.
| |
| 57 public: | |
| 58 typedef base::Callback<void(HttpTestServer* server)> CreateCallback; | |
| 59 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest* request)> | |
| 60 HandleRequestCallback; | |
| 61 | |
| 62 virtual ~HttpTestServer(); | |
| 63 | |
| 64 // Factory method. Created a server instance and returns a pointer or NULL in | |
| 65 // case of failure. Can be run from any thread. It is blocking. | |
| 66 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.
| |
| 67 | |
| 68 // Factory method. Created a server instance and returns a pointer to it | |
| 69 // through the |callback|. Returns NULL in case of failure. Must be called | |
| 70 // from IO thread. | |
| 71 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.
| |
| 72 | |
| 73 // Provides URL to the server which is useful when general purpose provider | |
| 74 // is registered. | |
| 75 virtual GURL GetBaseURL(); | |
| 76 | |
| 77 // The most general purpose method. Any request processing can be added using | |
| 78 // this method. Takes ownership of the object. The |callback| is called | |
| 79 // on UI thread. | |
| 80 virtual void RegisterRequestHandler(const HandleRequestCallback& callback); | |
| 81 | |
| 82 // Used to provide the same predefined response for the requests matching | |
| 83 // the |relative_path|. Should be used if any custom data, such as additional | |
| 84 // headers should be send from the server. | |
| 85 virtual GURL RegisterDefaultResponse( | |
| 86 const std::string& relative_path, | |
| 87 const HttpResponse& default_response); | |
| 88 | |
| 89 // Registers a simple text response. | |
| 90 virtual GURL RegisterTextResponse( | |
| 91 const std::string& relative_path, | |
| 92 const std::string& content, | |
| 93 const std::string& content_type, | |
| 94 const ResponseCode response_code); | |
| 95 // Registers a simple text response with automatically generated unique url. | |
| 96 // The response code is always 200. | |
| 97 virtual GURL RegisterTextResponse(const std::string& content, | |
| 98 const std::string& content_type); | |
| 99 | |
| 100 // Registers a simple file response. The file is loaded into memory. | |
| 101 virtual 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 // Registers a simple file response with automatically generated unique url. | |
| 107 // The response code is always 200. | |
| 108 virtual GURL RegisterFileResponse( | |
| 109 const FilePath& file_path, | |
| 110 const std::string& content_type); | |
| 111 | |
| 112 private: | |
| 113 // Creates and starts server and attaches it to |socket_descriptor| socket. | |
| 114 // Passed |port| should be a valid port name, on which the socket is | |
| 115 // listening. Use factory methods to create an instance of the server. | |
| 116 HttpTestServer(int port, const SocketDescriptor& socket_descriptor); | |
| 117 | |
| 118 // Handles a request when it is parsed. It passes the request to registed | |
| 119 // request handlers and sends a http response. | |
| 120 virtual void HandleRequest(HttpConnection* connection, | |
| 121 scoped_ptr<HttpRequest> request); | |
| 122 | |
| 123 // Generates unique identifier to make unique URL addresses. | |
| 124 virtual std::string GenerateUniqueIdentifier(); | |
| 125 | |
| 126 // |server| is the original listening Socket, connection is the new | |
| 127 // Socket that was created. Ownership of |connection| is transferred | |
| 128 // to the delegate with this call. | |
| 129 virtual void DidAccept(net::StreamListenSocket* server, | |
| 130 net::StreamListenSocket* connection) OVERRIDE; | |
| 131 virtual void DidRead(net::StreamListenSocket* connection, | |
| 132 const char* data, | |
| 133 int length) OVERRIDE; | |
| 134 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE; | |
| 135 | |
| 136 virtual HttpConnection* FindConnection(net::StreamListenSocket* socket); | |
| 137 | |
| 138 scoped_refptr<HttpListenSocket> listen_socket_; | |
| 139 int port_; | |
| 140 GURL base_url_; | |
| 141 int last_unique_id_; | |
| 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 base::WeakPtrFactory<HttpTestServer> weak_factory_; | |
| 150 DISALLOW_COPY_AND_ASSIGN(HttpTestServer); | |
| 151 }; | |
| 152 | |
| 153 } // namespace test_servers | |
| 154 } // namespace drive | |
| 155 | |
| 156 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_TEST_SERVER_H_ | |
| OLD | NEW |