| 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 "net/websockets/websocket_channel.h" | 5 #include "net/websockets/websocket_channel.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <iostream> | 9 #include <iostream> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
| 25 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 26 #include "net/websockets/websocket_errors.h" | 26 #include "net/websockets/websocket_errors.h" |
| 27 #include "net/websockets/websocket_event_interface.h" | 27 #include "net/websockets/websocket_event_interface.h" |
| 28 #include "net/websockets/websocket_handshake_request_info.h" | 28 #include "net/websockets/websocket_handshake_request_info.h" |
| 29 #include "net/websockets/websocket_handshake_response_info.h" | 29 #include "net/websockets/websocket_handshake_response_info.h" |
| 30 #include "net/websockets/websocket_mux.h" | 30 #include "net/websockets/websocket_mux.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 34 #include "url/origin.h" |
| 34 | 35 |
| 35 // Hacky macros to construct the body of a Close message from a code and a | 36 // Hacky macros to construct the body of a Close message from a code and a |
| 36 // string, while ensuring the result is a compile-time constant string. | 37 // string, while ensuring the result is a compile-time constant string. |
| 37 // Use like CLOSE_DATA(NORMAL_CLOSURE, "Explanation String") | 38 // Use like CLOSE_DATA(NORMAL_CLOSURE, "Explanation String") |
| 38 #define CLOSE_DATA(code, string) WEBSOCKET_CLOSE_CODE_AS_STRING_##code string | 39 #define CLOSE_DATA(code, string) WEBSOCKET_CLOSE_CODE_AS_STRING_##code string |
| 39 #define WEBSOCKET_CLOSE_CODE_AS_STRING_NORMAL_CLOSURE "\x03\xe8" | 40 #define WEBSOCKET_CLOSE_CODE_AS_STRING_NORMAL_CLOSURE "\x03\xe8" |
| 40 #define WEBSOCKET_CLOSE_CODE_AS_STRING_GOING_AWAY "\x03\xe9" | 41 #define WEBSOCKET_CLOSE_CODE_AS_STRING_GOING_AWAY "\x03\xe9" |
| 41 #define WEBSOCKET_CLOSE_CODE_AS_STRING_PROTOCOL_ERROR "\x03\xea" | 42 #define WEBSOCKET_CLOSE_CODE_AS_STRING_PROTOCOL_ERROR "\x03\xea" |
| 42 #define WEBSOCKET_CLOSE_CODE_AS_STRING_ABNORMAL_CLOSURE "\x03\xee" | 43 #define WEBSOCKET_CLOSE_CODE_AS_STRING_ABNORMAL_CLOSURE "\x03\xee" |
| 43 #define WEBSOCKET_CLOSE_CODE_AS_STRING_SERVER_ERROR "\x03\xf3" | 44 #define WEBSOCKET_CLOSE_CODE_AS_STRING_SERVER_ERROR "\x03\xf3" |
| (...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 MOCK_METHOD0(Close, void()); | 674 MOCK_METHOD0(Close, void()); |
| 674 MOCK_CONST_METHOD0(GetSubProtocol, std::string()); | 675 MOCK_CONST_METHOD0(GetSubProtocol, std::string()); |
| 675 MOCK_CONST_METHOD0(GetExtensions, std::string()); | 676 MOCK_CONST_METHOD0(GetExtensions, std::string()); |
| 676 MOCK_METHOD0(AsWebSocketStream, WebSocketStream*()); | 677 MOCK_METHOD0(AsWebSocketStream, WebSocketStream*()); |
| 677 }; | 678 }; |
| 678 | 679 |
| 679 struct ArgumentCopyingWebSocketStreamCreator { | 680 struct ArgumentCopyingWebSocketStreamCreator { |
| 680 scoped_ptr<WebSocketStreamRequest> Create( | 681 scoped_ptr<WebSocketStreamRequest> Create( |
| 681 const GURL& socket_url, | 682 const GURL& socket_url, |
| 682 const std::vector<std::string>& requested_subprotocols, | 683 const std::vector<std::string>& requested_subprotocols, |
| 683 const GURL& origin, | 684 const url::Origin& origin, |
| 684 URLRequestContext* url_request_context, | 685 URLRequestContext* url_request_context, |
| 685 const BoundNetLog& net_log, | 686 const BoundNetLog& net_log, |
| 686 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { | 687 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { |
| 687 this->socket_url = socket_url; | 688 this->socket_url = socket_url; |
| 688 this->requested_subprotocols = requested_subprotocols; | 689 this->requested_subprotocols = requested_subprotocols; |
| 689 this->origin = origin; | 690 this->origin = origin; |
| 690 this->url_request_context = url_request_context; | 691 this->url_request_context = url_request_context; |
| 691 this->net_log = net_log; | 692 this->net_log = net_log; |
| 692 this->connect_delegate = connect_delegate.Pass(); | 693 this->connect_delegate = connect_delegate.Pass(); |
| 693 return make_scoped_ptr(new WebSocketStreamRequest); | 694 return make_scoped_ptr(new WebSocketStreamRequest); |
| 694 } | 695 } |
| 695 | 696 |
| 696 GURL socket_url; | 697 GURL socket_url; |
| 697 GURL origin; | 698 url::Origin origin; |
| 698 std::vector<std::string> requested_subprotocols; | 699 std::vector<std::string> requested_subprotocols; |
| 699 URLRequestContext* url_request_context; | 700 URLRequestContext* url_request_context; |
| 700 BoundNetLog net_log; | 701 BoundNetLog net_log; |
| 701 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate; | 702 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate; |
| 702 }; | 703 }; |
| 703 | 704 |
| 704 // Converts a std::string to a std::vector<char>. For test purposes, it is | 705 // Converts a std::string to a std::vector<char>. For test purposes, it is |
| 705 // convenient to be able to specify data as a string, but the | 706 // convenient to be able to specify data as a string, but the |
| 706 // WebSocketEventInterface requires the vector<char> type. | 707 // WebSocketEventInterface requires the vector<char> type. |
| 707 std::vector<char> AsVector(const std::string& s) { | 708 std::vector<char> AsVector(const std::string& s) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 void set_stream(scoped_ptr<T> stream) { | 748 void set_stream(scoped_ptr<T> stream) { |
| 748 // Since the definition of "PassAs" depends on the type T, the C++ standard | 749 // Since the definition of "PassAs" depends on the type T, the C++ standard |
| 749 // requires the "template" keyword to indicate that "PassAs" should be | 750 // requires the "template" keyword to indicate that "PassAs" should be |
| 750 // parsed as a template method. | 751 // parsed as a template method. |
| 751 stream_ = stream.template PassAs<WebSocketStream>(); | 752 stream_ = stream.template PassAs<WebSocketStream>(); |
| 752 } | 753 } |
| 753 | 754 |
| 754 // A struct containing the data that will be used to connect the channel. | 755 // A struct containing the data that will be used to connect the channel. |
| 755 // Grouped for readability. | 756 // Grouped for readability. |
| 756 struct ConnectData { | 757 struct ConnectData { |
| 757 ConnectData() : socket_url("ws://ws/"), origin("http://ws/") {} | 758 ConnectData() : socket_url("ws://ws/"), origin("http://ws") {} |
| 758 | 759 |
| 759 // URLRequestContext object. | 760 // URLRequestContext object. |
| 760 URLRequestContext url_request_context; | 761 URLRequestContext url_request_context; |
| 761 | 762 |
| 762 // URL to (pretend to) connect to. | 763 // URL to (pretend to) connect to. |
| 763 GURL socket_url; | 764 GURL socket_url; |
| 764 // Requested protocols for the request. | 765 // Requested protocols for the request. |
| 765 std::vector<std::string> requested_subprotocols; | 766 std::vector<std::string> requested_subprotocols; |
| 766 // Origin of the request | 767 // Origin of the request |
| 767 GURL origin; | 768 url::Origin origin; |
| 768 | 769 |
| 769 // A fake WebSocketStreamCreator that just records its arguments. | 770 // A fake WebSocketStreamCreator that just records its arguments. |
| 770 ArgumentCopyingWebSocketStreamCreator creator; | 771 ArgumentCopyingWebSocketStreamCreator creator; |
| 771 }; | 772 }; |
| 772 ConnectData connect_data_; | 773 ConnectData connect_data_; |
| 773 | 774 |
| 774 // The channel we are testing. Not initialised until SetChannel() is called. | 775 // The channel we are testing. Not initialised until SetChannel() is called. |
| 775 scoped_ptr<WebSocketChannel> channel_; | 776 scoped_ptr<WebSocketChannel> channel_; |
| 776 | 777 |
| 777 // A mock or fake stream for tests that need one. | 778 // A mock or fake stream for tests that need one. |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 // whether these methods are called or not. | 953 // whether these methods are called or not. |
| 953 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); | 954 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 954 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); | 955 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 955 } | 956 } |
| 956 }; | 957 }; |
| 957 | 958 |
| 958 // Simple test that everything that should be passed to the creator function is | 959 // Simple test that everything that should be passed to the creator function is |
| 959 // passed to the creator function. | 960 // passed to the creator function. |
| 960 TEST_F(WebSocketChannelTest, EverythingIsPassedToTheCreatorFunction) { | 961 TEST_F(WebSocketChannelTest, EverythingIsPassedToTheCreatorFunction) { |
| 961 connect_data_.socket_url = GURL("ws://example.com/test"); | 962 connect_data_.socket_url = GURL("ws://example.com/test"); |
| 962 connect_data_.origin = GURL("http://example.com/test"); | 963 connect_data_.origin = url::Origin("http://example.com"); |
| 963 connect_data_.requested_subprotocols.push_back("Sinbad"); | 964 connect_data_.requested_subprotocols.push_back("Sinbad"); |
| 964 | 965 |
| 965 CreateChannelAndConnect(); | 966 CreateChannelAndConnect(); |
| 966 | 967 |
| 967 const ArgumentCopyingWebSocketStreamCreator& actual = connect_data_.creator; | 968 const ArgumentCopyingWebSocketStreamCreator& actual = connect_data_.creator; |
| 968 | 969 |
| 969 EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context); | 970 EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context); |
| 970 | 971 |
| 971 EXPECT_EQ(connect_data_.socket_url, actual.socket_url); | 972 EXPECT_EQ(connect_data_.socket_url, actual.socket_url); |
| 972 EXPECT_EQ(connect_data_.requested_subprotocols, | 973 EXPECT_EQ(connect_data_.requested_subprotocols, |
| (...skipping 2130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3103 channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK"); | 3104 channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK"); |
| 3104 ASSERT_TRUE(read_frames); | 3105 ASSERT_TRUE(read_frames); |
| 3105 // Provide the "Close" message from the server. | 3106 // Provide the "Close" message from the server. |
| 3106 *read_frames = CreateFrameVector(frames); | 3107 *read_frames = CreateFrameVector(frames); |
| 3107 read_callback.Run(OK); | 3108 read_callback.Run(OK); |
| 3108 completion.WaitForResult(); | 3109 completion.WaitForResult(); |
| 3109 } | 3110 } |
| 3110 | 3111 |
| 3111 } // namespace | 3112 } // namespace |
| 3112 } // namespace net | 3113 } // namespace net |
| OLD | NEW |