1 // Copyright 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.
5 #include "content/browser/renderer_host/websocket_dispatcher_host.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/ref_counted.h"
12 #include "content/browser/renderer_host/websocket_host.h"
13 #include "content/common/websocket.h"
14 #include "content/common/websocket_messages.h"
15 #include "ipc/ipc_message.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/origin.h"
23 // A mock of WebsocketHost which records received messages.
24 class MockWebSocketHost
: public WebSocketHost
{
26 MockWebSocketHost(int routing_id
,
27 WebSocketDispatcherHost
* dispatcher
,
28 net::URLRequestContext
* url_request_context
)
29 : WebSocketHost(routing_id
, dispatcher
, url_request_context
) {
32 virtual ~MockWebSocketHost() {}
34 virtual bool OnMessageReceived(const IPC::Message
& message
,
35 bool* message_was_ok
) OVERRIDE
{
36 received_messages_
.push_back(message
);
40 std::vector
<IPC::Message
> received_messages_
;
43 class WebSocketDispatcherHostTest
: public ::testing::Test
{
45 WebSocketDispatcherHostTest() {
46 dispatcher_host_
= new WebSocketDispatcherHost(
48 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext
,
49 base::Unretained(this)),
50 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost
,
51 base::Unretained(this)));
54 virtual ~WebSocketDispatcherHostTest() {}
57 scoped_refptr
<WebSocketDispatcherHost
> dispatcher_host_
;
59 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of
61 std::vector
<MockWebSocketHost
*> mock_hosts_
;
64 net::URLRequestContext
* OnGetRequestContext() {
68 WebSocketHost
* CreateWebSocketHost(int routing_id
) {
69 MockWebSocketHost
* host
=
70 new MockWebSocketHost(routing_id
, dispatcher_host_
.get(), NULL
);
71 mock_hosts_
.push_back(host
);
76 TEST_F(WebSocketDispatcherHostTest
, Construct
) {
80 TEST_F(WebSocketDispatcherHostTest
, UnrelatedMessage
) {
81 bool message_was_ok
= false;
83 EXPECT_FALSE(dispatcher_host_
->OnMessageReceived(message
, &message_was_ok
));
86 TEST_F(WebSocketDispatcherHostTest
, AddChannelRequest
) {
88 GURL
socket_url("ws://example.com/test");
89 std::vector
<std::string
> requested_protocols
;
90 requested_protocols
.push_back("hello");
91 url::Origin
origin("http://example.com/test");
92 WebSocketHostMsg_AddChannelRequest
message(
93 routing_id
, socket_url
, requested_protocols
, origin
);
95 bool message_was_ok
= false;
96 ASSERT_TRUE(dispatcher_host_
->OnMessageReceived(message
, &message_was_ok
));
98 ASSERT_EQ(1U, mock_hosts_
.size());
99 MockWebSocketHost
* host
= mock_hosts_
[0];
101 ASSERT_EQ(1U, host
->received_messages_
.size());
102 const IPC::Message
& forwarded_message
= host
->received_messages_
[0];
103 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID
, forwarded_message
.type());
104 EXPECT_EQ(routing_id
, forwarded_message
.routing_id());
107 TEST_F(WebSocketDispatcherHostTest
, SendFrameButNoHostYet
) {
108 int routing_id
= 123;
109 std::vector
<char> data
;
110 WebSocketMsg_SendFrame
message(
111 routing_id
, true, WEB_SOCKET_MESSAGE_TYPE_TEXT
, data
);
113 bool message_was_ok
= false;
114 // Expected to be ignored.
115 EXPECT_TRUE(dispatcher_host_
->OnMessageReceived(message
, &message_was_ok
));
117 EXPECT_EQ(0U, mock_hosts_
.size());
120 TEST_F(WebSocketDispatcherHostTest
, SendFrame
) {
121 int routing_id
= 123;
123 GURL
socket_url("ws://example.com/test");
124 std::vector
<std::string
> requested_protocols
;
125 requested_protocols
.push_back("hello");
126 url::Origin
origin("http://example.com/test");
127 WebSocketHostMsg_AddChannelRequest
add_channel_message(
128 routing_id
, socket_url
, requested_protocols
, origin
);
130 bool message_was_ok
= false;
132 ASSERT_TRUE(dispatcher_host_
->OnMessageReceived(
133 add_channel_message
, &message_was_ok
));
135 std::vector
<char> data
;
136 WebSocketMsg_SendFrame
send_frame_message(
137 routing_id
, true, WEB_SOCKET_MESSAGE_TYPE_TEXT
, data
);
139 EXPECT_TRUE(dispatcher_host_
->OnMessageReceived(
140 send_frame_message
, &message_was_ok
));
142 ASSERT_EQ(1U, mock_hosts_
.size());
143 MockWebSocketHost
* host
= mock_hosts_
[0];
145 ASSERT_EQ(2U, host
->received_messages_
.size());
147 const IPC::Message
& forwarded_message
= host
->received_messages_
[0];
148 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID
, forwarded_message
.type());
149 EXPECT_EQ(routing_id
, forwarded_message
.routing_id());
152 const IPC::Message
& forwarded_message
= host
->received_messages_
[1];
153 EXPECT_EQ(WebSocketMsg_SendFrame::ID
, forwarded_message
.type());
154 EXPECT_EQ(routing_id
, forwarded_message
.routing_id());
159 } // namespace content