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_GDATA_TEST_SERVERS_HTTP_TEST_SERVER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_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 "chrome/browser/chromeos/gdata/test_servers/http_connection.h" |
| 14 #include "chrome/browser/chromeos/gdata/test_servers/http_response.h" |
| 15 #include "chrome/browser/chromeos/gdata/test_servers/http_server.h" |
| 16 |
| 17 namespace gdata { |
| 18 namespace test_servers { |
| 19 |
| 20 // Interface for general request handlers. |
| 21 class ResponseProviderInterface { |
| 22 public: |
| 23 ResponseProviderInterface() {} |
| 24 virtual ~ResponseProviderInterface() {} |
| 25 |
| 26 // If returns true, then ProcessRequest will be called. If false, then |
| 27 // other request handlers will be tried (if available). |
| 28 // Note, that if another registered response provider handler the request |
| 29 // earlier, then this may not be called at all. |
| 30 virtual bool CanHandleRequest(HttpRequest* request) = 0; |
| 31 |
| 32 // Should return a valid HTTP response. It will be passed back to client. |
| 33 // This method is called only if previous CanHandleRequest returns true. |
| 34 virtual scoped_ptr<HttpResponse> ProcessRequest( |
| 35 HttpRequest* request) = 0; |
| 36 }; |
| 37 |
| 38 // This class is a default implementation of ResponseProviderInterface which |
| 39 // always returns the same predefined response if the |relative_path| |
| 40 // is matching the request. |
| 41 class DefaultResponseProvider: public ResponseProviderInterface { |
| 42 public: |
| 43 DefaultResponseProvider(const GURL& request_url, |
| 44 const HttpResponse& default_response); |
| 45 virtual ~DefaultResponseProvider(); |
| 46 |
| 47 virtual bool CanHandleRequest(HttpRequest* request) OVERRIDE; |
| 48 virtual scoped_ptr<HttpResponse> ProcessRequest( |
| 49 HttpRequest* request) OVERRIDE; |
| 50 |
| 51 private: |
| 52 const GURL request_url_; |
| 53 const HttpResponse default_response_; |
| 54 }; |
| 55 |
| 56 // Class providing a HTTP server for testing purpose. The common use case is |
| 57 // below: |
| 58 // |
| 59 // scoped_ptr<HttpTestServer> test_server_; |
| 60 // GURL hello_world_url_; |
| 61 // GURL file_url_; |
| 62 // (...) |
| 63 // void SetUp() { |
| 64 // test_server_.reset(new HttpTestServer()); |
| 65 // hello_world_url = test_server->RegisterTextResponse("<b>Hello world!</b>", |
| 66 // "text/html"); |
| 67 // metadata_url = test_server->RegisterFileResponse( |
| 68 // "metadata/file.doc") |
| 69 // "testing/data/metadata.xml", |
| 70 // "text/xml", |
| 71 // 200); |
| 72 // } |
| 73 class HttpTestServer: private HttpConnectionDelegate { |
| 74 public: |
| 75 // If we want to start the server pass true. Otherwise, you have to call |
| 76 // the Start() method. |
| 77 explicit HttpTestServer(bool start_on_create); |
| 78 virtual ~HttpTestServer(); |
| 79 |
| 80 // Starts the HTTP server on 127.0.0.1 ip, random port. If the port is not |
| 81 // available, another one is tried. If all the retries fails, then false |
| 82 // is returned. On success - true. |
| 83 bool Start(); |
| 84 |
| 85 // Provides URL to the server which is useful when general purpose provider |
| 86 // is registered. |
| 87 virtual GURL GetBaseURL(); |
| 88 |
| 89 // Provider URL to a resource causing a specified response code. |
| 90 virtual GURL GetErrorURL(RESPONSE_CODE code); |
| 91 |
| 92 // The most general purpose method. Any request processing can be added using |
| 93 // this method. Takes ownership of the object. |
| 94 virtual void RegisterResponseProvider(ResponseProviderInterface& provider); |
| 95 |
| 96 // Used to provide the same predefined response for the requests matching |
| 97 // the |relative_path|. Should be used if any custom data, such as additional |
| 98 // headers should be send from the server. |
| 99 virtual GURL RegisterDefaultResponse( |
| 100 const std::string& relative_path, |
| 101 const HttpResponse& default_response); |
| 102 |
| 103 // Registers a simple text response. |
| 104 virtual GURL RegisterTextResponse( |
| 105 const std::string& relative_path, |
| 106 const std::string& content, |
| 107 const std::string& content_type, |
| 108 const RESPONSE_CODE response_code); |
| 109 // Registers a simple text response with automatically generated unique url. |
| 110 // The response code is always 200. |
| 111 virtual GURL RegisterTextResponse(const std::string& content, |
| 112 const std::string& content_type); |
| 113 |
| 114 // Registers a simple file response. The file is loaded into memory. |
| 115 virtual GURL RegisterFileResponse( |
| 116 const std::string& relative_path, |
| 117 const FilePath& file_path, |
| 118 const std::string& content_type, |
| 119 const RESPONSE_CODE response_code); |
| 120 // Registers a simple file response with automatically generated unique url. |
| 121 // The response code is always 200. |
| 122 virtual GURL RegisterFileResponse( |
| 123 const FilePath& file_path, |
| 124 const std::string& content_type); |
| 125 |
| 126 private: |
| 127 // Implements HttpConnectionDelegate. |
| 128 virtual bool HandleRequest(HttpConnection& connection, |
| 129 scoped_ptr<HttpRequest> request) OVERRIDE; |
| 130 |
| 131 // Generates unique identifier to make unique urls. |
| 132 virtual std::string GenerateUniqueIdentifier(); |
| 133 |
| 134 int port_; |
| 135 GURL base_url_; |
| 136 int last_unique_id_; |
| 137 scoped_refptr<HttpServer> server_; |
| 138 // Owns the objects in the collection. |
| 139 std::vector<ResponseProviderInterface*> response_providers_; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(HttpTestServer); |
| 142 }; |
| 143 |
| 144 } // namespace test_servers |
| 145 } // namespace gdata |
| 146 |
| 147 #endif // CHROME_BROWSER_CHROMEOS_GDATA_TEST_SERVERS_HTTP_TEST_SERVER_H_ |
OLD | NEW |