Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/ref_counted.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/run_loop.h" | |
| 8 #include "content/browser/frame_host/navigation_request_info.h" | |
| 9 #include "content/browser/loader/navigation_url_loader_impl.h" | |
| 10 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 11 #include "content/browser/streams/stream.h" | |
| 12 #include "content/browser/streams/stream_context.h" | |
| 13 #include "content/browser/streams/stream_registry.h" | |
| 14 #include "content/browser/streams/stream_url_request_job.h" | |
| 15 #include "content/public/browser/browser_context.h" | |
| 16 #include "content/public/browser/resource_context.h" | |
| 17 #include "content/public/browser/stream_handle.h" | |
| 18 #include "content/public/common/resource_response.h" | |
| 19 #include "content/public/test/test_browser_context.h" | |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | |
| 21 #include "net/base/net_errors.h" | |
| 22 #include "net/http/http_response_headers.h" | |
| 23 #include "net/url_request/redirect_info.h" | |
| 24 #include "net/url_request/url_request.h" | |
| 25 #include "net/url_request/url_request_context.h" | |
| 26 #include "net/url_request/url_request_job_factory_impl.h" | |
| 27 #include "net/url_request/url_request_test_job.h" | |
| 28 #include "net/url_request/url_request_test_util.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | |
| 30 | |
| 31 namespace content { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 class StreamProtocolHandler | |
| 36 : public net::URLRequestJobFactory::ProtocolHandler { | |
| 37 public: | |
| 38 StreamProtocolHandler(StreamRegistry* registry) : registry_(registry) {} | |
| 39 | |
| 40 // net::URLRequestJobFactory::ProtocolHandler implementation. | |
| 41 virtual net::URLRequestJob* MaybeCreateJob( | |
| 42 net::URLRequest* request, | |
| 43 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
| 44 scoped_refptr<Stream> stream = registry_->GetStream(request->url()); | |
| 45 if (stream.get()) | |
| 46 return new StreamURLRequestJob(request, network_delegate, stream); | |
| 47 return NULL; | |
| 48 } | |
| 49 private: | |
| 50 StreamRegistry* registry_; | |
| 51 }; | |
| 52 | |
| 53 class TestNavigationURLLoaderDelegate : public NavigationURLLoader::Delegate { | |
| 54 public: | |
| 55 TestNavigationURLLoaderDelegate() | |
| 56 : net_error_(0), | |
| 57 request_redirected_(new base::RunLoop), | |
| 58 response_started_(new base::RunLoop), | |
| 59 request_failed_(new base::RunLoop) { | |
| 60 } | |
| 61 | |
| 62 const net::RedirectInfo& redirect_info() const { return redirect_info_; } | |
| 63 ResourceResponse* response() const { return response_.get(); } | |
| 64 StreamHandle* body() const { return body_.get(); } | |
| 65 int net_error() const { return net_error_; } | |
| 66 | |
| 67 void WaitForRequestRedirected() { | |
| 68 request_redirected_->Run(); | |
| 69 request_redirected_.reset(new base::RunLoop); | |
| 70 } | |
| 71 | |
| 72 void WaitForResponseStarted() { | |
| 73 response_started_->Run(); | |
| 74 response_started_.reset(new base::RunLoop); | |
| 75 } | |
| 76 | |
| 77 void WaitForRequestFailed() { | |
| 78 request_failed_->Run(); | |
| 79 request_failed_.reset(new base::RunLoop); | |
| 80 } | |
| 81 | |
| 82 // NavigationURLLoader::Delegate implementation. | |
| 83 virtual void OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
| 84 ResourceResponse* response) OVERRIDE { | |
| 85 redirect_info_ = redirect_info; | |
| 86 response_ = response; | |
| 87 request_redirected_->Quit(); | |
| 88 } | |
| 89 | |
| 90 virtual void OnResponseStarted(ResourceResponse* response, | |
| 91 scoped_ptr<StreamHandle> body) OVERRIDE { | |
| 92 response_ = response; | |
| 93 body_ = body.Pass(); | |
| 94 response_started_->Quit(); | |
| 95 } | |
| 96 | |
| 97 virtual void OnRequestFailed(int net_error) OVERRIDE { | |
| 98 net_error_ = net_error; | |
| 99 request_failed_->Quit(); | |
| 100 } | |
| 101 | |
| 102 private: | |
| 103 net::RedirectInfo redirect_info_; | |
| 104 scoped_refptr<ResourceResponse> response_; | |
| 105 scoped_ptr<StreamHandle> body_; | |
| 106 int net_error_; | |
| 107 | |
| 108 scoped_ptr<base::RunLoop> request_redirected_; | |
| 109 scoped_ptr<base::RunLoop> response_started_; | |
| 110 scoped_ptr<base::RunLoop> request_failed_; | |
| 111 }; | |
| 112 | |
| 113 } // namespace | |
| 114 | |
| 115 class NavigationURLLoaderTest : public testing::Test { | |
| 116 public: | |
| 117 NavigationURLLoaderTest() | |
| 118 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | |
| 119 browser_context_(new TestBrowserContext) { | |
| 120 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); | |
| 121 base::RunLoop().RunUntilIdle(); | |
| 122 net::URLRequestContext* request_context = | |
| 123 browser_context_->GetResourceContext()->GetRequestContext(); | |
| 124 // Attach URLRequestTestJob and make streams work. | |
| 125 job_factory_.SetProtocolHandler( | |
| 126 "test", net::URLRequestTestJob::CreateProtocolHandler()); | |
| 127 job_factory_.SetProtocolHandler( | |
| 128 "blob", new StreamProtocolHandler( | |
| 129 StreamContext::GetFor(browser_context_.get())->registry())); | |
| 130 request_context->set_job_factory(&job_factory_); | |
| 131 } | |
| 132 | |
| 133 // Helper function for fetching the body of a URL to a string. | |
| 134 std::string FetchURL(const GURL& url) { | |
| 135 net::TestDelegate delegate; | |
| 136 net::URLRequestContext* request_context = | |
| 137 browser_context_->GetResourceContext()->GetRequestContext(); | |
| 138 scoped_ptr<net::URLRequest> request(request_context->CreateRequest( | |
| 139 url, net::DEFAULT_PRIORITY, &delegate, NULL)); | |
| 140 request->Start(); | |
| 141 base::RunLoop().Run(); | |
| 142 | |
| 143 EXPECT_TRUE(request->status().is_success()); | |
| 144 EXPECT_EQ(200, request->response_headers()->response_code()); | |
| 145 return delegate.data_received(); | |
| 146 } | |
| 147 | |
| 148 protected: | |
| 149 TestBrowserThreadBundle thread_bundle_; | |
| 150 net::URLRequestJobFactoryImpl job_factory_; | |
| 151 scoped_ptr<TestBrowserContext> browser_context_; | |
| 152 ResourceDispatcherHostImpl host_; | |
| 153 }; | |
| 154 | |
| 155 // Tests that a basic request works. | |
| 156 TEST_F(NavigationURLLoaderTest, Basic) { | |
| 157 TestNavigationURLLoaderDelegate delegate; | |
| 158 | |
| 159 // Fake a top-level request. | |
| 160 FrameHostMsg_BeginNavigation_Params params; | |
|
clamy
2014/09/12 20:51:25
TestRenderFrameHost::SendBeginNavigationWithURL ha
davidben
2014/09/19 18:30:50
Hrm. Maybe, although layering-wise loader/ shouldn
clamy
2014/09/22 21:11:01
Acknowledged.
| |
| 161 params.method = "GET"; | |
| 162 params.url = net::URLRequestTestJob::test_url_1(); | |
| 163 NavigationRequestInfo request_info(params); | |
| 164 request_info.first_party_for_cookies = params.url; | |
| 165 request_info.is_main_frame = true; | |
| 166 | |
| 167 scoped_ptr<NavigationURLLoader> loader( | |
| 168 NavigationURLLoader::Create(browser_context_.get(), | |
| 169 0, request_info, NULL, &delegate)); | |
| 170 | |
| 171 // Wait for the response to come back. | |
| 172 delegate.WaitForResponseStarted(); | |
| 173 | |
| 174 // Check the response is correct. | |
| 175 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | |
| 176 EXPECT_EQ("HTTP/1.1 200 OK", | |
| 177 delegate.response()->head.headers->GetStatusLine()); | |
| 178 | |
| 179 // Check the body is correct. | |
| 180 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), | |
| 181 FetchURL(delegate.body()->GetURL())); | |
| 182 } | |
| 183 | |
| 184 // Tests that request failures are propogated correctly. | |
| 185 TEST_F(NavigationURLLoaderTest, RequestFailed) { | |
| 186 TestNavigationURLLoaderDelegate delegate; | |
| 187 | |
| 188 // Fake a top-level request. | |
| 189 FrameHostMsg_BeginNavigation_Params params; | |
| 190 params.method = "GET"; | |
| 191 params.url = GURL("bogus:bogus"); | |
| 192 NavigationRequestInfo request_info(params); | |
| 193 request_info.first_party_for_cookies = params.url; | |
| 194 request_info.is_main_frame = true; | |
| 195 | |
| 196 scoped_ptr<NavigationURLLoader> loader( | |
| 197 NavigationURLLoader::Create(browser_context_.get(), | |
| 198 0, request_info, NULL, &delegate)); | |
| 199 | |
| 200 // Wait for the request to fail as expected. | |
| 201 delegate.WaitForRequestFailed(); | |
| 202 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, delegate.net_error()); | |
| 203 } | |
| 204 | |
| 205 // Test that redirects are sent to the delegate. | |
| 206 TEST_F(NavigationURLLoaderTest, RequestRedirected) { | |
| 207 TestNavigationURLLoaderDelegate delegate; | |
| 208 | |
| 209 // Fake a top-level request. | |
| 210 FrameHostMsg_BeginNavigation_Params params; | |
| 211 params.method = "GET"; | |
| 212 params.url = net::URLRequestTestJob::test_url_redirect_to_url_2(); | |
| 213 NavigationRequestInfo request_info(params); | |
| 214 request_info.first_party_for_cookies = params.url; | |
| 215 request_info.is_main_frame = true; | |
| 216 | |
| 217 scoped_ptr<NavigationURLLoader> loader( | |
| 218 NavigationURLLoader::Create(browser_context_.get(), | |
| 219 0, request_info, NULL, &delegate)); | |
| 220 | |
| 221 // Wait for the request to redirect. | |
| 222 delegate.WaitForRequestRedirected(); | |
| 223 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | |
| 224 delegate.redirect_info().new_url); | |
| 225 EXPECT_EQ("GET", delegate.redirect_info().new_method); | |
| 226 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | |
| 227 delegate.redirect_info().new_first_party_for_cookies); | |
| 228 EXPECT_EQ("HTTP/1.1 302 MOVED", | |
| 229 delegate.response()->head.headers->GetStatusLine()); | |
| 230 | |
| 231 // Wait for the response to complete. | |
| 232 loader->FollowRedirect(); | |
| 233 base::RunLoop().RunUntilIdle(); | |
| 234 net::URLRequestTestJob::ProcessOnePendingMessage(); | |
| 235 delegate.WaitForResponseStarted(); | |
| 236 | |
| 237 // Check the response is correct. | |
| 238 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | |
| 239 EXPECT_EQ("HTTP/1.1 200 OK", | |
| 240 delegate.response()->head.headers->GetStatusLine()); | |
| 241 | |
| 242 // Check the body is correct. | |
| 243 EXPECT_EQ(net::URLRequestTestJob::test_data_2(), | |
| 244 FetchURL(delegate.body()->GetURL())); | |
| 245 } | |
| 246 | |
| 247 // Tests that the destroying the loader cancels the request. | |
| 248 TEST_F(NavigationURLLoaderTest, CancelOnDestruct) { | |
|
clamy
2014/09/12 20:51:25
Should we also add a test for the case where the l
davidben
2014/09/19 18:30:50
Added a couple of tests in this vein.
| |
| 249 TestNavigationURLLoaderDelegate delegate; | |
| 250 | |
| 251 // Fake a top-level request. | |
| 252 FrameHostMsg_BeginNavigation_Params params; | |
| 253 params.method = "GET"; | |
| 254 params.url = net::URLRequestTestJob::test_url_redirect_to_url_2(); | |
| 255 NavigationRequestInfo request_info(params); | |
| 256 request_info.first_party_for_cookies = params.url; | |
| 257 request_info.is_main_frame = true; | |
| 258 | |
| 259 scoped_ptr<NavigationURLLoader> loader( | |
| 260 NavigationURLLoader::Create(browser_context_.get(), | |
| 261 0, request_info, NULL, &delegate)); | |
| 262 | |
| 263 // Wait for the request to redirect. | |
| 264 delegate.WaitForRequestRedirected(); | |
| 265 | |
| 266 // Destroy the loader and verify that URLRequestTestJob no longer has anything | |
| 267 // paused. | |
| 268 loader.reset(); | |
| 269 base::RunLoop().RunUntilIdle(); | |
| 270 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); | |
| 271 } | |
| 272 | |
| 273 // Tests that the loader may be canceled by context. | |
| 274 TEST_F(NavigationURLLoaderTest, CancelByContext) { | |
| 275 TestNavigationURLLoaderDelegate delegate; | |
| 276 | |
| 277 // Fake a top-level request. | |
| 278 FrameHostMsg_BeginNavigation_Params params; | |
| 279 params.method = "GET"; | |
| 280 params.url = net::URLRequestTestJob::test_url_redirect_to_url_2(); | |
| 281 NavigationRequestInfo request_info(params); | |
| 282 request_info.first_party_for_cookies = params.url; | |
| 283 request_info.is_main_frame = true; | |
| 284 | |
| 285 scoped_ptr<NavigationURLLoader> loader( | |
| 286 NavigationURLLoader::Create(browser_context_.get(), | |
| 287 0, request_info, NULL, &delegate)); | |
| 288 | |
| 289 // Wait for the request to redirect. | |
| 290 delegate.WaitForRequestRedirected(); | |
| 291 | |
| 292 // Cancel all requests. | |
| 293 host_.CancelRequestsForContext(browser_context_->GetResourceContext()); | |
| 294 | |
| 295 // Wait for the request to now be aborted. | |
| 296 delegate.WaitForRequestFailed(); | |
| 297 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); | |
| 298 } | |
| 299 | |
| 300 } // namespace content | |
| OLD | NEW |