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