From 0f53f5628ec01d743afade7de5e8ae350ca6880b Mon Sep 17 00:00:00 2001 From: "stoyan@chromium.org" Date: Wed, 9 Jun 2010 16:54:42 +0000 Subject: Yet another test web server. Supports speed throttle and dump of the traffic. Allows binding on 0.0.0.0 to allow connections from a VM (for example). Simple GMock class and Actions. Review URL: http://codereview.chromium.org/2620006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49266 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome_frame/test/test_server.h | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'chrome_frame/test/test_server.h') diff --git a/chrome_frame/test/test_server.h b/chrome_frame/test/test_server.h index 58fd160..b827ceb 100644 --- a/chrome_frame/test/test_server.h +++ b/chrome_frame/test/test_server.h @@ -321,6 +321,83 @@ class SimpleWebServer : public ListenSocket::ListenSocketDelegate { DISALLOW_COPY_AND_ASSIGN(SimpleWebServer); }; +// Simple class holding incoming HTTP request. Can send the HTTP response +// at different rate - small chunks, on regular interval. +class ConfigurableConnection : public base::RefCounted { + public: + struct SendOptions { + enum Speed { IMMEDIATE, DELAYED, IMMEDIATE_HEADERS_DELAYED_CONTENT }; + SendOptions() : speed_(IMMEDIATE), chunk_size_(0), timeout_(0) { } + SendOptions(Speed speed, int chunk_size, int64 timeout) + : speed_(speed), chunk_size_(chunk_size), timeout_(timeout) { + } + + Speed speed_; + int chunk_size_; + int64 timeout_; + }; + + explicit ConfigurableConnection(ListenSocket* sock) + : socket_(sock), cur_pos_(0) { } + + // Send HTTP response with provided |headers| and |content|. Appends + // "Context-Length:" header if the |content| is not empty. + void Send(const std::string& headers, const std::string& content); + + // Send HTTP response with provided |headers| and |content|. Appends + // "Context-Length:" header if the |content| is not empty. + // Use the |options| to tweak the network speed behaviour. + void SendWithOptions(const std::string& headers, const std::string& content, + const SendOptions& options); + + private: + friend class HTTPTestServer; + // Sends a chunk of the response and queues itself as a task for sending + // next chunk of |data_|. + void SendChunk(); + + scoped_refptr socket_; + Request r_; + SendOptions options_; + std::string data_; + int cur_pos_; + + DISALLOW_COPY_AND_ASSIGN(ConfigurableConnection); +}; + +// Simple class used as a base class for mock webserver. +// Override virtual functions Get and Post and use passed ConfigurableConnection +// instance to send the response. +class HTTPTestServer : public ListenSocket::ListenSocketDelegate { + public: + explicit HTTPTestServer(int port, const char* address); + virtual ~HTTPTestServer(); + // HTTP GET request is received. Override in derived classes. + // |connection| can be used to send the response. + virtual void Get(ConfigurableConnection* connection, + const std::string& path, const Request& r) = 0; + // HTTP POST request is received. Override in derived classes. + // |connection| can be used to send the response + virtual void Post(ConfigurableConnection* connection, + const std::string& path, const Request& r) = 0; + +private: + typedef std::list > ConnectionList; + ConnectionList::iterator FindConnection(const ListenSocket* socket); + scoped_refptr ConnectionFromSocket( + const ListenSocket* socket); + + // ListenSocketDelegate overrides. + virtual void DidAccept(ListenSocket* server, ListenSocket* socket); + virtual void DidRead(ListenSocket* socket, const std::string& data); + virtual void DidClose(ListenSocket* socket); + + scoped_refptr server_; + ConnectionList connection_list_; + DISALLOW_COPY_AND_ASSIGN(HTTPTestServer); +}; + + } // namespace test_server #endif // CHROME_FRAME_TEST_TEST_SERVER_H_ -- cgit v1.1