Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: net/websockets/websocket_stream_test.cc

Issue 152483003: [WebSocket] Add Cache-Control to the request header. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/websockets/websocket_stream.cc ('k') | net/websockets/websocket_test_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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("Error during WebSocket handshake: Unexpected response code: 200",
658 failure_message());
655 } 659 }
656 660
657 // Redirects are not followed (according to the WHATWG WebSocket API, which 661 // Redirects are not followed (according to the WHATWG WebSocket API, which
658 // overrides RFC6455 for browser applications). 662 // overrides RFC6455 for browser applications).
659 TEST_F(WebSocketStreamCreateTest, RedirectsRejected) { 663 TEST_F(WebSocketStreamCreateTest, RedirectsRejected) {
660 static const char kRedirectResponse[] = 664 static const char kRedirectResponse[] =
661 "HTTP/1.1 302 Moved Temporarily\r\n" 665 "HTTP/1.1 302 Moved Temporarily\r\n"
662 "Content-Type: text/html\r\n" 666 "Content-Type: text/html\r\n"
663 "Content-Length: 34\r\n" 667 "Content-Length: 34\r\n"
664 "Connection: keep-alive\r\n" 668 "Connection: keep-alive\r\n"
665 "Location: ws://localhost/other\r\n" 669 "Location: ws://localhost/other\r\n"
666 "\r\n" 670 "\r\n"
667 "<title>Moved</title><h1>Moved</h1>"; 671 "<title>Moved</title><h1>Moved</h1>";
668 CreateAndConnectCustomResponse("ws://localhost/", 672 CreateAndConnectCustomResponse("ws://localhost/",
669 "/", 673 "/",
670 NoSubProtocols(), 674 NoSubProtocols(),
671 "http://localhost/", 675 "http://localhost/",
672 "", 676 "",
673 kRedirectResponse); 677 kRedirectResponse);
674 RunUntilIdle(); 678 RunUntilIdle();
675 EXPECT_TRUE(has_failed()); 679 EXPECT_TRUE(has_failed());
676 EXPECT_EQ("Unexpected status code: 302", failure_message()); 680 EXPECT_EQ("Error during WebSocket handshake: Unexpected response code: 302",
681 failure_message());
677 } 682 }
678 683
679 // Malformed responses should be rejected. HttpStreamParser will accept just 684 // 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 685 // 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 686 // has to be at the start of the response. Even then, it just gets treated as an
682 // HTTP/0.9 response. 687 // HTTP/0.9 response.
683 TEST_F(WebSocketStreamCreateTest, MalformedResponse) { 688 TEST_F(WebSocketStreamCreateTest, MalformedResponse) {
684 static const char kMalformedResponse[] = 689 static const char kMalformedResponse[] =
685 "220 mx.google.com ESMTP\r\n" 690 "220 mx.google.com ESMTP\r\n"
686 "HTTP/1.1 101 OK\r\n" 691 "HTTP/1.1 101 OK\r\n"
687 "Upgrade: websocket\r\n" 692 "Upgrade: websocket\r\n"
688 "Connection: Upgrade\r\n" 693 "Connection: Upgrade\r\n"
689 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" 694 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
690 "\r\n"; 695 "\r\n";
691 CreateAndConnectCustomResponse("ws://localhost/", 696 CreateAndConnectCustomResponse("ws://localhost/",
692 "/", 697 "/",
693 NoSubProtocols(), 698 NoSubProtocols(),
694 "http://localhost/", 699 "http://localhost/",
695 "", 700 "",
696 kMalformedResponse); 701 kMalformedResponse);
697 RunUntilIdle(); 702 RunUntilIdle();
698 EXPECT_TRUE(has_failed()); 703 EXPECT_TRUE(has_failed());
699 EXPECT_EQ("Unexpected status code: 200", failure_message()); 704 EXPECT_EQ("Error during WebSocket handshake: Unexpected response code: 200",
705 failure_message());
700 } 706 }
701 707
702 // Upgrade header must be present. 708 // Upgrade header must be present.
703 TEST_F(WebSocketStreamCreateTest, MissingUpgradeHeader) { 709 TEST_F(WebSocketStreamCreateTest, MissingUpgradeHeader) {
704 static const char kMissingUpgradeResponse[] = 710 static const char kMissingUpgradeResponse[] =
705 "HTTP/1.1 101 Switching Protocols\r\n" 711 "HTTP/1.1 101 Switching Protocols\r\n"
706 "Connection: Upgrade\r\n" 712 "Connection: Upgrade\r\n"
707 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" 713 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
708 "\r\n"; 714 "\r\n";
709 CreateAndConnectCustomResponse("ws://localhost/", 715 CreateAndConnectCustomResponse("ws://localhost/",
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 stream_request_.reset(); 960 stream_request_.reset();
955 RunUntilIdle(); 961 RunUntilIdle();
956 EXPECT_FALSE(has_failed()); 962 EXPECT_FALSE(has_failed());
957 EXPECT_FALSE(stream_); 963 EXPECT_FALSE(stream_);
958 EXPECT_TRUE(request_info_); 964 EXPECT_TRUE(request_info_);
959 EXPECT_FALSE(response_info_); 965 EXPECT_FALSE(response_info_);
960 } 966 }
961 967
962 } // namespace 968 } // namespace
963 } // namespace net 969 } // namespace net
OLDNEW
« no previous file with comments | « net/websockets/websocket_stream.cc ('k') | net/websockets/websocket_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698