| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/public/renderer/resource_fetcher.h" | 5 #include "content/public/renderer/resource_fetcher.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 17 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
| 18 #include "content/public/renderer/render_view.h" | 18 #include "content/public/renderer/render_view.h" |
| 19 #include "content/public/test/content_browser_test.h" | 19 #include "content/public/test/content_browser_test.h" |
| 20 #include "content/public/test/content_browser_test_utils.h" | 20 #include "content/public/test/content_browser_test_utils.h" |
| 21 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
| 22 #include "content/shell/browser/shell.h" | 22 #include "content/shell/browser/shell.h" |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 23 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 24 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 24 #include "third_party/WebKit/public/web/WebFrame.h" | 25 #include "third_party/WebKit/public/web/WebFrame.h" |
| 25 #include "third_party/WebKit/public/web/WebView.h" | 26 #include "third_party/WebKit/public/web/WebView.h" |
| 26 | 27 |
| 27 using blink::WebFrame; | 28 using blink::WebFrame; |
| 28 using blink::WebURLRequest; | 29 using blink::WebURLRequest; |
| 29 using blink::WebURLResponse; | 30 using blink::WebURLResponse; |
| 30 | 31 |
| 31 namespace content { | 32 namespace content { |
| 32 | 33 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 fetcher->Start(frame, | 174 fetcher->Start(frame, |
| 174 WebURLRequest::RequestContextInternal, | 175 WebURLRequest::RequestContextInternal, |
| 175 WebURLRequest::FrameTypeNone, | 176 WebURLRequest::FrameTypeNone, |
| 176 ResourceFetcher::PLATFORM_LOADER, | 177 ResourceFetcher::PLATFORM_LOADER, |
| 177 delegate->NewCallback()); | 178 delegate->NewCallback()); |
| 178 | 179 |
| 179 delegate->WaitForResponse(); | 180 delegate->WaitForResponse(); |
| 180 | 181 |
| 181 ASSERT_TRUE(delegate->completed()); | 182 ASSERT_TRUE(delegate->completed()); |
| 182 EXPECT_EQ(delegate->response().httpStatusCode(), 404); | 183 EXPECT_EQ(delegate->response().httpStatusCode(), 404); |
| 183 EXPECT_TRUE(delegate->data().find("Not Found.") != std::string::npos); | |
| 184 } | 184 } |
| 185 | 185 |
| 186 void ResourceFetcherDidFailOnRenderer() { | 186 void ResourceFetcherDidFailOnRenderer() { |
| 187 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 187 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
| 188 | 188 |
| 189 // Try to fetch a page on a site that doesn't exist. | 189 // Try to fetch a page on a site that doesn't exist. |
| 190 GURL url("http://localhost:1339/doesnotexist"); | 190 GURL url("http://localhost:1339/doesnotexist"); |
| 191 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 191 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
| 192 scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 192 scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
| 193 fetcher->Start(frame, | 193 fetcher->Start(frame, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 | 288 |
| 289 int32 render_view_routing_id_; | 289 int32 render_view_routing_id_; |
| 290 }; | 290 }; |
| 291 | 291 |
| 292 // Test a fetch from the test server. | 292 // Test a fetch from the test server. |
| 293 // If this flakes, use http://crbug.com/51622. | 293 // If this flakes, use http://crbug.com/51622. |
| 294 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDownload) { | 294 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDownload) { |
| 295 // Need to spin up the renderer. | 295 // Need to spin up the renderer. |
| 296 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 296 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 297 | 297 |
| 298 ASSERT_TRUE(test_server()->Start()); | 298 ASSERT_TRUE(embedded_test_server()->Start()); |
| 299 GURL url(test_server()->GetURL("files/simple_page.html")); | 299 GURL url(embedded_test_server()->GetURL("/simple_page.html")); |
| 300 | 300 |
| 301 PostTaskToInProcessRendererAndWait( | 301 PostTaskToInProcessRendererAndWait( |
| 302 base::Bind(&ResourceFetcherTests::ResourceFetcherDownloadOnRenderer, | 302 base::Bind(&ResourceFetcherTests::ResourceFetcherDownloadOnRenderer, |
| 303 base::Unretained(this), url)); | 303 base::Unretained(this), url)); |
| 304 } | 304 } |
| 305 | 305 |
| 306 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcher404) { | 306 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcher404) { |
| 307 // Need to spin up the renderer. | 307 // Need to spin up the renderer. |
| 308 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 308 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 309 | 309 |
| 310 // Test 404 response. | 310 // Test 404 response. |
| 311 ASSERT_TRUE(test_server()->Start()); | 311 ASSERT_TRUE(embedded_test_server()->Start()); |
| 312 GURL url = test_server()->GetURL("files/thisfiledoesntexist.html"); | 312 GURL url = embedded_test_server()->GetURL("/thisfiledoesntexist.html"); |
| 313 | 313 |
| 314 PostTaskToInProcessRendererAndWait( | 314 PostTaskToInProcessRendererAndWait( |
| 315 base::Bind(&ResourceFetcherTests::ResourceFetcher404OnRenderer, | 315 base::Bind(&ResourceFetcherTests::ResourceFetcher404OnRenderer, |
| 316 base::Unretained(this), url)); | 316 base::Unretained(this), url)); |
| 317 } | 317 } |
| 318 | 318 |
| 319 // If this flakes, use http://crbug.com/51622. | 319 // If this flakes, use http://crbug.com/51622. |
| 320 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDidFail) { | 320 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDidFail) { |
| 321 // Need to spin up the renderer. | 321 // Need to spin up the renderer. |
| 322 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 322 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 323 | 323 |
| 324 PostTaskToInProcessRendererAndWait( | 324 PostTaskToInProcessRendererAndWait( |
| 325 base::Bind(&ResourceFetcherTests::ResourceFetcherDidFailOnRenderer, | 325 base::Bind(&ResourceFetcherTests::ResourceFetcherDidFailOnRenderer, |
| 326 base::Unretained(this))); | 326 base::Unretained(this))); |
| 327 } | 327 } |
| 328 | 328 |
| 329 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) { | 329 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) { |
| 330 // Need to spin up the renderer. | 330 // Need to spin up the renderer. |
| 331 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 331 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 332 | 332 |
| 333 // Grab a page that takes at least 1 sec to respond, but set the fetcher to | 333 // Grab a page that takes at least 1 sec to respond, but set the fetcher to |
| 334 // timeout in 0 sec. | 334 // timeout in 0 sec. |
| 335 ASSERT_TRUE(test_server()->Start()); | 335 ASSERT_TRUE(embedded_test_server()->Start()); |
| 336 GURL url(test_server()->GetURL("slow?1")); | 336 GURL url(embedded_test_server()->GetURL("/slow?1")); |
| 337 | 337 |
| 338 PostTaskToInProcessRendererAndWait( | 338 PostTaskToInProcessRendererAndWait( |
| 339 base::Bind(&ResourceFetcherTests::ResourceFetcherTimeoutOnRenderer, | 339 base::Bind(&ResourceFetcherTests::ResourceFetcherTimeoutOnRenderer, |
| 340 base::Unretained(this), url)); | 340 base::Unretained(this), url)); |
| 341 } | 341 } |
| 342 | 342 |
| 343 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) { | 343 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) { |
| 344 // Need to spin up the renderer. | 344 // Need to spin up the renderer. |
| 345 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 345 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 346 | 346 |
| 347 // Grab a page that takes at least 1 sec to respond, but set the fetcher to | 347 // Grab a page that takes at least 1 sec to respond, but set the fetcher to |
| 348 // timeout in 0 sec. | 348 // timeout in 0 sec. |
| 349 ASSERT_TRUE(test_server()->Start()); | 349 ASSERT_TRUE(embedded_test_server()->Start()); |
| 350 GURL url(test_server()->GetURL("slow?1")); | 350 GURL url(embedded_test_server()->GetURL("/slow?1")); |
| 351 | 351 |
| 352 PostTaskToInProcessRendererAndWait( | 352 PostTaskToInProcessRendererAndWait( |
| 353 base::Bind( | 353 base::Bind( |
| 354 &ResourceFetcherTests::ResourceFetcherDeletedInCallbackOnRenderer, | 354 &ResourceFetcherTests::ResourceFetcherDeletedInCallbackOnRenderer, |
| 355 base::Unretained(this), url)); | 355 base::Unretained(this), url)); |
| 356 } | 356 } |
| 357 | 357 |
| 358 | 358 |
| 359 | 359 |
| 360 // Test that ResourceFetchers can handle POSTs. | 360 // Test that ResourceFetchers can handle POSTs. |
| 361 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherPost) { | 361 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherPost) { |
| 362 // Need to spin up the renderer. | 362 // Need to spin up the renderer. |
| 363 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 363 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 364 | 364 |
| 365 // Grab a page that echos the POST body. | 365 // Grab a page that echos the POST body. |
| 366 ASSERT_TRUE(test_server()->Start()); | 366 ASSERT_TRUE(embedded_test_server()->Start()); |
| 367 GURL url(test_server()->GetURL("echo")); | 367 GURL url(embedded_test_server()->GetURL("/echo")); |
| 368 | 368 |
| 369 PostTaskToInProcessRendererAndWait( | 369 PostTaskToInProcessRendererAndWait( |
| 370 base::Bind( | 370 base::Bind( |
| 371 &ResourceFetcherTests::ResourceFetcherPost, | 371 &ResourceFetcherTests::ResourceFetcherPost, |
| 372 base::Unretained(this), url)); | 372 base::Unretained(this), url)); |
| 373 } | 373 } |
| 374 | 374 |
| 375 // Test that ResourceFetchers can set headers. | 375 // Test that ResourceFetchers can set headers. |
| 376 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherSetHeader) { | 376 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherSetHeader) { |
| 377 // Need to spin up the renderer. | 377 // Need to spin up the renderer. |
| 378 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 378 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 379 | 379 |
| 380 // Grab a page that echos the POST body. | 380 // Grab a page that echos the POST body. |
| 381 ASSERT_TRUE(test_server()->Start()); | 381 ASSERT_TRUE(embedded_test_server()->Start()); |
| 382 GURL url(test_server()->GetURL("echoheader?header")); | 382 GURL url(embedded_test_server()->GetURL("/echoheader?header")); |
| 383 | 383 |
| 384 PostTaskToInProcessRendererAndWait( | 384 PostTaskToInProcessRendererAndWait( |
| 385 base::Bind( | 385 base::Bind( |
| 386 &ResourceFetcherTests::ResourceFetcherSetHeader, | 386 &ResourceFetcherTests::ResourceFetcherSetHeader, |
| 387 base::Unretained(this), url)); | 387 base::Unretained(this), url)); |
| 388 } | 388 } |
| 389 | 389 |
| 390 } // namespace content | 390 } // namespace content |
| OLD | NEW |