• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_TEST_CHROMEDRIVER_NET_TEST_HTTP_SERVER_H_
6 #define CHROME_TEST_CHROMEDRIVER_NET_TEST_HTTP_SERVER_H_
7 
8 #include <set>
9 
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/lock.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "net/server/http_server.h"
17 #include "url/gurl.h"
18 
19 namespace base {
20 class WaitableEvent;
21 }
22 
23 // HTTP server for web socket testing purposes that runs on its own thread.
24 // All public methods are thread safe and may be called on any thread, unless
25 // noted otherwise.
26 class TestHttpServer : public net::HttpServer::Delegate {
27  public:
28   enum WebSocketRequestAction {
29     kAccept,
30     kNotFound,
31     kClose,
32   };
33 
34   enum WebSocketMessageAction {
35     kEchoMessage,
36     kCloseOnMessage
37   };
38 
39   // Creates an http server. By default it accepts WebSockets and echoes
40   // WebSocket messages back.
41   TestHttpServer();
42   virtual ~TestHttpServer();
43 
44   // Starts the server. Returns whether it was started successfully.
45   bool Start();
46 
47   // Stops the server. May be called multiple times.
48   void Stop();
49 
50   // Waits until all open connections are closed. Returns true if all
51   // connections are closed, or false if a timeout is exceeded.
52   bool WaitForConnectionsToClose();
53 
54   // Sets the action to perform when receiving a WebSocket connect request.
55   void SetRequestAction(WebSocketRequestAction action);
56 
57   // Sets the action to perform when receiving a WebSocket message.
58   void SetMessageAction(WebSocketMessageAction action);
59 
60   // Returns the web socket URL that points to the server.
61   GURL web_socket_url() const;
62 
63   // Overridden from net::HttpServer::Delegate:
64   virtual void OnConnect(int connection_id) OVERRIDE;
OnHttpRequest(int connection_id,const net::HttpServerRequestInfo & info)65   virtual void OnHttpRequest(int connection_id,
66                              const net::HttpServerRequestInfo& info) OVERRIDE {}
67   virtual void OnWebSocketRequest(
68       int connection_id,
69       const net::HttpServerRequestInfo& info) OVERRIDE;
70   virtual void OnWebSocketMessage(int connection_id,
71                                   const std::string& data) OVERRIDE;
72   virtual void OnClose(int connection_id) OVERRIDE;
73 
74  private:
75   void StartOnServerThread(bool* success, base::WaitableEvent* event);
76   void StopOnServerThread(base::WaitableEvent* event);
77 
78   base::Thread thread_;
79 
80   // Access only on the server thread.
81   scoped_ptr<net::HttpServer> server_;
82 
83   // Access only on the server thread.
84   std::set<int> connections_;
85 
86   base::WaitableEvent all_closed_event_;
87 
88   // Protects |web_socket_url_|.
89   mutable base::Lock url_lock_;
90   GURL web_socket_url_;
91 
92   // Protects the action flags.
93   base::Lock action_lock_;
94   WebSocketRequestAction request_action_;
95   WebSocketMessageAction message_action_;
96 
97   DISALLOW_COPY_AND_ASSIGN(TestHttpServer);
98 };
99 
100 #endif  // CHROME_TEST_CHROMEDRIVER_NET_TEST_HTTP_SERVER_H_
101