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

Unified Diff: chrome/browser/chromeos/gdata/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: Addressed comments. Created 8 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/gdata/test_servers/http_test_server.h
diff --git a/chrome/browser/chromeos/gdata/test_servers/http_test_server.h b/chrome/browser/chromeos/gdata/test_servers/http_test_server.h
new file mode 100644
index 0000000000000000000000000000000000000000..73972427833bf8ede94e6b3fdbbebfa9a92f98f2
--- /dev/null
+++ b/chrome/browser/chromeos/gdata/test_servers/http_test_server.h
@@ -0,0 +1,147 @@
+// 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_GDATA_TEST_SERVERS_HTTP_TEST_SERVER_H_
+#define CHROME_BROWSER_CHROMEOS_GDATA_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 "chrome/browser/chromeos/gdata/test_servers/http_connection.h"
+#include "chrome/browser/chromeos/gdata/test_servers/http_response.h"
+#include "chrome/browser/chromeos/gdata/test_servers/http_server.h"
+
+namespace gdata {
+namespace test_servers {
+
+// Interface for general request handlers.
+class ResponseProviderInterface {
+ public:
+ ResponseProviderInterface() {}
+ virtual ~ResponseProviderInterface() {}
+
+ // If returns true, then ProcessRequest will be called. If false, then
+ // other request handlers will be tried (if available).
+ // Note, that if another registered response provider handler the request
+ // earlier, then this may not be called at all.
+ virtual bool CanHandleRequest(HttpRequest* request) = 0;
+
+ // Should return a valid HTTP response. It will be passed back to client.
+ // This method is called only if previous CanHandleRequest returns true.
+ virtual scoped_ptr<HttpResponse> ProcessRequest(
+ HttpRequest* request) = 0;
+};
+
+// This class is a default implementation of ResponseProviderInterface which
+// always returns the same predefined response if the |relative_path|
+// is matching the request.
+class DefaultResponseProvider: public ResponseProviderInterface {
+ public:
+ DefaultResponseProvider(const GURL& request_url,
+ const HttpResponse& default_response);
+ virtual ~DefaultResponseProvider();
+
+ virtual bool CanHandleRequest(HttpRequest* request) OVERRIDE;
+ virtual scoped_ptr<HttpResponse> ProcessRequest(
+ HttpRequest* request) OVERRIDE;
+
+ private:
+ const GURL request_url_;
+ const HttpResponse default_response_;
+};
+
+// Class providing a HTTP server for testing purpose. 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>",
+// "text/html");
+// metadata_url = test_server->RegisterFileResponse(
+// "metadata/file.doc")
+// "testing/data/metadata.xml",
+// "text/xml",
+// 200);
+// }
+class HttpTestServer: private HttpConnectionDelegate {
+ public:
+ // If we want to start the server pass true. Otherwise, you have to call
+ // the Start() method.
+ explicit HttpTestServer(bool start_on_create);
+ virtual ~HttpTestServer();
+
+ // Starts the HTTP server on 127.0.0.1 ip, random port. If the port is not
+ // available, another one is tried. If all the retries fails, then false
+ // is returned. On success - true.
+ bool Start();
+
+ // Provides URL to the server which is useful when general purpose provider
+ // is registered.
+ virtual GURL GetBaseURL();
+
+ // Provider URL to a resource causing a specified response code.
+ virtual GURL GetErrorURL(RESPONSE_CODE code);
+
+ // The most general purpose method. Any request processing can be added using
+ // this method. Takes ownership of the object.
+ virtual void RegisterResponseProvider(ResponseProviderInterface& provider);
+
+ // 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 RESPONSE_CODE 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 RESPONSE_CODE 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:
+ // Implements HttpConnectionDelegate.
+ virtual bool HandleRequest(HttpConnection& connection,
+ scoped_ptr<HttpRequest> request) OVERRIDE;
+
+ // Generates unique identifier to make unique urls.
+ virtual std::string GenerateUniqueIdentifier();
+
+ int port_;
+ GURL base_url_;
+ int last_unique_id_;
+ scoped_refptr<HttpServer> server_;
+ // Owns the objects in the collection.
+ std::vector<ResponseProviderInterface*> response_providers_;
+
+ DISALLOW_COPY_AND_ASSIGN(HttpTestServer);
+};
+
+} // namespace test_servers
+} // namespace gdata
+
+#endif // CHROME_BROWSER_CHROMEOS_GDATA_TEST_SERVERS_HTTP_TEST_SERVER_H_

Powered by Google App Engine
This is Rietveld 408576698