| 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> |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 ASSERT_TRUE(request_info_); | 244 ASSERT_TRUE(request_info_); |
| 245 ASSERT_TRUE(response_info_); | 245 ASSERT_TRUE(response_info_); |
| 246 std::vector<HeaderKeyValuePair> request_headers = | 246 std::vector<HeaderKeyValuePair> request_headers = |
| 247 ToVector(request_info_->headers); | 247 ToVector(request_info_->headers); |
| 248 // We examine the contents of request_info_ and response_info_ | 248 // We examine the contents of request_info_ and response_info_ |
| 249 // mainly only in this test case. | 249 // mainly only in this test case. |
| 250 EXPECT_EQ(GURL("ws://localhost/"), request_info_->url); | 250 EXPECT_EQ(GURL("ws://localhost/"), request_info_->url); |
| 251 EXPECT_EQ(GURL("ws://localhost/"), response_info_->url); | 251 EXPECT_EQ(GURL("ws://localhost/"), response_info_->url); |
| 252 EXPECT_EQ(101, response_info_->status_code); | 252 EXPECT_EQ(101, response_info_->status_code); |
| 253 EXPECT_EQ("Switching Protocols", response_info_->status_text); | 253 EXPECT_EQ("Switching Protocols", response_info_->status_text); |
| 254 EXPECT_EQ(10u, request_headers.size()); | 254 ASSERT_EQ(12u, request_headers.size()); |
| 255 EXPECT_EQ(HeaderKeyValuePair("Host", "localhost"), request_headers[0]); | 255 EXPECT_EQ(HeaderKeyValuePair("Host", "localhost"), request_headers[0]); |
| 256 EXPECT_EQ(HeaderKeyValuePair("Connection", "Upgrade"), request_headers[1]); | 256 EXPECT_EQ(HeaderKeyValuePair("Connection", "Upgrade"), request_headers[1]); |
| 257 EXPECT_EQ(HeaderKeyValuePair("Upgrade", "websocket"), request_headers[2]); | 257 EXPECT_EQ(HeaderKeyValuePair("Pragma", "no-cache"), request_headers[2]); |
| 258 EXPECT_EQ(HeaderKeyValuePair("Cache-Control", "no-cache"), |
| 259 request_headers[3]); |
| 260 EXPECT_EQ(HeaderKeyValuePair("Upgrade", "websocket"), request_headers[4]); |
| 258 EXPECT_EQ(HeaderKeyValuePair("Origin", "http://localhost/"), | 261 EXPECT_EQ(HeaderKeyValuePair("Origin", "http://localhost/"), |
| 259 request_headers[3]); | 262 request_headers[5]); |
| 260 EXPECT_EQ(HeaderKeyValuePair("Sec-WebSocket-Version", "13"), | 263 EXPECT_EQ(HeaderKeyValuePair("Sec-WebSocket-Version", "13"), |
| 261 request_headers[4]); | 264 request_headers[6]); |
| 262 EXPECT_EQ(HeaderKeyValuePair("User-Agent", ""), request_headers[5]); | 265 EXPECT_EQ(HeaderKeyValuePair("User-Agent", ""), request_headers[7]); |
| 263 EXPECT_EQ(HeaderKeyValuePair("Accept-Encoding", "gzip,deflate"), | 266 EXPECT_EQ(HeaderKeyValuePair("Accept-Encoding", "gzip,deflate"), |
| 264 request_headers[6]); | 267 request_headers[8]); |
| 265 EXPECT_EQ(HeaderKeyValuePair("Accept-Language", "en-us,fr"), | 268 EXPECT_EQ(HeaderKeyValuePair("Accept-Language", "en-us,fr"), |
| 266 request_headers[7]); | 269 request_headers[9]); |
| 267 EXPECT_EQ("Sec-WebSocket-Key", request_headers[8].first); | 270 EXPECT_EQ("Sec-WebSocket-Key", request_headers[10].first); |
| 268 EXPECT_EQ(HeaderKeyValuePair("Sec-WebSocket-Extensions", | 271 EXPECT_EQ(HeaderKeyValuePair("Sec-WebSocket-Extensions", |
| 269 "permessage-deflate; client_max_window_bits"), | 272 "permessage-deflate; client_max_window_bits"), |
| 270 request_headers[9]); | 273 request_headers[11]); |
| 271 | 274 |
| 272 std::vector<HeaderKeyValuePair> response_headers = | 275 std::vector<HeaderKeyValuePair> response_headers = |
| 273 ToVector(*response_info_->headers); | 276 ToVector(*response_info_->headers); |
| 274 ASSERT_EQ(6u, response_headers.size()); | 277 ASSERT_EQ(6u, response_headers.size()); |
| 275 // Sort the headers for ease of verification. | 278 // Sort the headers for ease of verification. |
| 276 std::sort(response_headers.begin(), response_headers.end()); | 279 std::sort(response_headers.begin(), response_headers.end()); |
| 277 | 280 |
| 278 EXPECT_EQ(HeaderKeyValuePair("Connection", "Upgrade"), response_headers[0]); | 281 EXPECT_EQ(HeaderKeyValuePair("Connection", "Upgrade"), response_headers[0]); |
| 279 EXPECT_EQ("Sec-WebSocket-Accept", response_headers[1].first); | 282 EXPECT_EQ("Sec-WebSocket-Accept", response_headers[1].first); |
| 280 EXPECT_EQ(HeaderKeyValuePair("Upgrade", "websocket"), response_headers[2]); | 283 EXPECT_EQ(HeaderKeyValuePair("Upgrade", "websocket"), response_headers[2]); |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" | 647 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| 645 "\r\n"; | 648 "\r\n"; |
| 646 CreateAndConnectCustomResponse("ws://localhost/", | 649 CreateAndConnectCustomResponse("ws://localhost/", |
| 647 "/", | 650 "/", |
| 648 NoSubProtocols(), | 651 NoSubProtocols(), |
| 649 "http://localhost/", | 652 "http://localhost/", |
| 650 "", | 653 "", |
| 651 kInvalidStatusCodeResponse); | 654 kInvalidStatusCodeResponse); |
| 652 RunUntilIdle(); | 655 RunUntilIdle(); |
| 653 EXPECT_TRUE(has_failed()); | 656 EXPECT_TRUE(has_failed()); |
| 654 EXPECT_EQ("Unexpected status code: 200", failure_message()); | 657 EXPECT_EQ("Unexpected response code: 200", failure_message()); |
| 655 } | 658 } |
| 656 | 659 |
| 657 // Redirects are not followed (according to the WHATWG WebSocket API, which | 660 // Redirects are not followed (according to the WHATWG WebSocket API, which |
| 658 // overrides RFC6455 for browser applications). | 661 // overrides RFC6455 for browser applications). |
| 659 TEST_F(WebSocketStreamCreateTest, RedirectsRejected) { | 662 TEST_F(WebSocketStreamCreateTest, RedirectsRejected) { |
| 660 static const char kRedirectResponse[] = | 663 static const char kRedirectResponse[] = |
| 661 "HTTP/1.1 302 Moved Temporarily\r\n" | 664 "HTTP/1.1 302 Moved Temporarily\r\n" |
| 662 "Content-Type: text/html\r\n" | 665 "Content-Type: text/html\r\n" |
| 663 "Content-Length: 34\r\n" | 666 "Content-Length: 34\r\n" |
| 664 "Connection: keep-alive\r\n" | 667 "Connection: keep-alive\r\n" |
| 665 "Location: ws://localhost/other\r\n" | 668 "Location: ws://localhost/other\r\n" |
| 666 "\r\n" | 669 "\r\n" |
| 667 "<title>Moved</title><h1>Moved</h1>"; | 670 "<title>Moved</title><h1>Moved</h1>"; |
| 668 CreateAndConnectCustomResponse("ws://localhost/", | 671 CreateAndConnectCustomResponse("ws://localhost/", |
| 669 "/", | 672 "/", |
| 670 NoSubProtocols(), | 673 NoSubProtocols(), |
| 671 "http://localhost/", | 674 "http://localhost/", |
| 672 "", | 675 "", |
| 673 kRedirectResponse); | 676 kRedirectResponse); |
| 674 RunUntilIdle(); | 677 RunUntilIdle(); |
| 675 EXPECT_TRUE(has_failed()); | 678 EXPECT_TRUE(has_failed()); |
| 676 EXPECT_EQ("Unexpected status code: 302", failure_message()); | 679 EXPECT_EQ("Unexpected response code: 302", failure_message()); |
| 677 } | 680 } |
| 678 | 681 |
| 679 // Malformed responses should be rejected. HttpStreamParser will accept just | 682 // Malformed responses should be rejected. HttpStreamParser will accept just |
| 680 // about any garbage in the middle of the headers. To make it give up, the junk | 683 // about any garbage in the middle of the headers. To make it give up, the junk |
| 681 // has to be at the start of the response. Even then, it just gets treated as an | 684 // has to be at the start of the response. Even then, it just gets treated as an |
| 682 // HTTP/0.9 response. | 685 // HTTP/0.9 response. |
| 683 TEST_F(WebSocketStreamCreateTest, MalformedResponse) { | 686 TEST_F(WebSocketStreamCreateTest, MalformedResponse) { |
| 684 static const char kMalformedResponse[] = | 687 static const char kMalformedResponse[] = |
| 685 "220 mx.google.com ESMTP\r\n" | 688 "220 mx.google.com ESMTP\r\n" |
| 686 "HTTP/1.1 101 OK\r\n" | 689 "HTTP/1.1 101 OK\r\n" |
| 687 "Upgrade: websocket\r\n" | 690 "Upgrade: websocket\r\n" |
| 688 "Connection: Upgrade\r\n" | 691 "Connection: Upgrade\r\n" |
| 689 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" | 692 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| 690 "\r\n"; | 693 "\r\n"; |
| 691 CreateAndConnectCustomResponse("ws://localhost/", | 694 CreateAndConnectCustomResponse("ws://localhost/", |
| 692 "/", | 695 "/", |
| 693 NoSubProtocols(), | 696 NoSubProtocols(), |
| 694 "http://localhost/", | 697 "http://localhost/", |
| 695 "", | 698 "", |
| 696 kMalformedResponse); | 699 kMalformedResponse); |
| 697 RunUntilIdle(); | 700 RunUntilIdle(); |
| 698 EXPECT_TRUE(has_failed()); | 701 EXPECT_TRUE(has_failed()); |
| 699 EXPECT_EQ("Unexpected status code: 200", failure_message()); | 702 EXPECT_EQ("Unexpected response code: 200", failure_message()); |
| 700 } | 703 } |
| 701 | 704 |
| 702 // Upgrade header must be present. | 705 // Upgrade header must be present. |
| 703 TEST_F(WebSocketStreamCreateTest, MissingUpgradeHeader) { | 706 TEST_F(WebSocketStreamCreateTest, MissingUpgradeHeader) { |
| 704 static const char kMissingUpgradeResponse[] = | 707 static const char kMissingUpgradeResponse[] = |
| 705 "HTTP/1.1 101 Switching Protocols\r\n" | 708 "HTTP/1.1 101 Switching Protocols\r\n" |
| 706 "Connection: Upgrade\r\n" | 709 "Connection: Upgrade\r\n" |
| 707 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" | 710 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| 708 "\r\n"; | 711 "\r\n"; |
| 709 CreateAndConnectCustomResponse("ws://localhost/", | 712 CreateAndConnectCustomResponse("ws://localhost/", |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 954 stream_request_.reset(); | 957 stream_request_.reset(); |
| 955 RunUntilIdle(); | 958 RunUntilIdle(); |
| 956 EXPECT_FALSE(has_failed()); | 959 EXPECT_FALSE(has_failed()); |
| 957 EXPECT_FALSE(stream_); | 960 EXPECT_FALSE(stream_); |
| 958 EXPECT_TRUE(request_info_); | 961 EXPECT_TRUE(request_info_); |
| 959 EXPECT_FALSE(response_info_); | 962 EXPECT_FALSE(response_info_); |
| 960 } | 963 } |
| 961 | 964 |
| 962 } // namespace | 965 } // namespace |
| 963 } // namespace net | 966 } // namespace net |
| OLD | NEW |