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

Side by Side Diff: net/http/http_network_transaction_unittest.cc

Issue 23856018: Changes to HttpNetworkTransaction for WebSocket Handshake (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add tests by yhirano Created 7 years, 1 month 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
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/http/http_network_transaction.h" 5 #include "net/http/http_network_transaction.h"
6 6
7 #include <math.h> // ceil 7 #include <math.h> // ceil
8 #include <stdarg.h> 8 #include <stdarg.h>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 #include "net/socket/ssl_client_socket.h" 57 #include "net/socket/ssl_client_socket.h"
58 #include "net/spdy/spdy_framer.h" 58 #include "net/spdy/spdy_framer.h"
59 #include "net/spdy/spdy_session.h" 59 #include "net/spdy/spdy_session.h"
60 #include "net/spdy/spdy_session_pool.h" 60 #include "net/spdy/spdy_session_pool.h"
61 #include "net/spdy/spdy_test_util_common.h" 61 #include "net/spdy/spdy_test_util_common.h"
62 #include "net/ssl/ssl_cert_request_info.h" 62 #include "net/ssl/ssl_cert_request_info.h"
63 #include "net/ssl/ssl_config_service.h" 63 #include "net/ssl/ssl_config_service.h"
64 #include "net/ssl/ssl_config_service_defaults.h" 64 #include "net/ssl/ssl_config_service_defaults.h"
65 #include "net/ssl/ssl_info.h" 65 #include "net/ssl/ssl_info.h"
66 #include "net/test/cert_test_util.h" 66 #include "net/test/cert_test_util.h"
67 #include "net/websockets/websocket_handshake_stream_base.h"
67 #include "testing/gtest/include/gtest/gtest.h" 68 #include "testing/gtest/include/gtest/gtest.h"
68 #include "testing/platform_test.h" 69 #include "testing/platform_test.h"
69 #include "url/gurl.h" 70 #include "url/gurl.h"
70 71
71 //----------------------------------------------------------------------------- 72 //-----------------------------------------------------------------------------
72 73
73 namespace { 74 namespace {
74 75
75 const base::string16 kBar(ASCIIToUTF16("bar")); 76 const base::string16 kBar(ASCIIToUTF16("bar"));
76 const base::string16 kBar2(ASCIIToUTF16("bar2")); 77 const base::string16 kBar2(ASCIIToUTF16("bar2"));
(...skipping 11873 matching lines...) Expand 10 before | Expand all | Expand 10 after
11950 }; 11951 };
11951 11952
11952 // Fake HttpStreamRequest that simply records calls to SetPriority() 11953 // Fake HttpStreamRequest that simply records calls to SetPriority()
11953 // and vends FakeStreams with its current priority. 11954 // and vends FakeStreams with its current priority.
11954 class FakeStreamRequest : public HttpStreamRequest, 11955 class FakeStreamRequest : public HttpStreamRequest,
11955 public base::SupportsWeakPtr<FakeStreamRequest> { 11956 public base::SupportsWeakPtr<FakeStreamRequest> {
11956 public: 11957 public:
11957 FakeStreamRequest(RequestPriority priority, 11958 FakeStreamRequest(RequestPriority priority,
11958 HttpStreamRequest::Delegate* delegate) 11959 HttpStreamRequest::Delegate* delegate)
11959 : priority_(priority), 11960 : priority_(priority),
11960 delegate_(delegate) {} 11961 delegate_(delegate),
11962 websocket_stream_factory_(NULL) {}
11963 FakeStreamRequest(RequestPriority priority,
11964 HttpStreamRequest::Delegate* delegate,
11965 WebSocketHandshakeStreamBase::Factory* factory)
11966 : priority_(priority),
11967 delegate_(delegate),
11968 websocket_stream_factory_(factory) {}
11961 11969
11962 virtual ~FakeStreamRequest() {} 11970 virtual ~FakeStreamRequest() {}
11963 11971
11964 RequestPriority priority() const { return priority_; } 11972 RequestPriority priority() const { return priority_; }
11965 11973
11974 const WebSocketHandshakeStreamBase::Factory* websocket_stream_factory()
11975 const {
11976 return websocket_stream_factory_;
11977 }
11978
11966 // Create a new FakeStream and pass it to the request's 11979 // Create a new FakeStream and pass it to the request's
11967 // delegate. Returns a weak pointer to the FakeStream. 11980 // delegate. Returns a weak pointer to the FakeStream.
11968 base::WeakPtr<FakeStream> FinishStreamRequest() { 11981 base::WeakPtr<FakeStream> FinishStreamRequest() {
11969 FakeStream* fake_stream = new FakeStream(priority_); 11982 FakeStream* fake_stream = new FakeStream(priority_);
11970 // Do this before calling OnStreamReady() as OnStreamReady() may 11983 // Do this before calling OnStreamReady() as OnStreamReady() may
11971 // immediately delete |fake_stream|. 11984 // immediately delete |fake_stream|.
11972 base::WeakPtr<FakeStream> weak_stream = fake_stream->AsWeakPtr(); 11985 base::WeakPtr<FakeStream> weak_stream = fake_stream->AsWeakPtr();
11973 delegate_->OnStreamReady(SSLConfig(), ProxyInfo(), fake_stream); 11986 delegate_->OnStreamReady(SSLConfig(), ProxyInfo(), fake_stream);
11974 return weak_stream; 11987 return weak_stream;
11975 } 11988 }
(...skipping 21 matching lines...) Expand all
11997 return kProtoUnknown; 12010 return kProtoUnknown;
11998 } 12011 }
11999 12012
12000 virtual bool using_spdy() const OVERRIDE { 12013 virtual bool using_spdy() const OVERRIDE {
12001 return false; 12014 return false;
12002 } 12015 }
12003 12016
12004 private: 12017 private:
12005 RequestPriority priority_; 12018 RequestPriority priority_;
12006 HttpStreamRequest::Delegate* const delegate_; 12019 HttpStreamRequest::Delegate* const delegate_;
12020 WebSocketHandshakeStreamBase::Factory* websocket_stream_factory_;
12007 12021
12008 DISALLOW_COPY_AND_ASSIGN(FakeStreamRequest); 12022 DISALLOW_COPY_AND_ASSIGN(FakeStreamRequest);
12009 }; 12023 };
12010 12024
12011 // Fake HttpStreamFactory that vends FakeStreamRequests. 12025 // Fake HttpStreamFactory that vends FakeStreamRequests.
12012 class FakeStreamFactory : public HttpStreamFactory { 12026 class FakeStreamFactory : public HttpStreamFactory {
12013 public: 12027 public:
12014 FakeStreamFactory() {} 12028 FakeStreamFactory() {}
12015 virtual ~FakeStreamFactory() {} 12029 virtual ~FakeStreamFactory() {}
12016 12030
(...skipping 16 matching lines...) Expand all
12033 } 12047 }
12034 12048
12035 virtual HttpStreamRequest* RequestWebSocketHandshakeStream( 12049 virtual HttpStreamRequest* RequestWebSocketHandshakeStream(
12036 const HttpRequestInfo& info, 12050 const HttpRequestInfo& info,
12037 RequestPriority priority, 12051 RequestPriority priority,
12038 const SSLConfig& server_ssl_config, 12052 const SSLConfig& server_ssl_config,
12039 const SSLConfig& proxy_ssl_config, 12053 const SSLConfig& proxy_ssl_config,
12040 HttpStreamRequest::Delegate* delegate, 12054 HttpStreamRequest::Delegate* delegate,
12041 WebSocketHandshakeStreamBase::Factory* factory, 12055 WebSocketHandshakeStreamBase::Factory* factory,
12042 const BoundNetLog& net_log) OVERRIDE { 12056 const BoundNetLog& net_log) OVERRIDE {
12043 ADD_FAILURE(); 12057 FakeStreamRequest* fake_request =
12044 return NULL; 12058 new FakeStreamRequest(priority, delegate, factory);
12059 last_stream_request_ = fake_request->AsWeakPtr();
12060 return fake_request;
12045 } 12061 }
12046 12062
12047 virtual void PreconnectStreams(int num_streams, 12063 virtual void PreconnectStreams(int num_streams,
12048 const HttpRequestInfo& info, 12064 const HttpRequestInfo& info,
12049 RequestPriority priority, 12065 RequestPriority priority,
12050 const SSLConfig& server_ssl_config, 12066 const SSLConfig& server_ssl_config,
12051 const SSLConfig& proxy_ssl_config) OVERRIDE { 12067 const SSLConfig& proxy_ssl_config) OVERRIDE {
12052 ADD_FAILURE(); 12068 ADD_FAILURE();
12053 } 12069 }
12054 12070
12055 virtual base::Value* PipelineInfoToValue() const OVERRIDE { 12071 virtual base::Value* PipelineInfoToValue() const OVERRIDE {
12056 ADD_FAILURE(); 12072 ADD_FAILURE();
12057 return NULL; 12073 return NULL;
12058 } 12074 }
12059 12075
12060 virtual const HostMappingRules* GetHostMappingRules() const OVERRIDE { 12076 virtual const HostMappingRules* GetHostMappingRules() const OVERRIDE {
12061 ADD_FAILURE(); 12077 ADD_FAILURE();
12062 return NULL; 12078 return NULL;
12063 } 12079 }
12064 12080
12065 private: 12081 private:
12066 base::WeakPtr<FakeStreamRequest> last_stream_request_; 12082 base::WeakPtr<FakeStreamRequest> last_stream_request_;
12067 12083
12068 DISALLOW_COPY_AND_ASSIGN(FakeStreamFactory); 12084 DISALLOW_COPY_AND_ASSIGN(FakeStreamFactory);
12069 }; 12085 };
12070 12086
12087 // TODO(yhirano): Split this class out into a net/websockets file, if it is
12088 // worth doing.
12089 class FakeWebSocketStreamFactory :
12090 public WebSocketHandshakeStreamBase::Factory {
12091 public:
12092 virtual WebSocketHandshakeStreamBase* CreateBasicStream(
12093 ClientSocketHandle* connection,
12094 bool using_proxy) OVERRIDE {
12095 NOTREACHED();
12096 return NULL;
12097 }
12098
12099 virtual WebSocketHandshakeStreamBase* CreateSpdyStream(
12100 const base::WeakPtr<SpdySession>& session,
12101 bool use_relative_url) OVERRIDE {
12102 NOTREACHED();
12103 return NULL;
12104 };
12105
12106 virtual ~FakeWebSocketStreamFactory() {}
12107
12108 virtual scoped_ptr<WebSocketStream> Upgrade() {
12109 NOTREACHED();
12110 return scoped_ptr<WebSocketStream>();
12111 }
12112 };
12113
12071 } // namespace 12114 } // namespace
12072 12115
12073 // Make sure that HttpNetworkTransaction passes on its priority to its 12116 // Make sure that HttpNetworkTransaction passes on its priority to its
12074 // stream request on start. 12117 // stream request on start.
12075 TEST_P(HttpNetworkTransactionTest, SetStreamRequestPriorityOnStart) { 12118 TEST_P(HttpNetworkTransactionTest, SetStreamRequestPriorityOnStart) {
12076 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); 12119 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
12077 HttpNetworkSessionPeer peer(session); 12120 HttpNetworkSessionPeer peer(session);
12078 FakeStreamFactory* fake_factory = new FakeStreamFactory(); 12121 FakeStreamFactory* fake_factory = new FakeStreamFactory();
12079 peer.SetHttpStreamFactory(fake_factory); 12122 peer.SetHttpStreamFactory(fake_factory);
12080 12123
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
12137 fake_factory->last_stream_request(); 12180 fake_factory->last_stream_request();
12138 ASSERT_TRUE(fake_request != NULL); 12181 ASSERT_TRUE(fake_request != NULL);
12139 base::WeakPtr<FakeStream> fake_stream = fake_request->FinishStreamRequest(); 12182 base::WeakPtr<FakeStream> fake_stream = fake_request->FinishStreamRequest();
12140 ASSERT_TRUE(fake_stream != NULL); 12183 ASSERT_TRUE(fake_stream != NULL);
12141 EXPECT_EQ(LOW, fake_stream->priority()); 12184 EXPECT_EQ(LOW, fake_stream->priority());
12142 12185
12143 trans.SetPriority(LOWEST); 12186 trans.SetPriority(LOWEST);
12144 EXPECT_EQ(LOWEST, fake_stream->priority()); 12187 EXPECT_EQ(LOWEST, fake_stream->priority());
12145 } 12188 }
12146 12189
12190 TEST_P(HttpNetworkTransactionTest, CreateWebSocketHandshakeStream) {
12191 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
12192 HttpNetworkSessionPeer peer(session);
12193 FakeStreamFactory* fake_factory = new FakeStreamFactory();
12194 FakeWebSocketStreamFactory websocket_stream_factory;
12195 peer.SetWebSocketHandshakeStreamFactory(fake_factory);
12196
12197 HttpNetworkTransaction trans(LOW, session);
12198 trans.SetWebSocketHandshakeStreamFactory(&websocket_stream_factory);
12199
12200 HttpRequestInfo request;
12201 TestCompletionCallback callback;
12202 request.method = "GET";
12203 request.url = GURL("ws://www.google.com/");
12204
12205 EXPECT_EQ(ERR_IO_PENDING,
12206 trans.Start(&request, callback.callback(), BoundNetLog()));
12207
12208 base::WeakPtr<FakeStreamRequest> fake_request =
12209 fake_factory->last_stream_request();
12210 ASSERT_TRUE(fake_request != NULL);
12211 EXPECT_EQ(&websocket_stream_factory,
12212 fake_request->websocket_stream_factory());
12213 }
12214
12215 TEST_P(HttpNetworkTransactionTest, CreateWebSocketSecureHandshakeStream) {
12216 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
12217 HttpNetworkSessionPeer peer(session);
12218 FakeStreamFactory* fake_factory = new FakeStreamFactory();
12219 FakeWebSocketStreamFactory websocket_stream_factory;
12220 peer.SetWebSocketHandshakeStreamFactory(fake_factory);
12221
12222 HttpNetworkTransaction trans(LOW, session);
12223 trans.SetWebSocketHandshakeStreamFactory(&websocket_stream_factory);
12224
12225 HttpRequestInfo request;
12226 TestCompletionCallback callback;
12227 request.method = "GET";
12228 request.url = GURL("wss://www.google.com/");
12229
12230 EXPECT_EQ(ERR_IO_PENDING,
12231 trans.Start(&request, callback.callback(), BoundNetLog()));
12232
12233 base::WeakPtr<FakeStreamRequest> fake_request =
12234 fake_factory->last_stream_request();
12235 ASSERT_TRUE(fake_request != NULL);
12236 EXPECT_EQ(&websocket_stream_factory,
12237 fake_request->websocket_stream_factory());
12238 }
12239
12147 // Tests that when a used socket is returned to the SSL socket pool, it's closed 12240 // Tests that when a used socket is returned to the SSL socket pool, it's closed
12148 // if the transport socket pool is stalled on the global socket limit. 12241 // if the transport socket pool is stalled on the global socket limit.
12149 TEST_P(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest) { 12242 TEST_P(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest) {
12150 ClientSocketPoolManager::set_max_sockets_per_group( 12243 ClientSocketPoolManager::set_max_sockets_per_group(
12151 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); 12244 HttpNetworkSession::NORMAL_SOCKET_POOL, 1);
12152 ClientSocketPoolManager::set_max_sockets_per_pool( 12245 ClientSocketPoolManager::set_max_sockets_per_pool(
12153 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); 12246 HttpNetworkSession::NORMAL_SOCKET_POOL, 1);
12154 12247
12155 // Set up SSL request. 12248 // Set up SSL request.
12156 12249
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
12302 // established, to let the HTTP request start. 12395 // established, to let the HTTP request start.
12303 ASSERT_EQ(OK, http_callback.WaitForResult()); 12396 ASSERT_EQ(OK, http_callback.WaitForResult());
12304 std::string response_data; 12397 std::string response_data;
12305 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); 12398 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data));
12306 EXPECT_EQ("falafel", response_data); 12399 EXPECT_EQ("falafel", response_data);
12307 12400
12308 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); 12401 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session));
12309 } 12402 }
12310 12403
12311 } // namespace net 12404 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698