OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 5776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5787 | 5787 |
5788 TestOldCompletionCallback callback; | 5788 TestOldCompletionCallback callback; |
5789 | 5789 |
5790 int rv = trans->Start(&request, &callback, BoundNetLog()); | 5790 int rv = trans->Start(&request, &callback, BoundNetLog()); |
5791 EXPECT_EQ(ERR_IO_PENDING, rv); | 5791 EXPECT_EQ(ERR_IO_PENDING, rv); |
5792 | 5792 |
5793 rv = callback.WaitForResult(); | 5793 rv = callback.WaitForResult(); |
5794 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv); | 5794 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv); |
5795 } | 5795 } |
5796 | 5796 |
5797 // Host resolution observer used by | |
5798 // HttpNetworkTransactionTest.ResolveMadeWithReferrer to check that host | |
5799 // resovle requests are issued with a referrer of |expected_referrer|. | |
5800 class ResolutionReferrerObserver : public HostResolver::Observer { | |
5801 public: | |
5802 explicit ResolutionReferrerObserver(const GURL& expected_referrer) | |
5803 : expected_referrer_(expected_referrer), | |
5804 called_start_with_referrer_(false), | |
5805 called_finish_with_referrer_(false) { | |
5806 } | |
5807 | |
5808 virtual void OnStartResolution(int id, | |
5809 const HostResolver::RequestInfo& info) { | |
5810 if (info.referrer() == expected_referrer_) | |
5811 called_start_with_referrer_ = true; | |
5812 } | |
5813 | |
5814 virtual void OnFinishResolutionWithStatus( | |
5815 int id, bool was_resolved, const HostResolver::RequestInfo& info ) { | |
5816 if (info.referrer() == expected_referrer_) | |
5817 called_finish_with_referrer_ = true; | |
5818 } | |
5819 | |
5820 virtual void OnCancelResolution(int id, | |
5821 const HostResolver::RequestInfo& info ) { | |
5822 FAIL() << "Should not be cancelling any requests!"; | |
5823 } | |
5824 | |
5825 bool did_complete_with_expected_referrer() const { | |
5826 return called_start_with_referrer_ && called_finish_with_referrer_; | |
5827 } | |
5828 | |
5829 private: | |
5830 GURL expected_referrer_; | |
5831 bool called_start_with_referrer_; | |
5832 bool called_finish_with_referrer_; | |
5833 | |
5834 DISALLOW_COPY_AND_ASSIGN(ResolutionReferrerObserver); | |
5835 }; | |
5836 | |
5837 // Make sure that when HostResolver::Resolve() is invoked, it passes through | |
5838 // the "referrer". This is depended on by the DNS prefetch observer. | |
5839 TEST_F(HttpNetworkTransactionTest, ResolveMadeWithReferrer) { | |
5840 GURL referrer = GURL("http://expected-referrer/"); | |
5841 EXPECT_TRUE(referrer.is_valid()); | |
5842 ResolutionReferrerObserver resolution_observer(referrer); | |
5843 | |
5844 // Issue a request, containing an HTTP referrer. | |
5845 HttpRequestInfo request; | |
5846 request.method = "GET"; | |
5847 request.url = GURL("http://www.google.com/"); | |
5848 request.extra_headers.SetHeader(HttpRequestHeaders::kReferer, | |
5849 referrer.spec()); | |
5850 | |
5851 SessionDependencies session_deps; | |
5852 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( | |
5853 CreateSession(&session_deps))); | |
5854 | |
5855 // Attach an observer to watch the host resolutions being made. | |
5856 session_deps.host_resolver->AddObserver(&resolution_observer); | |
5857 | |
5858 // Connect up a mock socket which will fail when reading. | |
5859 MockRead data_reads[] = { | |
5860 MockRead(false, ERR_FAILED), | |
5861 }; | |
5862 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
5863 session_deps.socket_factory.AddSocketDataProvider(&data); | |
5864 | |
5865 // Run the request until it fails reading from the socket. | |
5866 TestOldCompletionCallback callback; | |
5867 int rv = trans->Start(&request, &callback, BoundNetLog()); | |
5868 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5869 rv = callback.WaitForResult(); | |
5870 EXPECT_EQ(ERR_FAILED, rv); | |
5871 | |
5872 // Check that the host resolution observer saw |referrer|. | |
5873 EXPECT_TRUE(resolution_observer.did_complete_with_expected_referrer()); | |
5874 } | |
5875 | |
5876 // Base test to make sure that when the load flags for a request specify to | 5797 // Base test to make sure that when the load flags for a request specify to |
5877 // bypass the cache, the DNS cache is not used. | 5798 // bypass the cache, the DNS cache is not used. |
5878 void BypassHostCacheOnRefreshHelper(int load_flags) { | 5799 void BypassHostCacheOnRefreshHelper(int load_flags) { |
5879 // Issue a request, asking to bypass the cache(s). | 5800 // Issue a request, asking to bypass the cache(s). |
5880 HttpRequestInfo request; | 5801 HttpRequestInfo request; |
5881 request.method = "GET"; | 5802 request.method = "GET"; |
5882 request.load_flags = load_flags; | 5803 request.load_flags = load_flags; |
5883 request.url = GURL("http://www.google.com/"); | 5804 request.url = GURL("http://www.google.com/"); |
5884 | 5805 |
5885 SessionDependencies session_deps; | 5806 SessionDependencies session_deps; |
(...skipping 3246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9132 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); | 9053 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); |
9133 if (rv == OK && info.host_port_pair().Equals(host_port_)) | 9054 if (rv == OK && info.host_port_pair().Equals(host_port_)) |
9134 host_resolver_.GetHostCache()->clear(); | 9055 host_resolver_.GetHostCache()->clear(); |
9135 return rv; | 9056 return rv; |
9136 } | 9057 } |
9137 | 9058 |
9138 virtual void CancelRequest(RequestHandle req) OVERRIDE { | 9059 virtual void CancelRequest(RequestHandle req) OVERRIDE { |
9139 host_resolver_.CancelRequest(req); | 9060 host_resolver_.CancelRequest(req); |
9140 } | 9061 } |
9141 | 9062 |
9142 virtual void AddObserver(Observer* observer) OVERRIDE { | |
9143 return host_resolver_.AddObserver(observer); | |
9144 } | |
9145 | |
9146 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
9147 return host_resolver_.RemoveObserver(observer); | |
9148 } | |
9149 | |
9150 MockCachingHostResolver* GetMockHostResolver() { | 9063 MockCachingHostResolver* GetMockHostResolver() { |
9151 return &host_resolver_; | 9064 return &host_resolver_; |
9152 } | 9065 } |
9153 | 9066 |
9154 private: | 9067 private: |
9155 MockCachingHostResolver host_resolver_; | 9068 MockCachingHostResolver host_resolver_; |
9156 const HostPortPair host_port_; | 9069 const HostPortPair host_port_; |
9157 }; | 9070 }; |
9158 | 9071 |
9159 TEST_F(HttpNetworkTransactionTest, | 9072 TEST_F(HttpNetworkTransactionTest, |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9309 StaticSocketDataProvider* data[] = { &data1, &data2 }; | 9222 StaticSocketDataProvider* data[] = { &data1, &data2 }; |
9310 | 9223 |
9311 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); | 9224 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
9312 | 9225 |
9313 EXPECT_EQ(OK, out.rv); | 9226 EXPECT_EQ(OK, out.rv); |
9314 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | 9227 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
9315 EXPECT_EQ("hello world", out.response_data); | 9228 EXPECT_EQ("hello world", out.response_data); |
9316 } | 9229 } |
9317 | 9230 |
9318 } // namespace net | 9231 } // namespace net |
OLD | NEW |