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"
22 // A mock of WebsocketHost which records received messages.
23 class MockWebSocketHost
: public WebSocketHost
{
25 MockWebSocketHost(int routing_id
,
26 WebSocketDispatcherHost
* dispatcher
,
27 net::URLRequestContext
* url_request_context
)
28 : WebSocketHost(routing_id
, dispatcher
, url_request_context
) {
31 virtual ~MockWebSocketHost() {}
33 virtual bool OnMessageReceived(const IPC::Message
& message
,
34 bool* message_was_ok
) OVERRIDE
{
35 received_messages_
.push_back(message
);
39 std::vector
<IPC::Message
> received_messages_
;
42 class WebSocketDispatcherHostTest
: public ::testing::Test
{
44 WebSocketDispatcherHostTest() {
46 new WebSocketDispatcherHost(
47 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext
,
48 base::Unretained(this)),
49 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost
,
50 base::Unretained(this)));
53 virtual ~WebSocketDispatcherHostTest() {}
56 scoped_refptr
<WebSocketDispatcherHost
> dispatcher_host_
;
58 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of
60 std::vector
<MockWebSocketHost
*> mock_hosts_
;
63 net::URLRequestContext
* OnGetRequestContext() {
67 WebSocketHost
* CreateWebSocketHost(int routing_id
) {
68 MockWebSocketHost
* host
=
69 new MockWebSocketHost(routing_id
, dispatcher_host_
.get(), NULL
);
70 mock_hosts_
.push_back(host
);
75 TEST_F(WebSocketDispatcherHostTest
, Construct
) {
79 TEST_F(WebSocketDispatcherHostTest
, UnrelatedMessage
) {
80 bool message_was_ok
= false;
82 EXPECT_FALSE(dispatcher_host_
->OnMessageReceived(message
, &message_was_ok
));
85 TEST_F(WebSocketDispatcherHostTest
, AddChannelRequest
) {
87 GURL
socket_url("ws://example.com/test");
88 std::vector
<std::string
> requested_protocols
;
89 requested_protocols
.push_back("hello");
90 GURL
origin("http://example.com/test");
91 WebSocketHostMsg_AddChannelRequest
message(
92 routing_id
, socket_url
, requested_protocols
, origin
);
94 bool message_was_ok
= false;
95 ASSERT_TRUE(dispatcher_host_
->OnMessageReceived(message
, &message_was_ok
));
97 ASSERT_EQ(1U, mock_hosts_
.size());
98 MockWebSocketHost
* host
= mock_hosts_
[0];
100 ASSERT_EQ(1U, host
->received_messages_
.size());
101 const IPC::Message
& forwarded_message
= host
->received_messages_
[0];
102 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID
, forwarded_message
.type());
103 EXPECT_EQ(routing_id
, forwarded_message
.routing_id());
106 TEST_F(WebSocketDispatcherHostTest
, SendFrameButNoHostYet
) {
107 int routing_id
= 123;
108 std::vector
<char> data
;
109 WebSocketMsg_SendFrame
message(
110 routing_id
, true, WEB_SOCKET_MESSAGE_TYPE_TEXT
, data
);
112 bool message_was_ok
= false;
113 // Expected to be ignored.
114 EXPECT_TRUE(dispatcher_host_
->OnMessageReceived(message
, &message_was_ok
));
116 EXPECT_EQ(0U, mock_hosts_
.size());
119 TEST_F(WebSocketDispatcherHostTest
, SendFrame
) {
120 int routing_id
= 123;
122 GURL
socket_url("ws://example.com/test");
123 std::vector
<std::string
> requested_protocols
;
124 requested_protocols
.push_back("hello");
125 GURL
origin("http://example.com/test");
126 WebSocketHostMsg_AddChannelRequest
add_channel_message(
127 routing_id
, socket_url
, requested_protocols
, origin
);
129 bool message_was_ok
= false;
131 ASSERT_TRUE(dispatcher_host_
->OnMessageReceived(
132 add_channel_message
, &message_was_ok
));
134 std::vector
<char> data
;
135 WebSocketMsg_SendFrame
send_frame_message(
136 routing_id
, true, WEB_SOCKET_MESSAGE_TYPE_TEXT
, data
);
138 EXPECT_TRUE(dispatcher_host_
->OnMessageReceived(
139 send_frame_message
, &message_was_ok
));
141 ASSERT_EQ(1U, mock_hosts_
.size());
142 MockWebSocketHost
* host
= mock_hosts_
[0];
144 ASSERT_EQ(2U, host
->received_messages_
.size());
146 const IPC::Message
& forwarded_message
= host
->received_messages_
[0];
147 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID
, forwarded_message
.type());
148 EXPECT_EQ(routing_id
, forwarded_message
.routing_id());
151 const IPC::Message
& forwarded_message
= host
->received_messages_
[1];
152 EXPECT_EQ(WebSocketMsg_SendFrame::ID
, forwarded_message
.type());
153 EXPECT_EQ(routing_id
, forwarded_message
.routing_id());
158 } // namespace content