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
3 // found in the LICENSE file.
9 #include "base/compiler_specific.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/run_loop.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/threading/thread.h"
18 #include "base/time/time.h"
19 #include "chrome/test/chromedriver/net/test_http_server.h"
20 #include "chrome/test/chromedriver/net/websocket.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
27 void OnConnectFinished(base::RunLoop
* run_loop
, int* save_error
, int error
) {
32 void RunPending(base::MessageLoop
* loop
) {
33 base::RunLoop run_loop
;
34 loop
->PostTask(FROM_HERE
, run_loop
.QuitClosure());
38 class Listener
: public WebSocketListener
{
40 explicit Listener(const std::vector
<std::string
>& messages
)
41 : messages_(messages
) {}
44 EXPECT_TRUE(messages_
.empty());
47 virtual void OnMessageReceived(const std::string
& message
) override
{
48 ASSERT_TRUE(messages_
.size());
49 EXPECT_EQ(messages_
[0], message
);
50 messages_
.erase(messages_
.begin());
51 if (messages_
.empty())
52 base::MessageLoop::current()->Quit();
55 virtual void OnClose() override
{
60 std::vector
<std::string
> messages_
;
63 class CloseListener
: public WebSocketListener
{
65 explicit CloseListener(base::RunLoop
* run_loop
)
66 : run_loop_(run_loop
) {}
68 virtual ~CloseListener() {
69 EXPECT_FALSE(run_loop_
);
72 virtual void OnMessageReceived(const std::string
& message
) override
{}
74 virtual void OnClose() override
{
75 EXPECT_TRUE(run_loop_
);
82 base::RunLoop
* run_loop_
;
85 class WebSocketTest
: public testing::Test
{
88 virtual ~WebSocketTest() {}
90 virtual void SetUp() override
{
91 ASSERT_TRUE(server_
.Start());
94 virtual void TearDown() override
{
99 scoped_ptr
<WebSocket
> CreateWebSocket(const GURL
& url
,
100 WebSocketListener
* listener
) {
102 scoped_ptr
<WebSocket
> sock(new WebSocket(url
, listener
));
103 base::RunLoop run_loop
;
104 sock
->Connect(base::Bind(&OnConnectFinished
, &run_loop
, &error
));
105 loop_
.PostDelayedTask(
106 FROM_HERE
, run_loop
.QuitClosure(),
107 base::TimeDelta::FromSeconds(10));
109 if (error
== net::OK
)
111 return scoped_ptr
<WebSocket
>();
114 scoped_ptr
<WebSocket
> CreateConnectedWebSocket(WebSocketListener
* listener
) {
115 return CreateWebSocket(server_
.web_socket_url(), listener
);
118 void SendReceive(const std::vector
<std::string
>& messages
) {
119 Listener
listener(messages
);
120 scoped_ptr
<WebSocket
> sock(CreateConnectedWebSocket(&listener
));
122 for (size_t i
= 0; i
< messages
.size(); ++i
) {
123 ASSERT_TRUE(sock
->Send(messages
[i
]));
125 base::RunLoop run_loop
;
126 loop_
.PostDelayedTask(
127 FROM_HERE
, run_loop
.QuitClosure(),
128 base::TimeDelta::FromSeconds(10));
132 base::MessageLoopForIO loop_
;
133 TestHttpServer server_
;
138 TEST_F(WebSocketTest
, CreateDestroy
) {
139 CloseListener
listener(NULL
);
140 WebSocket
sock(GURL("ws://127.0.0.1:2222"), &listener
);
143 TEST_F(WebSocketTest
, Connect
) {
144 CloseListener
listener(NULL
);
145 ASSERT_TRUE(CreateWebSocket(server_
.web_socket_url(), &listener
));
147 ASSERT_TRUE(server_
.WaitForConnectionsToClose());
150 TEST_F(WebSocketTest
, ConnectNoServer
) {
151 CloseListener
listener(NULL
);
152 ASSERT_FALSE(CreateWebSocket(GURL("ws://127.0.0.1:33333"), NULL
));
155 TEST_F(WebSocketTest
, Connect404
) {
156 server_
.SetRequestAction(TestHttpServer::kNotFound
);
157 CloseListener
listener(NULL
);
158 ASSERT_FALSE(CreateWebSocket(server_
.web_socket_url(), NULL
));
160 ASSERT_TRUE(server_
.WaitForConnectionsToClose());
163 TEST_F(WebSocketTest
, ConnectServerClosesConn
) {
164 server_
.SetRequestAction(TestHttpServer::kClose
);
165 CloseListener
listener(NULL
);
166 ASSERT_FALSE(CreateWebSocket(server_
.web_socket_url(), &listener
));
169 TEST_F(WebSocketTest
, CloseOnReceive
) {
170 server_
.SetMessageAction(TestHttpServer::kCloseOnMessage
);
171 base::RunLoop run_loop
;
172 CloseListener
listener(&run_loop
);
173 scoped_ptr
<WebSocket
> sock(CreateConnectedWebSocket(&listener
));
175 ASSERT_TRUE(sock
->Send("hi"));
176 loop_
.PostDelayedTask(
177 FROM_HERE
, run_loop
.QuitClosure(),
178 base::TimeDelta::FromSeconds(10));
182 TEST_F(WebSocketTest
, CloseOnSend
) {
183 base::RunLoop run_loop
;
184 CloseListener
listener(&run_loop
);
185 scoped_ptr
<WebSocket
> sock(CreateConnectedWebSocket(&listener
));
190 loop_
.PostDelayedTask(
191 FROM_HERE
, run_loop
.QuitClosure(),
192 base::TimeDelta::FromSeconds(10));
194 ASSERT_FALSE(sock
->Send("hi"));
197 TEST_F(WebSocketTest
, SendReceive
) {
198 std::vector
<std::string
> messages
;
199 messages
.push_back("hello");
200 SendReceive(messages
);
203 TEST_F(WebSocketTest
, SendReceiveLarge
) {
204 std::vector
<std::string
> messages
;
205 messages
.push_back(std::string(10 << 20, 'a'));
206 SendReceive(messages
);
209 TEST_F(WebSocketTest
, SendReceiveMultiple
) {
210 std::vector
<std::string
> messages
;
211 messages
.push_back("1");
212 messages
.push_back("2");
213 messages
.push_back("3");
214 SendReceive(messages
);