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