| 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..0e59224eae44e0c961b8ac93a800646c3441923c
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/drive/test_servers/http_test_server.h
|
| @@ -0,0 +1,154 @@
|
| +// 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);
|
| + virtual void Listen();
|
| +
|
| + private:
|
| + virtual ~HttpListenSocket();
|
| +};
|
| +
|
| +// 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>",
|
| +// "text/html");
|
| +// metadata_url = test_server->RegisterFileResponse(
|
| +// "metadata/file.doc")
|
| +// "testing/data/metadata.xml",
|
| +// "text/xml",
|
| +// 200);
|
| +// }
|
| +class HttpTestServer : private net::StreamListenSocket::Delegate {
|
| + 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();
|
| +
|
| + // 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);
|
| +
|
| + // 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_
|
|
|