| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 private: | 100 private: |
| 101 DISALLOW_COPY_AND_ASSIGN(Request); | 101 DISALLOW_COPY_AND_ASSIGN(Request); |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 // Manages request headers for a single request. | 104 // Manages request headers for a single request. |
| 105 // For each successful request that's made, the server will keep an instance | 105 // For each successful request that's made, the server will keep an instance |
| 106 // of this class so that they can be checked even after the server has been | 106 // of this class so that they can be checked even after the server has been |
| 107 // shut down. | 107 // shut down. |
| 108 class Connection { | 108 class Connection { |
| 109 public: | 109 public: |
| 110 explicit Connection(net::StreamListenSocket* sock) : socket_(sock) { | 110 explicit Connection(scoped_ptr<net::StreamListenSocket> sock) |
| 111 : socket_(sock.Pass()) { |
| 111 } | 112 } |
| 112 | 113 |
| 113 ~Connection() { | 114 ~Connection() { |
| 114 } | 115 } |
| 115 | 116 |
| 116 bool IsSame(const net::StreamListenSocket* socket) const { | 117 bool IsSame(const net::StreamListenSocket* socket) const { |
| 117 return socket_ == socket; | 118 return socket_ == socket; |
| 118 } | 119 } |
| 119 | 120 |
| 120 const Request& request() const { | 121 const Request& request() const { |
| 121 return request_; | 122 return request_; |
| 122 } | 123 } |
| 123 | 124 |
| 124 Request& request() { | 125 Request& request() { |
| 125 return request_; | 126 return request_; |
| 126 } | 127 } |
| 127 | 128 |
| 128 void OnSocketClosed() { | 129 void OnSocketClosed() { |
| 129 socket_ = NULL; | 130 socket_.reset(); |
| 130 } | 131 } |
| 131 | 132 |
| 132 protected: | 133 protected: |
| 133 scoped_refptr<net::StreamListenSocket> socket_; | 134 scoped_ptr<net::StreamListenSocket> socket_; |
| 134 Request request_; | 135 Request request_; |
| 135 | 136 |
| 136 private: | 137 private: |
| 137 DISALLOW_COPY_AND_ASSIGN(Connection); | 138 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 138 }; | 139 }; |
| 139 | 140 |
| 140 // Abstract interface with default implementations for some of the methods and | 141 // Abstract interface with default implementations for some of the methods and |
| 141 // a counter for how many times the response object has served requests. | 142 // a counter for how many times the response object has served requests. |
| 142 class Response { | 143 class Response { |
| 143 public: | 144 public: |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 // Ownership of response objects is by default assumed to be outside | 297 // Ownership of response objects is by default assumed to be outside |
| 297 // of the SimpleWebServer class. | 298 // of the SimpleWebServer class. |
| 298 // However, if the caller doesn't wish to maintain a list of response objects | 299 // However, if the caller doesn't wish to maintain a list of response objects |
| 299 // but rather let this class hold the only references to those objects, | 300 // but rather let this class hold the only references to those objects, |
| 300 // the caller can call this method to delete the objects as part of | 301 // the caller can call this method to delete the objects as part of |
| 301 // the cleanup process. | 302 // the cleanup process. |
| 302 void DeleteAllResponses(); | 303 void DeleteAllResponses(); |
| 303 | 304 |
| 304 // StreamListenSocket::Delegate overrides. | 305 // StreamListenSocket::Delegate overrides. |
| 305 virtual void DidAccept(net::StreamListenSocket* server, | 306 virtual void DidAccept(net::StreamListenSocket* server, |
| 306 net::StreamListenSocket* connection); | 307 scoped_ptr<net::StreamListenSocket> connection); |
| 307 virtual void DidRead(net::StreamListenSocket* connection, | 308 virtual void DidRead(net::StreamListenSocket* connection, |
| 308 const char* data, | 309 const char* data, |
| 309 int len); | 310 int len); |
| 310 virtual void DidClose(net::StreamListenSocket* sock); | 311 virtual void DidClose(net::StreamListenSocket* sock); |
| 311 | 312 |
| 312 // Returns the host on which the server is listening. This is suitable for | 313 // Returns the host on which the server is listening. This is suitable for |
| 313 // use in URLs for resources served by this instance. | 314 // use in URLs for resources served by this instance. |
| 314 const std::string& host() const { | 315 const std::string& host() const { |
| 315 return host_; | 316 return host_; |
| 316 } | 317 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 329 virtual void WriteContents(net::StreamListenSocket* socket) const { | 330 virtual void WriteContents(net::StreamListenSocket* socket) const { |
| 330 SimpleResponse::WriteContents(socket); | 331 SimpleResponse::WriteContents(socket); |
| 331 base::MessageLoop::current()->Quit(); | 332 base::MessageLoop::current()->Quit(); |
| 332 } | 333 } |
| 333 }; | 334 }; |
| 334 | 335 |
| 335 Response* FindResponse(const Request& request) const; | 336 Response* FindResponse(const Request& request) const; |
| 336 Connection* FindConnection(const net::StreamListenSocket* socket) const; | 337 Connection* FindConnection(const net::StreamListenSocket* socket) const; |
| 337 | 338 |
| 338 std::string host_; | 339 std::string host_; |
| 339 scoped_refptr<net::StreamListenSocket> server_; | 340 scoped_ptr<net::StreamListenSocket> server_; |
| 340 ConnectionList connections_; | 341 ConnectionList connections_; |
| 341 std::list<Response*> responses_; | 342 std::list<Response*> responses_; |
| 342 QuitResponse quit_; | 343 QuitResponse quit_; |
| 343 | 344 |
| 344 private: | 345 private: |
| 345 void Construct(const std::string& address, int port); | 346 void Construct(const std::string& address, int port); |
| 346 DISALLOW_COPY_AND_ASSIGN(SimpleWebServer); | 347 DISALLOW_COPY_AND_ASSIGN(SimpleWebServer); |
| 347 }; | 348 }; |
| 348 | 349 |
| 349 // Simple class holding incoming HTTP request. Can send the HTTP response | 350 // Simple class holding incoming HTTP request. Can send the HTTP response |
| 350 // at different rate - small chunks, on regular interval. | 351 // at different rate - small chunks, on regular interval. |
| 351 class ConfigurableConnection : public base::RefCounted<ConfigurableConnection> { | 352 class ConfigurableConnection : public base::RefCounted<ConfigurableConnection> { |
| 352 public: | 353 public: |
| 353 struct SendOptions { | 354 struct SendOptions { |
| 354 enum Speed { IMMEDIATE, DELAYED, IMMEDIATE_HEADERS_DELAYED_CONTENT }; | 355 enum Speed { IMMEDIATE, DELAYED, IMMEDIATE_HEADERS_DELAYED_CONTENT }; |
| 355 SendOptions() : speed_(IMMEDIATE), chunk_size_(0), timeout_(0) { } | 356 SendOptions() : speed_(IMMEDIATE), chunk_size_(0), timeout_(0) { } |
| 356 SendOptions(Speed speed, int chunk_size, int64 timeout) | 357 SendOptions(Speed speed, int chunk_size, int64 timeout) |
| 357 : speed_(speed), chunk_size_(chunk_size), timeout_(timeout) { | 358 : speed_(speed), chunk_size_(chunk_size), timeout_(timeout) { |
| 358 } | 359 } |
| 359 | 360 |
| 360 Speed speed_; | 361 Speed speed_; |
| 361 int chunk_size_; | 362 int chunk_size_; |
| 362 int64 timeout_; | 363 int64 timeout_; |
| 363 }; | 364 }; |
| 364 | 365 |
| 365 explicit ConfigurableConnection(net::StreamListenSocket* sock) | 366 explicit ConfigurableConnection(scoped_ptr<net::StreamListenSocket> sock) |
| 366 : socket_(sock), | 367 : socket_(sock.Pass()), |
| 367 cur_pos_(0) {} | 368 cur_pos_(0) {} |
| 368 | 369 |
| 369 // Send HTTP response with provided |headers| and |content|. Appends | 370 // Send HTTP response with provided |headers| and |content|. Appends |
| 370 // "Context-Length:" header if the |content| is not empty. | 371 // "Context-Length:" header if the |content| is not empty. |
| 371 void Send(const std::string& headers, const std::string& content); | 372 void Send(const std::string& headers, const std::string& content); |
| 372 | 373 |
| 373 // Send HTTP response with provided |headers| and |content|. Appends | 374 // Send HTTP response with provided |headers| and |content|. Appends |
| 374 // "Context-Length:" header if the |content| is not empty. | 375 // "Context-Length:" header if the |content| is not empty. |
| 375 // Use the |options| to tweak the network speed behaviour. | 376 // Use the |options| to tweak the network speed behaviour. |
| 376 void SendWithOptions(const std::string& headers, const std::string& content, | 377 void SendWithOptions(const std::string& headers, const std::string& content, |
| 377 const SendOptions& options); | 378 const SendOptions& options); |
| 378 | 379 |
| 379 private: | 380 private: |
| 380 friend class HTTPTestServer; | 381 friend class HTTPTestServer; |
| 381 // Sends a chunk of the response and queues itself as a task for sending | 382 // Sends a chunk of the response and queues itself as a task for sending |
| 382 // next chunk of |data_|. | 383 // next chunk of |data_|. |
| 383 void SendChunk(); | 384 void SendChunk(); |
| 384 | 385 |
| 385 // Closes the connection by releasing this instance's reference on its socket. | 386 // Closes the connection by releasing this instance's reference on its socket. |
| 386 void Close(); | 387 void Close(); |
| 387 | 388 |
| 388 scoped_refptr<net::StreamListenSocket> socket_; | 389 scoped_ptr<net::StreamListenSocket> socket_; |
| 389 Request r_; | 390 Request r_; |
| 390 SendOptions options_; | 391 SendOptions options_; |
| 391 std::string data_; | 392 std::string data_; |
| 392 int cur_pos_; | 393 int cur_pos_; |
| 393 | 394 |
| 394 DISALLOW_COPY_AND_ASSIGN(ConfigurableConnection); | 395 DISALLOW_COPY_AND_ASSIGN(ConfigurableConnection); |
| 395 }; | 396 }; |
| 396 | 397 |
| 397 // Simple class used as a base class for mock webserver. | 398 // Simple class used as a base class for mock webserver. |
| 398 // Override virtual functions Get and Post and use passed ConfigurableConnection | 399 // Override virtual functions Get and Post and use passed ConfigurableConnection |
| (...skipping 26 matching lines...) Expand all Loading... |
| 425 | 426 |
| 426 private: | 427 private: |
| 427 typedef std::list<scoped_refptr<ConfigurableConnection> > ConnectionList; | 428 typedef std::list<scoped_refptr<ConfigurableConnection> > ConnectionList; |
| 428 ConnectionList::iterator FindConnection( | 429 ConnectionList::iterator FindConnection( |
| 429 const net::StreamListenSocket* socket); | 430 const net::StreamListenSocket* socket); |
| 430 scoped_refptr<ConfigurableConnection> ConnectionFromSocket( | 431 scoped_refptr<ConfigurableConnection> ConnectionFromSocket( |
| 431 const net::StreamListenSocket* socket); | 432 const net::StreamListenSocket* socket); |
| 432 | 433 |
| 433 // StreamListenSocket::Delegate overrides. | 434 // StreamListenSocket::Delegate overrides. |
| 434 virtual void DidAccept(net::StreamListenSocket* server, | 435 virtual void DidAccept(net::StreamListenSocket* server, |
| 435 net::StreamListenSocket* socket); | 436 scoped_ptr<net::StreamListenSocket> socket); |
| 436 virtual void DidRead(net::StreamListenSocket* socket, | 437 virtual void DidRead(net::StreamListenSocket* socket, |
| 437 const char* data, int len); | 438 const char* data, int len); |
| 438 virtual void DidClose(net::StreamListenSocket* socket); | 439 virtual void DidClose(net::StreamListenSocket* socket); |
| 439 | 440 |
| 440 scoped_refptr<net::StreamListenSocket> server_; | 441 scoped_ptr<net::StreamListenSocket> server_; |
| 441 ConnectionList connection_list_; | 442 ConnectionList connection_list_; |
| 442 | 443 |
| 443 DISALLOW_COPY_AND_ASSIGN(HTTPTestServer); | 444 DISALLOW_COPY_AND_ASSIGN(HTTPTestServer); |
| 444 }; | 445 }; |
| 445 | 446 |
| 446 } // namespace test_server | 447 } // namespace test_server |
| 447 | 448 |
| 448 #endif // CHROME_FRAME_TEST_TEST_SERVER_H_ | 449 #endif // CHROME_FRAME_TEST_TEST_SERVER_H_ |
| OLD | NEW |