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

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: changed implementation of attach/detach of request Created 4 years, 5 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 9311 matching lines...) Expand 10 before | Expand all | Expand 10 after
9322 9322
9323 rv = callback.WaitForResult(); 9323 rv = callback.WaitForResult();
9324 EXPECT_THAT(rv, IsError(ERR_PROXY_CONNECTION_FAILED)); 9324 EXPECT_THAT(rv, IsError(ERR_PROXY_CONNECTION_FAILED));
9325 } 9325 }
9326 9326
9327 // Base test to make sure that when the load flags for a request specify to 9327 // Base test to make sure that when the load flags for a request specify to
9328 // bypass the cache, the DNS cache is not used. 9328 // bypass the cache, the DNS cache is not used.
9329 void HttpNetworkTransactionTest::BypassHostCacheOnRefreshHelper( 9329 void HttpNetworkTransactionTest::BypassHostCacheOnRefreshHelper(
9330 int load_flags) { 9330 int load_flags) {
9331 // Issue a request, asking to bypass the cache(s). 9331 // Issue a request, asking to bypass the cache(s).
9332 HttpRequestInfo request; 9332 HttpRequestInfo request_info;
9333 request.method = "GET"; 9333 request_info.method = "GET";
9334 request.load_flags = load_flags; 9334 request_info.load_flags = load_flags;
9335 request.url = GURL("http://www.example.org/"); 9335 request_info.url = GURL("http://www.example.org/");
9336 9336
9337 // Select a host resolver that does caching. 9337 // Select a host resolver that does caching.
9338 session_deps_.host_resolver.reset(new MockCachingHostResolver); 9338 session_deps_.host_resolver.reset(new MockCachingHostResolver);
9339 9339
9340 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); 9340 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_));
9341 std::unique_ptr<HttpTransaction> trans( 9341 std::unique_ptr<HttpTransaction> trans(
9342 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); 9342 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
9343 9343
9344 // Warm up the host cache so it has an entry for "www.example.org". 9344 // Warm up the host cache so it has an entry for "www.example.org".
9345 AddressList addrlist; 9345 AddressList addrlist;
9346 TestCompletionCallback callback; 9346 TestCompletionCallback callback;
9347 std::unique_ptr<HostResolver::Request> request1;
9347 int rv = session_deps_.host_resolver->Resolve( 9348 int rv = session_deps_.host_resolver->Resolve(
9348 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), 9349 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)),
9349 DEFAULT_PRIORITY, &addrlist, callback.callback(), NULL, BoundNetLog()); 9350 DEFAULT_PRIORITY, &addrlist, callback.callback(), &request1,
9351 BoundNetLog());
9350 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 9352 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
9351 rv = callback.WaitForResult(); 9353 rv = callback.WaitForResult();
9352 EXPECT_THAT(rv, IsOk()); 9354 EXPECT_THAT(rv, IsOk());
9353 9355
9354 // Verify that it was added to host cache, by doing a subsequent async lookup 9356 // Verify that it was added to host cache, by doing a subsequent async lookup
9355 // and confirming it completes synchronously. 9357 // and confirming it completes synchronously.
9358 std::unique_ptr<HostResolver::Request> request2;
9356 rv = session_deps_.host_resolver->Resolve( 9359 rv = session_deps_.host_resolver->Resolve(
9357 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)), 9360 HostResolver::RequestInfo(HostPortPair("www.example.org", 80)),
9358 DEFAULT_PRIORITY, &addrlist, callback.callback(), NULL, BoundNetLog()); 9361 DEFAULT_PRIORITY, &addrlist, callback.callback(), &request2,
9362 BoundNetLog());
9359 ASSERT_THAT(rv, IsOk()); 9363 ASSERT_THAT(rv, IsOk());
9360 9364
9361 // Inject a failure the next time that "www.example.org" is resolved. This way 9365 // Inject a failure the next time that "www.example.org" is resolved. This way
9362 // we can tell if the next lookup hit the cache, or the "network". 9366 // we can tell if the next lookup hit the cache, or the "network".
9363 // (cache --> success, "network" --> failure). 9367 // (cache --> success, "network" --> failure).
9364 session_deps_.host_resolver->rules()->AddSimulatedFailure("www.example.org"); 9368 session_deps_.host_resolver->rules()->AddSimulatedFailure("www.example.org");
9365 9369
9366 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the 9370 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the
9367 // first read -- this won't be reached as the host resolution will fail first. 9371 // first read -- this won't be reached as the host resolution will fail first.
9368 MockRead data_reads[] = { MockRead(SYNCHRONOUS, ERR_UNEXPECTED) }; 9372 MockRead data_reads[] = { MockRead(SYNCHRONOUS, ERR_UNEXPECTED) };
9369 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); 9373 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0);
9370 session_deps_.socket_factory->AddSocketDataProvider(&data); 9374 session_deps_.socket_factory->AddSocketDataProvider(&data);
9371 9375
9372 // Run the request. 9376 // Run the request.
9373 rv = trans->Start(&request, callback.callback(), BoundNetLog()); 9377 rv = trans->Start(&request_info, callback.callback(), BoundNetLog());
9374 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); 9378 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
9375 rv = callback.WaitForResult(); 9379 rv = callback.WaitForResult();
9376 9380
9377 // If we bypassed the cache, we would have gotten a failure while resolving 9381 // If we bypassed the cache, we would have gotten a failure while resolving
9378 // "www.example.org". 9382 // "www.example.org".
9379 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); 9383 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED));
9380 } 9384 }
9381 9385
9382 // There are multiple load flags that should trigger the host cache bypass. 9386 // There are multiple load flags that should trigger the host cache bypass.
9383 // Test each in isolation: 9387 // Test each in isolation:
(...skipping 3445 matching lines...) Expand 10 before | Expand all | Expand 10 after
12829 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); 12833 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine());
12830 12834
12831 std::string response_data; 12835 std::string response_data;
12832 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); 12836 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk());
12833 EXPECT_EQ("hello!", response_data); 12837 EXPECT_EQ("hello!", response_data);
12834 12838
12835 // Preload www.gmail.com into HostCache. 12839 // Preload www.gmail.com into HostCache.
12836 HostPortPair host_port("www.gmail.com", 443); 12840 HostPortPair host_port("www.gmail.com", 443);
12837 HostResolver::RequestInfo resolve_info(host_port); 12841 HostResolver::RequestInfo resolve_info(host_port);
12838 AddressList ignored; 12842 AddressList ignored;
12839 rv = session_deps_.host_resolver->Resolve(resolve_info, 12843 std::unique_ptr<HostResolver::Request> request;
12840 DEFAULT_PRIORITY, 12844 rv = session_deps_.host_resolver->Resolve(resolve_info, DEFAULT_PRIORITY,
12841 &ignored, 12845 &ignored, callback.callback(),
12842 callback.callback(), 12846 &request, BoundNetLog());
12843 NULL,
12844 BoundNetLog());
12845 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 12847 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
12846 rv = callback.WaitForResult(); 12848 rv = callback.WaitForResult();
12847 EXPECT_THAT(rv, IsOk()); 12849 EXPECT_THAT(rv, IsOk());
12848 12850
12849 HttpRequestInfo request2; 12851 HttpRequestInfo request2;
12850 request2.method = "GET"; 12852 request2.method = "GET";
12851 request2.url = GURL("https://www.gmail.com/"); 12853 request2.url = GURL("https://www.gmail.com/");
12852 request2.load_flags = 0; 12854 request2.load_flags = 0;
12853 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); 12855 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get());
12854 12856
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
12951 : host_port_(host_port) {} 12953 : host_port_(host_port) {}
12952 ~OneTimeCachingHostResolver() override {} 12954 ~OneTimeCachingHostResolver() override {}
12953 12955
12954 RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); } 12956 RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); }
12955 12957
12956 // HostResolver methods: 12958 // HostResolver methods:
12957 int Resolve(const RequestInfo& info, 12959 int Resolve(const RequestInfo& info,
12958 RequestPriority priority, 12960 RequestPriority priority,
12959 AddressList* addresses, 12961 AddressList* addresses,
12960 const CompletionCallback& callback, 12962 const CompletionCallback& callback,
12961 RequestHandle* out_req, 12963 std::unique_ptr<Request>* out_req,
12962 const BoundNetLog& net_log) override { 12964 const BoundNetLog& net_log) override {
12963 return host_resolver_.Resolve( 12965 return host_resolver_.Resolve(
12964 info, priority, addresses, callback, out_req, net_log); 12966 info, priority, addresses, callback, out_req, net_log);
12965 } 12967 }
12966 12968
12967 int ResolveFromCache(const RequestInfo& info, 12969 int ResolveFromCache(const RequestInfo& info,
12968 AddressList* addresses, 12970 AddressList* addresses,
12969 const BoundNetLog& net_log) override { 12971 const BoundNetLog& net_log) override {
12970 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); 12972 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log);
12971 if (rv == OK && info.host_port_pair().Equals(host_port_)) 12973 if (rv == OK && info.host_port_pair().Equals(host_port_))
12972 host_resolver_.GetHostCache()->clear(); 12974 host_resolver_.GetHostCache()->clear();
12973 return rv; 12975 return rv;
12974 } 12976 }
12975 12977
12976 void CancelRequest(RequestHandle req) override {
12977 host_resolver_.CancelRequest(req);
12978 }
12979
12980 MockCachingHostResolver* GetMockHostResolver() { 12978 MockCachingHostResolver* GetMockHostResolver() {
12981 return &host_resolver_; 12979 return &host_resolver_;
12982 } 12980 }
12983 12981
12984 private: 12982 private:
12985 MockCachingHostResolver host_resolver_; 12983 MockCachingHostResolver host_resolver_;
12986 const HostPortPair host_port_; 12984 const HostPortPair host_port_;
12987 }; 12985 };
12988 12986
12989 TEST_P(HttpNetworkTransactionTest, 12987 TEST_P(HttpNetworkTransactionTest,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
13045 ASSERT_TRUE(response->headers); 13043 ASSERT_TRUE(response->headers);
13046 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); 13044 EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine());
13047 13045
13048 std::string response_data; 13046 std::string response_data;
13049 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); 13047 ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk());
13050 EXPECT_EQ("hello!", response_data); 13048 EXPECT_EQ("hello!", response_data);
13051 13049
13052 // Preload cache entries into HostCache. 13050 // Preload cache entries into HostCache.
13053 HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443)); 13051 HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443));
13054 AddressList ignored; 13052 AddressList ignored;
13055 rv = host_resolver.Resolve(resolve_info, 13053 std::unique_ptr<HostResolver::Request> request;
13056 DEFAULT_PRIORITY, 13054 rv = host_resolver.Resolve(resolve_info, DEFAULT_PRIORITY, &ignored,
13057 &ignored, 13055 callback.callback(), &request, BoundNetLog());
13058 callback.callback(),
13059 NULL,
13060 BoundNetLog());
13061 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 13056 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
13062 rv = callback.WaitForResult(); 13057 rv = callback.WaitForResult();
13063 EXPECT_THAT(rv, IsOk()); 13058 EXPECT_THAT(rv, IsOk());
13064 13059
13065 HttpRequestInfo request2; 13060 HttpRequestInfo request2;
13066 request2.method = "GET"; 13061 request2.method = "GET";
13067 request2.url = GURL("https://www.gmail.com/"); 13062 request2.url = GURL("https://www.gmail.com/");
13068 request2.load_flags = 0; 13063 request2.load_flags = 0;
13069 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); 13064 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get());
13070 13065
(...skipping 2565 matching lines...) Expand 10 before | Expand all | Expand 10 after
15636 base::RunLoop().RunUntilIdle(); 15631 base::RunLoop().RunUntilIdle();
15637 15632
15638 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy); 15633 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy);
15639 HttpRequestHeaders headers; 15634 HttpRequestHeaders headers;
15640 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers)); 15635 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers));
15641 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding)); 15636 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding));
15642 } 15637 }
15643 #endif // !defined(OS_IOS) 15638 #endif // !defined(OS_IOS)
15644 15639
15645 } // namespace net 15640 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698