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