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

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

Issue 2116983002: Change HostResolver::Resolve() to take an std::unique_ptr<Request>* rather than a RequestHandle* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: http_stream_factory_impl_job_controller_unittest RequestHandle* to unique_ptr Created 4 years, 4 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
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 <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 9272 matching lines...) Expand 10 before | Expand all | Expand 10 after
9283 9283
9284 rv = callback.WaitForResult(); 9284 rv = callback.WaitForResult();
9285 EXPECT_THAT(rv, IsError(ERR_PROXY_CONNECTION_FAILED)); 9285 EXPECT_THAT(rv, IsError(ERR_PROXY_CONNECTION_FAILED));
9286 } 9286 }
9287 9287
9288 // Base test to make sure that when the load flags for a request specify to 9288 // Base test to make sure that when the load flags for a request specify to
9289 // bypass the cache, the DNS cache is not used. 9289 // bypass the cache, the DNS cache is not used.
9290 void HttpNetworkTransactionTest::BypassHostCacheOnRefreshHelper( 9290 void HttpNetworkTransactionTest::BypassHostCacheOnRefreshHelper(
9291 int load_flags) { 9291 int load_flags) {
9292 // Issue a request, asking to bypass the cache(s). 9292 // Issue a request, asking to bypass the cache(s).
9293 HttpRequestInfo request; 9293 HttpRequestInfo request_info;
9294 request.method = "GET"; 9294 request_info.method = "GET";
9295 request.load_flags = load_flags; 9295 request_info.load_flags = load_flags;
9296 request.url = GURL("http://www.example.org/"); 9296 request_info.url = GURL("http://www.example.org/");
9297 9297
9298 // Select a host resolver that does caching. 9298 // Select a host resolver that does caching.
9299 session_deps_.host_resolver.reset(new MockCachingHostResolver); 9299 session_deps_.host_resolver.reset(new MockCachingHostResolver);
9300 9300
9301 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); 9301 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_));
9302 std::unique_ptr<HttpTransaction> trans( 9302 std::unique_ptr<HttpTransaction> trans(
9303 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); 9303 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
9304 9304
9305 // Warm up the host cache so it has an entry for "www.example.org". 9305 // Warm up the host cache so it has an entry for "www.example.org".
9306 AddressList addrlist; 9306 AddressList addrlist;
9307 TestCompletionCallback callback; 9307 TestCompletionCallback callback;
9308 std::unique_ptr<HostResolver::Request> request1;
9308 int rv = session_deps_.host_resolver->Resolve( 9309 int rv = session_deps_.host_resolver->Resolve(
9309 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), 9310 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)),
9310 DEFAULT_PRIORITY, &addrlist, callback.callback(), NULL, BoundNetLog()); 9311 DEFAULT_PRIORITY, &addrlist, callback.callback(), &request1,
9312 BoundNetLog());
9311 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 9313 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
9312 rv = callback.WaitForResult(); 9314 rv = callback.WaitForResult();
9313 EXPECT_THAT(rv, IsOk()); 9315 EXPECT_THAT(rv, IsOk());
9314 9316
9315 // Verify that it was added to host cache, by doing a subsequent async lookup 9317 // Verify that it was added to host cache, by doing a subsequent async lookup
9316 // and confirming it completes synchronously. 9318 // and confirming it completes synchronously.
9319 std::unique_ptr<HostResolver::Request> request2;
9317 rv = session_deps_.host_resolver->Resolve( 9320 rv = session_deps_.host_resolver->Resolve(
9318 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), 9321 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)),
9319 DEFAULT_PRIORITY, &addrlist, callback.callback(), NULL, BoundNetLog()); 9322 DEFAULT_PRIORITY, &addrlist, callback.callback(), &request2,
9323 BoundNetLog());
9320 ASSERT_THAT(rv, IsOk()); 9324 ASSERT_THAT(rv, IsOk());
9321 9325
9322 // Inject a failure the next time that "www.example.org" is resolved. This way 9326 // Inject a failure the next time that "www.example.org" is resolved. This way
9323 // we can tell if the next lookup hit the cache, or the "network". 9327 // we can tell if the next lookup hit the cache, or the "network".
9324 // (cache --> success, "network" --> failure). 9328 // (cache --> success, "network" --> failure).
9325 session_deps_.host_resolver->rules()->AddSimulatedFailure("www.example.org"); 9329 session_deps_.host_resolver->rules()->AddSimulatedFailure("www.example.org");
9326 9330
9327 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the 9331 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the
9328 // first read -- this won't be reached as the host resolution will fail first. 9332 // first read -- this won't be reached as the host resolution will fail first.
9329 MockRead data_reads[] = { MockRead(SYNCHRONOUS, ERR_UNEXPECTED) }; 9333 MockRead data_reads[] = { MockRead(SYNCHRONOUS, ERR_UNEXPECTED) };
9330 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); 9334 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0);
9331 session_deps_.socket_factory->AddSocketDataProvider(&data); 9335 session_deps_.socket_factory->AddSocketDataProvider(&data);
9332 9336
9333 // Run the request. 9337 // Run the request.
9334 rv = trans->Start(&request, callback.callback(), BoundNetLog()); 9338 rv = trans->Start(&request_info, callback.callback(), BoundNetLog());
9335 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); 9339 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
9336 rv = callback.WaitForResult(); 9340 rv = callback.WaitForResult();
9337 9341
9338 // If we bypassed the cache, we would have gotten a failure while resolving 9342 // If we bypassed the cache, we would have gotten a failure while resolving
9339 // "www.example.org". 9343 // "www.example.org".
9340 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); 9344 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED));
9341 } 9345 }
9342 9346
9343 // There are multiple load flags that should trigger the host cache bypass. 9347 // There are multiple load flags that should trigger the host cache bypass.
9344 // Test each in isolation: 9348 // Test each in isolation:
(...skipping 3435 matching lines...) Expand 10 before | Expand all | Expand 10 after
12780 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); 12784 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine());
12781 12785
12782 std::string response_data; 12786 std::string response_data;
12783 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); 12787 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk());
12784 EXPECT_EQ("hello!", response_data); 12788 EXPECT_EQ("hello!", response_data);
12785 12789
12786 // Preload www.gmail.com into HostCache. 12790 // Preload www.gmail.com into HostCache.
12787 HostPortPair host_port("www.gmail.com", 443); 12791 HostPortPair host_port("www.gmail.com", 443);
12788 HostResolver::RequestInfo resolve_info(host_port); 12792 HostResolver::RequestInfo resolve_info(host_port);
12789 AddressList ignored; 12793 AddressList ignored;
12790 rv = session_deps_.host_resolver->Resolve(resolve_info, 12794 std::unique_ptr<HostResolver::Request> request;
12791 DEFAULT_PRIORITY, 12795 rv = session_deps_.host_resolver->Resolve(resolve_info, DEFAULT_PRIORITY,
12792 &ignored, 12796 &ignored, callback.callback(),
12793 callback.callback(), 12797 &request, BoundNetLog());
12794 NULL,
12795 BoundNetLog());
12796 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 12798 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
12797 rv = callback.WaitForResult(); 12799 rv = callback.WaitForResult();
12798 EXPECT_THAT(rv, IsOk()); 12800 EXPECT_THAT(rv, IsOk());
12799 12801
12800 HttpRequestInfo request2; 12802 HttpRequestInfo request2;
12801 request2.method = "GET"; 12803 request2.method = "GET";
12802 request2.url = GURL("https://www.gmail.com/"); 12804 request2.url = GURL("https://www.gmail.com/");
12803 request2.load_flags = 0; 12805 request2.load_flags = 0;
12804 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); 12806 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get());
12805 12807
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
12900 : host_port_(host_port) {} 12902 : host_port_(host_port) {}
12901 ~OneTimeCachingHostResolver() override {} 12903 ~OneTimeCachingHostResolver() override {}
12902 12904
12903 RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); } 12905 RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); }
12904 12906
12905 // HostResolver methods: 12907 // HostResolver methods:
12906 int Resolve(const RequestInfo& info, 12908 int Resolve(const RequestInfo& info,
12907 RequestPriority priority, 12909 RequestPriority priority,
12908 AddressList* addresses, 12910 AddressList* addresses,
12909 const CompletionCallback& callback, 12911 const CompletionCallback& callback,
12910 RequestHandle* out_req, 12912 std::unique_ptr<Request>* out_req,
12911 const BoundNetLog& net_log) override { 12913 const BoundNetLog& net_log) override {
12912 return host_resolver_.Resolve( 12914 return host_resolver_.Resolve(
12913 info, priority, addresses, callback, out_req, net_log); 12915 info, priority, addresses, callback, out_req, net_log);
12914 } 12916 }
12915 12917
12916 int ResolveFromCache(const RequestInfo& info, 12918 int ResolveFromCache(const RequestInfo& info,
12917 AddressList* addresses, 12919 AddressList* addresses,
12918 const BoundNetLog& net_log) override { 12920 const BoundNetLog& net_log) override {
12919 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); 12921 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log);
12920 if (rv == OK && info.host_port_pair().Equals(host_port_)) 12922 if (rv == OK && info.host_port_pair().Equals(host_port_))
12921 host_resolver_.GetHostCache()->clear(); 12923 host_resolver_.GetHostCache()->clear();
12922 return rv; 12924 return rv;
12923 } 12925 }
12924 12926
12925 void CancelRequest(RequestHandle req) override {
12926 host_resolver_.CancelRequest(req);
12927 }
12928
12929 MockCachingHostResolver* GetMockHostResolver() { 12927 MockCachingHostResolver* GetMockHostResolver() {
12930 return &host_resolver_; 12928 return &host_resolver_;
12931 } 12929 }
12932 12930
12933 private: 12931 private:
12934 MockCachingHostResolver host_resolver_; 12932 MockCachingHostResolver host_resolver_;
12935 const HostPortPair host_port_; 12933 const HostPortPair host_port_;
12936 }; 12934 };
12937 12935
12938 TEST_F(HttpNetworkTransactionTest, 12936 TEST_F(HttpNetworkTransactionTest,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
12992 ASSERT_TRUE(response->headers); 12990 ASSERT_TRUE(response->headers);
12993 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); 12991 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine());
12994 12992
12995 std::string response_data; 12993 std::string response_data;
12996 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); 12994 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk());
12997 EXPECT_EQ("hello!", response_data); 12995 EXPECT_EQ("hello!", response_data);
12998 12996
12999 // Preload cache entries into HostCache. 12997 // Preload cache entries into HostCache.
13000 HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443)); 12998 HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443));
13001 AddressList ignored; 12999 AddressList ignored;
13002 rv = host_resolver.Resolve(resolve_info, 13000 std::unique_ptr<HostResolver::Request> request;
13003 DEFAULT_PRIORITY, 13001 rv = host_resolver.Resolve(resolve_info, DEFAULT_PRIORITY, &ignored,
13004 &ignored, 13002 callback.callback(), &request, BoundNetLog());
13005 callback.callback(),
13006 NULL,
13007 BoundNetLog());
13008 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 13003 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
13009 rv = callback.WaitForResult(); 13004 rv = callback.WaitForResult();
13010 EXPECT_THAT(rv, IsOk()); 13005 EXPECT_THAT(rv, IsOk());
13011 13006
13012 HttpRequestInfo request2; 13007 HttpRequestInfo request2;
13013 request2.method = "GET"; 13008 request2.method = "GET";
13014 request2.url = GURL("https://www.gmail.com/"); 13009 request2.url = GURL("https://www.gmail.com/");
13015 request2.load_flags = 0; 13010 request2.load_flags = 0;
13016 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); 13011 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get());
13017 13012
(...skipping 2562 matching lines...) Expand 10 before | Expand all | Expand 10 after
15580 base::RunLoop().RunUntilIdle(); 15575 base::RunLoop().RunUntilIdle();
15581 15576
15582 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy); 15577 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy);
15583 HttpRequestHeaders headers; 15578 HttpRequestHeaders headers;
15584 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers)); 15579 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers));
15585 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding)); 15580 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding));
15586 } 15581 }
15587 #endif // !defined(OS_IOS) 15582 #endif // !defined(OS_IOS)
15588 15583
15589 } // namespace net 15584 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698