OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_FRAME_TEST_TEST_SERVER_H_ | 5 #ifndef CHROME_FRAME_TEST_TEST_SERVER_H_ |
6 #define CHROME_FRAME_TEST_TEST_SERVER_H_ | 6 #define CHROME_FRAME_TEST_TEST_SERVER_H_ |
7 | 7 |
8 // Implementation of an HTTP server for tests. | 8 // Implementation of an HTTP server for tests. |
9 // To instantiate the server, make sure you have a message loop on the | 9 // To instantiate the server, make sure you have a message loop on the |
10 // current thread and then create an instance of the SimpleWebServer class. | 10 // current thread and then create an instance of the SimpleWebServer class. |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 protected: | 314 protected: |
315 scoped_refptr<ListenSocket> server_; | 315 scoped_refptr<ListenSocket> server_; |
316 ConnectionList connections_; | 316 ConnectionList connections_; |
317 std::list<Response*> responses_; | 317 std::list<Response*> responses_; |
318 QuitResponse quit_; | 318 QuitResponse quit_; |
319 | 319 |
320 private: | 320 private: |
321 DISALLOW_COPY_AND_ASSIGN(SimpleWebServer); | 321 DISALLOW_COPY_AND_ASSIGN(SimpleWebServer); |
322 }; | 322 }; |
323 | 323 |
| 324 // Simple class holding incoming HTTP request. Can send the HTTP response |
| 325 // at different rate - small chunks, on regular interval. |
| 326 class ConfigurableConnection : public base::RefCounted<ConfigurableConnection> { |
| 327 public: |
| 328 struct SendOptions { |
| 329 enum Speed { IMMEDIATE, DELAYED, IMMEDIATE_HEADERS_DELAYED_CONTENT }; |
| 330 SendOptions() : speed_(IMMEDIATE), chunk_size_(0), timeout_(0) { } |
| 331 SendOptions(Speed speed, int chunk_size, int64 timeout) |
| 332 : speed_(speed), chunk_size_(chunk_size), timeout_(timeout) { |
| 333 } |
| 334 |
| 335 Speed speed_; |
| 336 int chunk_size_; |
| 337 int64 timeout_; |
| 338 }; |
| 339 |
| 340 explicit ConfigurableConnection(ListenSocket* sock) |
| 341 : socket_(sock), cur_pos_(0) { } |
| 342 |
| 343 // Send HTTP response with provided |headers| and |content|. Appends |
| 344 // "Context-Length:" header if the |content| is not empty. |
| 345 void Send(const std::string& headers, const std::string& content); |
| 346 |
| 347 // Send HTTP response with provided |headers| and |content|. Appends |
| 348 // "Context-Length:" header if the |content| is not empty. |
| 349 // Use the |options| to tweak the network speed behaviour. |
| 350 void SendWithOptions(const std::string& headers, const std::string& content, |
| 351 const SendOptions& options); |
| 352 |
| 353 private: |
| 354 friend class HTTPTestServer; |
| 355 // Sends a chunk of the response and queues itself as a task for sending |
| 356 // next chunk of |data_|. |
| 357 void SendChunk(); |
| 358 |
| 359 scoped_refptr<ListenSocket> socket_; |
| 360 Request r_; |
| 361 SendOptions options_; |
| 362 std::string data_; |
| 363 int cur_pos_; |
| 364 |
| 365 DISALLOW_COPY_AND_ASSIGN(ConfigurableConnection); |
| 366 }; |
| 367 |
| 368 // Simple class used as a base class for mock webserver. |
| 369 // Override virtual functions Get and Post and use passed ConfigurableConnection |
| 370 // instance to send the response. |
| 371 class HTTPTestServer : public ListenSocket::ListenSocketDelegate { |
| 372 public: |
| 373 explicit HTTPTestServer(int port, const char* address); |
| 374 virtual ~HTTPTestServer(); |
| 375 // HTTP GET request is received. Override in derived classes. |
| 376 // |connection| can be used to send the response. |
| 377 virtual void Get(ConfigurableConnection* connection, |
| 378 const std::string& path, const Request& r) = 0; |
| 379 // HTTP POST request is received. Override in derived classes. |
| 380 // |connection| can be used to send the response |
| 381 virtual void Post(ConfigurableConnection* connection, |
| 382 const std::string& path, const Request& r) = 0; |
| 383 |
| 384 private: |
| 385 typedef std::list<scoped_refptr<ConfigurableConnection> > ConnectionList; |
| 386 ConnectionList::iterator FindConnection(const ListenSocket* socket); |
| 387 scoped_refptr<ConfigurableConnection> ConnectionFromSocket( |
| 388 const ListenSocket* socket); |
| 389 |
| 390 // ListenSocketDelegate overrides. |
| 391 virtual void DidAccept(ListenSocket* server, ListenSocket* socket); |
| 392 virtual void DidRead(ListenSocket* socket, const std::string& data); |
| 393 virtual void DidClose(ListenSocket* socket); |
| 394 |
| 395 scoped_refptr<ListenSocket> server_; |
| 396 ConnectionList connection_list_; |
| 397 DISALLOW_COPY_AND_ASSIGN(HTTPTestServer); |
| 398 }; |
| 399 |
| 400 |
324 } // namespace test_server | 401 } // namespace test_server |
325 | 402 |
326 #endif // CHROME_FRAME_TEST_TEST_SERVER_H_ | 403 #endif // CHROME_FRAME_TEST_TEST_SERVER_H_ |
OLD | NEW |