OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" | 5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "content/browser/renderer_host/websocket_host.h" | 12 #include "content/browser/renderer_host/websocket_host.h" |
13 #include "content/common/websocket.h" | 13 #include "content/common/websocket.h" |
14 #include "content/common/websocket_messages.h" | 14 #include "content/common/websocket_messages.h" |
15 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 #include "url/origin.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 namespace { | 21 namespace { |
21 | 22 |
22 // A mock of WebsocketHost which records received messages. | 23 // A mock of WebsocketHost which records received messages. |
23 class MockWebSocketHost : public WebSocketHost { | 24 class MockWebSocketHost : public WebSocketHost { |
24 public: | 25 public: |
25 MockWebSocketHost(int routing_id, | 26 MockWebSocketHost(int routing_id, |
26 WebSocketDispatcherHost* dispatcher, | 27 WebSocketDispatcherHost* dispatcher, |
27 net::URLRequestContext* url_request_context) | 28 net::URLRequestContext* url_request_context) |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 bool message_was_ok = false; | 81 bool message_was_ok = false; |
81 IPC::Message message; | 82 IPC::Message message; |
82 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message, &message_was_ok)); | 83 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message, &message_was_ok)); |
83 } | 84 } |
84 | 85 |
85 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { | 86 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { |
86 int routing_id = 123; | 87 int routing_id = 123; |
87 GURL socket_url("ws://example.com/test"); | 88 GURL socket_url("ws://example.com/test"); |
88 std::vector<std::string> requested_protocols; | 89 std::vector<std::string> requested_protocols; |
89 requested_protocols.push_back("hello"); | 90 requested_protocols.push_back("hello"); |
90 GURL origin("http://example.com/test"); | 91 url::Origin origin("http://example.com/test"); |
91 WebSocketHostMsg_AddChannelRequest message( | 92 WebSocketHostMsg_AddChannelRequest message( |
92 routing_id, socket_url, requested_protocols, origin); | 93 routing_id, socket_url, requested_protocols, origin); |
93 | 94 |
94 bool message_was_ok = false; | 95 bool message_was_ok = false; |
95 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message, &message_was_ok)); | 96 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message, &message_was_ok)); |
96 | 97 |
97 ASSERT_EQ(1U, mock_hosts_.size()); | 98 ASSERT_EQ(1U, mock_hosts_.size()); |
98 MockWebSocketHost* host = mock_hosts_[0]; | 99 MockWebSocketHost* host = mock_hosts_[0]; |
99 | 100 |
100 ASSERT_EQ(1U, host->received_messages_.size()); | 101 ASSERT_EQ(1U, host->received_messages_.size()); |
(...skipping 14 matching lines...) Expand all Loading... |
115 | 116 |
116 EXPECT_EQ(0U, mock_hosts_.size()); | 117 EXPECT_EQ(0U, mock_hosts_.size()); |
117 } | 118 } |
118 | 119 |
119 TEST_F(WebSocketDispatcherHostTest, SendFrame) { | 120 TEST_F(WebSocketDispatcherHostTest, SendFrame) { |
120 int routing_id = 123; | 121 int routing_id = 123; |
121 | 122 |
122 GURL socket_url("ws://example.com/test"); | 123 GURL socket_url("ws://example.com/test"); |
123 std::vector<std::string> requested_protocols; | 124 std::vector<std::string> requested_protocols; |
124 requested_protocols.push_back("hello"); | 125 requested_protocols.push_back("hello"); |
125 GURL origin("http://example.com/test"); | 126 url::Origin origin("http://example.com/test"); |
126 WebSocketHostMsg_AddChannelRequest add_channel_message( | 127 WebSocketHostMsg_AddChannelRequest add_channel_message( |
127 routing_id, socket_url, requested_protocols, origin); | 128 routing_id, socket_url, requested_protocols, origin); |
128 | 129 |
129 bool message_was_ok = false; | 130 bool message_was_ok = false; |
130 | 131 |
131 ASSERT_TRUE(dispatcher_host_->OnMessageReceived( | 132 ASSERT_TRUE(dispatcher_host_->OnMessageReceived( |
132 add_channel_message, &message_was_ok)); | 133 add_channel_message, &message_was_ok)); |
133 | 134 |
134 std::vector<char> data; | 135 std::vector<char> data; |
135 WebSocketMsg_SendFrame send_frame_message( | 136 WebSocketMsg_SendFrame send_frame_message( |
(...skipping 13 matching lines...) Expand all Loading... |
149 } | 150 } |
150 { | 151 { |
151 const IPC::Message& forwarded_message = host->received_messages_[1]; | 152 const IPC::Message& forwarded_message = host->received_messages_[1]; |
152 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 153 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
153 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 154 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
154 } | 155 } |
155 } | 156 } |
156 | 157 |
157 } // namespace | 158 } // namespace |
158 } // namespace content | 159 } // namespace content |
OLD | NEW |