| 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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 class MockWebSocketHost : public WebSocketHost { | 24 class MockWebSocketHost : public WebSocketHost { |
| 25 public: | 25 public: |
| 26 MockWebSocketHost(int routing_id, | 26 MockWebSocketHost(int routing_id, |
| 27 WebSocketDispatcherHost* dispatcher, | 27 WebSocketDispatcherHost* dispatcher, |
| 28 net::URLRequestContext* url_request_context) | 28 net::URLRequestContext* url_request_context) |
| 29 : WebSocketHost(routing_id, dispatcher, url_request_context) { | 29 : WebSocketHost(routing_id, dispatcher, url_request_context) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 virtual ~MockWebSocketHost() {} | 32 virtual ~MockWebSocketHost() {} |
| 33 | 33 |
| 34 virtual bool OnMessageReceived(const IPC::Message& message, | 34 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ |
| 35 bool* message_was_ok) OVERRIDE{ | |
| 36 received_messages_.push_back(message); | 35 received_messages_.push_back(message); |
| 37 return true; | 36 return true; |
| 38 } | 37 } |
| 39 | 38 |
| 40 std::vector<IPC::Message> received_messages_; | 39 std::vector<IPC::Message> received_messages_; |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 class WebSocketDispatcherHostTest : public ::testing::Test { | 42 class WebSocketDispatcherHostTest : public ::testing::Test { |
| 44 public: | 43 public: |
| 45 WebSocketDispatcherHostTest() { | 44 WebSocketDispatcherHostTest() { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 71 mock_hosts_.push_back(host); | 70 mock_hosts_.push_back(host); |
| 72 return host; | 71 return host; |
| 73 } | 72 } |
| 74 }; | 73 }; |
| 75 | 74 |
| 76 TEST_F(WebSocketDispatcherHostTest, Construct) { | 75 TEST_F(WebSocketDispatcherHostTest, Construct) { |
| 77 // Do nothing. | 76 // Do nothing. |
| 78 } | 77 } |
| 79 | 78 |
| 80 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 79 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
| 81 bool message_was_ok = false; | |
| 82 IPC::Message message; | 80 IPC::Message message; |
| 83 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message, &message_was_ok)); | 81 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
| 84 } | 82 } |
| 85 | 83 |
| 86 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { | 84 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { |
| 87 int routing_id = 123; | 85 int routing_id = 123; |
| 88 GURL socket_url("ws://example.com/test"); | 86 GURL socket_url("ws://example.com/test"); |
| 89 std::vector<std::string> requested_protocols; | 87 std::vector<std::string> requested_protocols; |
| 90 requested_protocols.push_back("hello"); | 88 requested_protocols.push_back("hello"); |
| 91 url::Origin origin("http://example.com/test"); | 89 url::Origin origin("http://example.com/test"); |
| 92 WebSocketHostMsg_AddChannelRequest message( | 90 WebSocketHostMsg_AddChannelRequest message( |
| 93 routing_id, socket_url, requested_protocols, origin); | 91 routing_id, socket_url, requested_protocols, origin); |
| 94 | 92 |
| 95 bool message_was_ok = false; | 93 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
| 96 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message, &message_was_ok)); | |
| 97 | 94 |
| 98 ASSERT_EQ(1U, mock_hosts_.size()); | 95 ASSERT_EQ(1U, mock_hosts_.size()); |
| 99 MockWebSocketHost* host = mock_hosts_[0]; | 96 MockWebSocketHost* host = mock_hosts_[0]; |
| 100 | 97 |
| 101 ASSERT_EQ(1U, host->received_messages_.size()); | 98 ASSERT_EQ(1U, host->received_messages_.size()); |
| 102 const IPC::Message& forwarded_message = host->received_messages_[0]; | 99 const IPC::Message& forwarded_message = host->received_messages_[0]; |
| 103 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 100 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
| 104 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 101 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 105 } | 102 } |
| 106 | 103 |
| 107 TEST_F(WebSocketDispatcherHostTest, SendFrameButNoHostYet) { | 104 TEST_F(WebSocketDispatcherHostTest, SendFrameButNoHostYet) { |
| 108 int routing_id = 123; | 105 int routing_id = 123; |
| 109 std::vector<char> data; | 106 std::vector<char> data; |
| 110 WebSocketMsg_SendFrame message( | 107 WebSocketMsg_SendFrame message( |
| 111 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | 108 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
| 112 | 109 |
| 113 bool message_was_ok = false; | |
| 114 // Expected to be ignored. | 110 // Expected to be ignored. |
| 115 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(message, &message_was_ok)); | 111 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
| 116 | 112 |
| 117 EXPECT_EQ(0U, mock_hosts_.size()); | 113 EXPECT_EQ(0U, mock_hosts_.size()); |
| 118 } | 114 } |
| 119 | 115 |
| 120 TEST_F(WebSocketDispatcherHostTest, SendFrame) { | 116 TEST_F(WebSocketDispatcherHostTest, SendFrame) { |
| 121 int routing_id = 123; | 117 int routing_id = 123; |
| 122 | 118 |
| 123 GURL socket_url("ws://example.com/test"); | 119 GURL socket_url("ws://example.com/test"); |
| 124 std::vector<std::string> requested_protocols; | 120 std::vector<std::string> requested_protocols; |
| 125 requested_protocols.push_back("hello"); | 121 requested_protocols.push_back("hello"); |
| 126 url::Origin origin("http://example.com/test"); | 122 url::Origin origin("http://example.com/test"); |
| 127 WebSocketHostMsg_AddChannelRequest add_channel_message( | 123 WebSocketHostMsg_AddChannelRequest add_channel_message( |
| 128 routing_id, socket_url, requested_protocols, origin); | 124 routing_id, socket_url, requested_protocols, origin); |
| 129 | 125 |
| 130 bool message_was_ok = false; | 126 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); |
| 131 | |
| 132 ASSERT_TRUE(dispatcher_host_->OnMessageReceived( | |
| 133 add_channel_message, &message_was_ok)); | |
| 134 | 127 |
| 135 std::vector<char> data; | 128 std::vector<char> data; |
| 136 WebSocketMsg_SendFrame send_frame_message( | 129 WebSocketMsg_SendFrame send_frame_message( |
| 137 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | 130 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
| 138 | 131 |
| 139 EXPECT_TRUE(dispatcher_host_->OnMessageReceived( | 132 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(send_frame_message)); |
| 140 send_frame_message, &message_was_ok)); | |
| 141 | 133 |
| 142 ASSERT_EQ(1U, mock_hosts_.size()); | 134 ASSERT_EQ(1U, mock_hosts_.size()); |
| 143 MockWebSocketHost* host = mock_hosts_[0]; | 135 MockWebSocketHost* host = mock_hosts_[0]; |
| 144 | 136 |
| 145 ASSERT_EQ(2U, host->received_messages_.size()); | 137 ASSERT_EQ(2U, host->received_messages_.size()); |
| 146 { | 138 { |
| 147 const IPC::Message& forwarded_message = host->received_messages_[0]; | 139 const IPC::Message& forwarded_message = host->received_messages_[0]; |
| 148 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 140 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
| 149 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 141 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 150 } | 142 } |
| 151 { | 143 { |
| 152 const IPC::Message& forwarded_message = host->received_messages_[1]; | 144 const IPC::Message& forwarded_message = host->received_messages_[1]; |
| 153 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 145 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
| 154 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 146 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 155 } | 147 } |
| 156 } | 148 } |
| 157 | 149 |
| 158 } // namespace | 150 } // namespace |
| 159 } // namespace content | 151 } // namespace content |
| OLD | NEW |