1 // Copyright 2015 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.
5 #include "content/browser/loader/async_resource_handler.h"
9 #include "base/format_macros.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/test/browser_test_utils.h"
14 #include "content/public/test/content_browser_test.h"
15 #include "content/public/test/content_browser_test_utils.h"
16 #include "content/shell/browser/shell.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
18 #include "net/test/embedded_test_server/http_request.h"
19 #include "net/test/embedded_test_server/http_response.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 const char kPostPath
[] = "/post";
27 const char kRedirectPostPath
[] = "/redirect";
29 // ThreadSanitizer is too slow to perform the full upload, so tests
30 // using that build get an easier test which might not show two distinct
31 // progress events. See crbug.com/526985.
32 #if defined(THREAD_SANITIZER)
33 const size_t kPayloadSize
= 1062882; // 2*3^12
35 const size_t kPayloadSize
= 28697814; // 2*3^15
38 scoped_ptr
<net::test_server::HttpResponse
> HandlePostAndRedirectURLs(
39 const std::string
& request_path
,
40 const net::test_server::HttpRequest
& request
) {
41 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
42 new net::test_server::BasicHttpResponse());
43 if (base::StartsWith(request
.relative_url
, kRedirectPostPath
,
44 base::CompareCase::SENSITIVE
)) {
45 http_response
->set_code(net::HTTP_TEMPORARY_REDIRECT
);
46 http_response
->AddCustomHeader("Location", kPostPath
);
47 EXPECT_EQ(request
.content
.length(), kPayloadSize
);;
48 return http_response
.Pass();
49 } else if(base::StartsWith(request
.relative_url
, kPostPath
,
50 base::CompareCase::SENSITIVE
)) {
51 http_response
->set_content("hello");
52 http_response
->set_content_type("text/plain");
53 EXPECT_EQ(request
.content
.length(), kPayloadSize
);
54 return http_response
.Pass();
56 return scoped_ptr
<net::test_server::HttpResponse
>();
62 class AsyncResourceHandlerBrowserTest
: public ContentBrowserTest
{
65 IN_PROC_BROWSER_TEST_F(AsyncResourceHandlerBrowserTest
, UploadProgress
) {
66 net::test_server::EmbeddedTestServer
* test_server
= embedded_test_server();
67 ASSERT_TRUE(test_server
->InitializeAndWaitUntilReady());
68 test_server
->RegisterRequestHandler(
69 base::Bind(&HandlePostAndRedirectURLs
, kPostPath
));
71 NavigateToURL(shell(),
72 test_server
->GetURL("/loader/async_resource_handler.html"));
74 std::string js_result
;
75 EXPECT_TRUE(ExecuteScriptAndExtractString(
76 shell()->web_contents(),
77 base::StringPrintf("WaitForAsyncXHR('%s', %" PRIuS
")",
81 EXPECT_EQ(js_result
, "success");
84 IN_PROC_BROWSER_TEST_F(AsyncResourceHandlerBrowserTest
,
85 UploadProgressRedirect
) {
86 net::test_server::EmbeddedTestServer
* test_server
= embedded_test_server();
87 ASSERT_TRUE(test_server
->InitializeAndWaitUntilReady());
88 test_server
->RegisterRequestHandler(
89 base::Bind(&HandlePostAndRedirectURLs
, kRedirectPostPath
));
91 NavigateToURL(shell(),
92 test_server
->GetURL("/loader/async_resource_handler.html"));
94 std::string js_result
;
95 EXPECT_TRUE(ExecuteScriptAndExtractString(
96 shell()->web_contents(),
97 base::StringPrintf("WaitForAsyncXHR('%s', %" PRIuS
")",
101 EXPECT_EQ(js_result
, "success");
104 } // namespace content