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_stream.h" | 5 #include "net/websockets/websocket_stream.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/metrics/histogram_samples.h" | 16 #include "base/metrics/histogram_samples.h" |
17 #include "base/metrics/statistics_recorder.h" | 17 #include "base/metrics/statistics_recorder.h" |
18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
20 #include "base/timer/mock_timer.h" | 20 #include "base/timer/mock_timer.h" |
21 #include "base/timer/timer.h" | 21 #include "base/timer/timer.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/http/http_request_headers.h" | 23 #include "net/http/http_request_headers.h" |
24 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
25 #include "net/proxy/proxy_service.h" | 25 #include "net/proxy/proxy_service.h" |
26 #include "net/socket/client_socket_handle.h" | 26 #include "net/socket/client_socket_handle.h" |
27 #include "net/socket/socket_test_util.h" | 27 #include "net/socket/socket_test_util.h" |
28 #include "net/test/cert_test_util.h" | 28 #include "net/test/cert_test_util.h" |
| 29 #include "net/test/gtest_util.h" |
29 #include "net/test/test_data_directory.h" | 30 #include "net/test/test_data_directory.h" |
30 #include "net/url_request/url_request_test_util.h" | 31 #include "net/url_request/url_request_test_util.h" |
31 #include "net/websockets/websocket_basic_handshake_stream.h" | 32 #include "net/websockets/websocket_basic_handshake_stream.h" |
32 #include "net/websockets/websocket_frame.h" | 33 #include "net/websockets/websocket_frame.h" |
33 #include "net/websockets/websocket_stream_create_test_base.h" | 34 #include "net/websockets/websocket_stream_create_test_base.h" |
34 #include "net/websockets/websocket_test_util.h" | 35 #include "net/websockets/websocket_test_util.h" |
| 36 #include "testing/gmock/include/gmock/gmock.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
36 #include "url/gurl.h" | 38 #include "url/gurl.h" |
37 #include "url/origin.h" | 39 #include "url/origin.h" |
38 | 40 |
| 41 using net::test::IsOk; |
| 42 |
39 namespace net { | 43 namespace net { |
40 namespace { | 44 namespace { |
41 | 45 |
42 // Simple builder for a SequencedSocketData object to save repetitive code. | 46 // Simple builder for a SequencedSocketData object to save repetitive code. |
43 // It always sets the connect data to MockConnect(SYNCHRONOUS, OK), so it cannot | 47 // It always sets the connect data to MockConnect(SYNCHRONOUS, OK), so it cannot |
44 // be used in tests where the connect fails. In practice, those tests never have | 48 // be used in tests where the connect fails. In practice, those tests never have |
45 // any read/write data and so can't benefit from it anyway. The arrays are not | 49 // any read/write data and so can't benefit from it anyway. The arrays are not |
46 // copied. It is up to the caller to ensure they stay in scope until the test | 50 // copied. It is up to the caller to ensure they stay in scope until the test |
47 // ends. | 51 // ends. |
48 template <size_t reads_count, size_t writes_count> | 52 template <size_t reads_count, size_t writes_count> |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 "Sec-WebSocket-Extensions: permessage-deflate\r\n") + | 549 "Sec-WebSocket-Extensions: permessage-deflate\r\n") + |
546 std::string( | 550 std::string( |
547 "\xc1\x07" // WebSocket header (FIN + RSV1, Text payload 7 bytes) | 551 "\xc1\x07" // WebSocket header (FIN + RSV1, Text payload 7 bytes) |
548 "\xf2\x48\xcd\xc9\xc9\x07\x00", // "Hello" DEFLATE compressed | 552 "\xf2\x48\xcd\xc9\xc9\x07\x00", // "Hello" DEFLATE compressed |
549 9)); | 553 9)); |
550 WaitUntilConnectDone(); | 554 WaitUntilConnectDone(); |
551 | 555 |
552 ASSERT_TRUE(stream_); | 556 ASSERT_TRUE(stream_); |
553 std::vector<std::unique_ptr<WebSocketFrame>> frames; | 557 std::vector<std::unique_ptr<WebSocketFrame>> frames; |
554 CompletionCallback callback; | 558 CompletionCallback callback; |
555 ASSERT_EQ(OK, stream_->ReadFrames(&frames, callback)); | 559 ASSERT_THAT(stream_->ReadFrames(&frames, callback), IsOk()); |
556 ASSERT_EQ(1U, frames.size()); | 560 ASSERT_EQ(1U, frames.size()); |
557 ASSERT_EQ(5U, frames[0]->header.payload_length); | 561 ASSERT_EQ(5U, frames[0]->header.payload_length); |
558 EXPECT_EQ("Hello", std::string(frames[0]->data->data(), 5)); | 562 EXPECT_EQ("Hello", std::string(frames[0]->data->data(), 5)); |
559 } | 563 } |
560 | 564 |
561 // Unknown extension in the response is rejected | 565 // Unknown extension in the response is rejected |
562 TEST_F(WebSocketStreamCreateExtensionTest, UnknownExtension) { | 566 TEST_F(WebSocketStreamCreateExtensionTest, UnknownExtension) { |
563 CreateAndConnectWithExtensions("x-unknown-extension"); | 567 CreateAndConnectWithExtensions("x-unknown-extension"); |
564 EXPECT_FALSE(stream_); | 568 EXPECT_FALSE(stream_); |
565 EXPECT_TRUE(has_failed()); | 569 EXPECT_TRUE(has_failed()); |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 LocalhostOrigin(), "", | 1234 LocalhostOrigin(), "", |
1231 std::move(socket_data)); | 1235 std::move(socket_data)); |
1232 WaitUntilConnectDone(); | 1236 WaitUntilConnectDone(); |
1233 EXPECT_TRUE(has_failed()); | 1237 EXPECT_TRUE(has_failed()); |
1234 EXPECT_EQ("Establishing a tunnel via proxy server failed.", | 1238 EXPECT_EQ("Establishing a tunnel via proxy server failed.", |
1235 failure_message()); | 1239 failure_message()); |
1236 } | 1240 } |
1237 | 1241 |
1238 } // namespace | 1242 } // namespace |
1239 } // namespace net | 1243 } // namespace net |
OLD | NEW |