| 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/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 <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 9355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9366 // Select a host resolver that does caching. | 9366 // Select a host resolver that does caching. |
| 9367 session_deps_.host_resolver.reset(new MockCachingHostResolver); | 9367 session_deps_.host_resolver.reset(new MockCachingHostResolver); |
| 9368 | 9368 |
| 9369 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | 9369 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 9370 std::unique_ptr<HttpTransaction> trans( | 9370 std::unique_ptr<HttpTransaction> trans( |
| 9371 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | 9371 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 9372 | 9372 |
| 9373 // Warm up the host cache so it has an entry for "www.example.org". | 9373 // Warm up the host cache so it has an entry for "www.example.org". |
| 9374 AddressList addrlist; | 9374 AddressList addrlist; |
| 9375 TestCompletionCallback callback; | 9375 TestCompletionCallback callback; |
| 9376 std::unique_ptr<HostResolver::Request> req1; |
| 9376 int rv = session_deps_.host_resolver->Resolve( | 9377 int rv = session_deps_.host_resolver->Resolve( |
| 9377 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), | 9378 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), |
| 9378 DEFAULT_PRIORITY, &addrlist, callback.callback(), NULL, BoundNetLog()); | 9379 DEFAULT_PRIORITY, &addrlist, callback.callback(), &req1, BoundNetLog()); |
| 9379 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 9380 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 9380 rv = callback.WaitForResult(); | 9381 rv = callback.WaitForResult(); |
| 9381 EXPECT_THAT(rv, IsOk()); | 9382 EXPECT_THAT(rv, IsOk()); |
| 9382 | 9383 |
| 9383 // Verify that it was added to host cache, by doing a subsequent async lookup | 9384 // Verify that it was added to host cache, by doing a subsequent async lookup |
| 9384 // and confirming it completes synchronously. | 9385 // and confirming it completes synchronously. |
| 9386 std::unique_ptr<HostResolver::Request> req2; |
| 9385 rv = session_deps_.host_resolver->Resolve( | 9387 rv = session_deps_.host_resolver->Resolve( |
| 9386 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), | 9388 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), |
| 9387 DEFAULT_PRIORITY, &addrlist, callback.callback(), NULL, BoundNetLog()); | 9389 DEFAULT_PRIORITY, &addrlist, callback.callback(), &req2, BoundNetLog()); |
| 9388 ASSERT_THAT(rv, IsOk()); | 9390 ASSERT_THAT(rv, IsOk()); |
| 9389 | 9391 |
| 9390 // Inject a failure the next time that "www.example.org" is resolved. This way | 9392 // Inject a failure the next time that "www.example.org" is resolved. This way |
| 9391 // we can tell if the next lookup hit the cache, or the "network". | 9393 // we can tell if the next lookup hit the cache, or the "network". |
| 9392 // (cache --> success, "network" --> failure). | 9394 // (cache --> success, "network" --> failure). |
| 9393 session_deps_.host_resolver->rules()->AddSimulatedFailure("www.example.org"); | 9395 session_deps_.host_resolver->rules()->AddSimulatedFailure("www.example.org"); |
| 9394 | 9396 |
| 9395 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the | 9397 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
| 9396 // first read -- this won't be reached as the host resolution will fail first. | 9398 // first read -- this won't be reached as the host resolution will fail first. |
| 9397 MockRead data_reads[] = { MockRead(SYNCHRONOUS, ERR_UNEXPECTED) }; | 9399 MockRead data_reads[] = { MockRead(SYNCHRONOUS, ERR_UNEXPECTED) }; |
| (...skipping 3513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12911 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); | 12913 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 12912 | 12914 |
| 12913 std::string response_data; | 12915 std::string response_data; |
| 12914 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); | 12916 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 12915 EXPECT_EQ("hello!", response_data); | 12917 EXPECT_EQ("hello!", response_data); |
| 12916 | 12918 |
| 12917 // Preload www.gmail.com into HostCache. | 12919 // Preload www.gmail.com into HostCache. |
| 12918 HostPortPair host_port("www.gmail.com", 443); | 12920 HostPortPair host_port("www.gmail.com", 443); |
| 12919 HostResolver::RequestInfo resolve_info(host_port); | 12921 HostResolver::RequestInfo resolve_info(host_port); |
| 12920 AddressList ignored; | 12922 AddressList ignored; |
| 12921 rv = session_deps_.host_resolver->Resolve(resolve_info, | 12923 std::unique_ptr<HostResolver::Request> req; |
| 12922 DEFAULT_PRIORITY, | 12924 rv = session_deps_.host_resolver->Resolve(resolve_info, DEFAULT_PRIORITY, |
| 12923 &ignored, | 12925 &ignored, callback.callback(), &req, |
| 12924 callback.callback(), | |
| 12925 NULL, | |
| 12926 BoundNetLog()); | 12926 BoundNetLog()); |
| 12927 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 12927 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 12928 rv = callback.WaitForResult(); | 12928 rv = callback.WaitForResult(); |
| 12929 EXPECT_THAT(rv, IsOk()); | 12929 EXPECT_THAT(rv, IsOk()); |
| 12930 | 12930 |
| 12931 HttpRequestInfo request2; | 12931 HttpRequestInfo request2; |
| 12932 request2.method = "GET"; | 12932 request2.method = "GET"; |
| 12933 request2.url = GURL("https://www.gmail.com/"); | 12933 request2.url = GURL("https://www.gmail.com/"); |
| 12934 request2.load_flags = 0; | 12934 request2.load_flags = 0; |
| 12935 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); | 12935 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 13035 : host_port_(host_port) {} | 13035 : host_port_(host_port) {} |
| 13036 ~OneTimeCachingHostResolver() override {} | 13036 ~OneTimeCachingHostResolver() override {} |
| 13037 | 13037 |
| 13038 RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); } | 13038 RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); } |
| 13039 | 13039 |
| 13040 // HostResolver methods: | 13040 // HostResolver methods: |
| 13041 int Resolve(const RequestInfo& info, | 13041 int Resolve(const RequestInfo& info, |
| 13042 RequestPriority priority, | 13042 RequestPriority priority, |
| 13043 AddressList* addresses, | 13043 AddressList* addresses, |
| 13044 const CompletionCallback& callback, | 13044 const CompletionCallback& callback, |
| 13045 RequestHandle* out_req, | 13045 std::unique_ptr<Request>* out_req, |
| 13046 const BoundNetLog& net_log) override { | 13046 const BoundNetLog& net_log) override { |
| 13047 return host_resolver_.Resolve( | 13047 return host_resolver_.Resolve( |
| 13048 info, priority, addresses, callback, out_req, net_log); | 13048 info, priority, addresses, callback, out_req, net_log); |
| 13049 } | 13049 } |
| 13050 | 13050 |
| 13051 int ResolveFromCache(const RequestInfo& info, | 13051 int ResolveFromCache(const RequestInfo& info, |
| 13052 AddressList* addresses, | 13052 AddressList* addresses, |
| 13053 const BoundNetLog& net_log) override { | 13053 const BoundNetLog& net_log) override { |
| 13054 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); | 13054 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); |
| 13055 if (rv == OK && info.host_port_pair().Equals(host_port_)) | 13055 if (rv == OK && info.host_port_pair().Equals(host_port_)) |
| 13056 host_resolver_.GetHostCache()->clear(); | 13056 host_resolver_.GetHostCache()->clear(); |
| 13057 return rv; | 13057 return rv; |
| 13058 } | 13058 } |
| 13059 | 13059 |
| 13060 void CancelRequest(RequestHandle req) override { | |
| 13061 host_resolver_.CancelRequest(req); | |
| 13062 } | |
| 13063 | |
| 13064 MockCachingHostResolver* GetMockHostResolver() { | 13060 MockCachingHostResolver* GetMockHostResolver() { |
| 13065 return &host_resolver_; | 13061 return &host_resolver_; |
| 13066 } | 13062 } |
| 13067 | 13063 |
| 13068 private: | 13064 private: |
| 13069 MockCachingHostResolver host_resolver_; | 13065 MockCachingHostResolver host_resolver_; |
| 13070 const HostPortPair host_port_; | 13066 const HostPortPair host_port_; |
| 13071 }; | 13067 }; |
| 13072 | 13068 |
| 13073 TEST_P(HttpNetworkTransactionTest, | 13069 TEST_P(HttpNetworkTransactionTest, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 13131 ASSERT_TRUE(response->headers); | 13127 ASSERT_TRUE(response->headers); |
| 13132 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); | 13128 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 13133 | 13129 |
| 13134 std::string response_data; | 13130 std::string response_data; |
| 13135 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); | 13131 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 13136 EXPECT_EQ("hello!", response_data); | 13132 EXPECT_EQ("hello!", response_data); |
| 13137 | 13133 |
| 13138 // Preload cache entries into HostCache. | 13134 // Preload cache entries into HostCache. |
| 13139 HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443)); | 13135 HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443)); |
| 13140 AddressList ignored; | 13136 AddressList ignored; |
| 13141 rv = host_resolver.Resolve(resolve_info, | 13137 std::unique_ptr<HostResolver::Request> req; |
| 13142 DEFAULT_PRIORITY, | 13138 rv = host_resolver.Resolve(resolve_info, DEFAULT_PRIORITY, &ignored, |
| 13143 &ignored, | 13139 callback.callback(), &req, BoundNetLog()); |
| 13144 callback.callback(), | |
| 13145 NULL, | |
| 13146 BoundNetLog()); | |
| 13147 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 13140 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13148 rv = callback.WaitForResult(); | 13141 rv = callback.WaitForResult(); |
| 13149 EXPECT_THAT(rv, IsOk()); | 13142 EXPECT_THAT(rv, IsOk()); |
| 13150 | 13143 |
| 13151 HttpRequestInfo request2; | 13144 HttpRequestInfo request2; |
| 13152 request2.method = "GET"; | 13145 request2.method = "GET"; |
| 13153 request2.url = GURL("https://www.gmail.com/"); | 13146 request2.url = GURL("https://www.gmail.com/"); |
| 13154 request2.load_flags = 0; | 13147 request2.load_flags = 0; |
| 13155 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); | 13148 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| 13156 | 13149 |
| (...skipping 2771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 15928 base::RunLoop().RunUntilIdle(); | 15921 base::RunLoop().RunUntilIdle(); |
| 15929 | 15922 |
| 15930 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy); | 15923 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy); |
| 15931 HttpRequestHeaders headers; | 15924 HttpRequestHeaders headers; |
| 15932 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers)); | 15925 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers)); |
| 15933 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding)); | 15926 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding)); |
| 15934 } | 15927 } |
| 15935 #endif // !defined(OS_IOS) | 15928 #endif // !defined(OS_IOS) |
| 15936 | 15929 |
| 15937 } // namespace net | 15930 } // namespace net |
| OLD | NEW |