Chromium Code Reviews| Index: net/url_request/url_fetcher_impl_unittest.cc |
| diff --git a/net/url_request/url_fetcher_impl_unittest.cc b/net/url_request/url_fetcher_impl_unittest.cc |
| index fce7ace52848e518e0e93238d6abf4d5b8e92344..abc1518d7277310a38202a2e7d9b04c58d9c77b7 100644 |
| --- a/net/url_request/url_fetcher_impl_unittest.cc |
| +++ b/net/url_request/url_fetcher_impl_unittest.cc |
| @@ -17,7 +17,11 @@ |
| #include "base/threading/thread.h" |
| #include "build/build_config.h" |
| #include "crypto/nss_util.h" |
| +#include "net/base/elements_upload_data_stream.h" |
| #include "net/base/network_change_notifier.h" |
| +#include "net/base/upload_bytes_element_reader.h" |
| +#include "net/base/upload_element_reader.h" |
| +#include "net/base/upload_file_element_reader.h" |
| #include "net/dns/mock_host_resolver.h" |
| #include "net/http/http_response_headers.h" |
| #include "net/test/spawned_test_server/spawned_test_server.h" |
| @@ -45,6 +49,16 @@ const base::FilePath::CharType kDocRoot[] = |
| FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); |
| const char kTestServerFilePrefix[] = "files/"; |
| +scoped_ptr<UploadDataStream> CreateUploadStream(size_t* call_count) { |
| + ++*call_count; |
| + const std::string str("bobsyeruncle\n"); |
| + std::vector<char> buffer(str.begin(), str.end()); |
| + return ElementsUploadDataStream::CreateWithReader( |
| + scoped_ptr<UploadElementReader>( |
| + new UploadOwnedBytesElementReader(&buffer)), |
| + 0); |
| +} |
| + |
| class ThrottlingTestURLRequestContext : public TestURLRequestContext { |
| public: |
| ThrottlingTestURLRequestContext() : TestURLRequestContext(true) { |
| @@ -81,7 +95,7 @@ class ThrottlingTestURLRequestContextGetter |
| class URLFetcherTest : public testing::Test, |
| public URLFetcherDelegate { |
| public: |
| - URLFetcherTest() : fetcher_(NULL) {} |
| + URLFetcherTest() : fetcher_(NULL), expected_status_code_(200) {} |
| static int GetNumFetcherCores() { |
| return URLFetcherImpl::GetNumFetcherCores(); |
| @@ -135,6 +149,7 @@ class URLFetcherTest : public testing::Test, |
| URLFetcherImpl* fetcher_; |
| scoped_ptr<TestURLRequestContext> context_; |
| + int expected_status_code_; |
| }; |
| // A test fixture that uses a MockHostResolver, so that name resolutions can |
| @@ -166,7 +181,7 @@ void URLFetcherTest::CreateFetcher(const GURL& url) { |
| void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { |
| EXPECT_TRUE(source->GetStatus().is_success()); |
| - EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
| + EXPECT_EQ(expected_status_code_, source->GetResponseCode()); // HTTP OK |
| std::string data; |
| EXPECT_TRUE(source->GetResponseAsString(&data)); |
| @@ -259,6 +274,21 @@ class URLFetcherPostFileTest : public URLFetcherTest { |
| uint64 range_length_; |
| }; |
| +class URLFetcherPostStreamTest : public URLFetcherTest { |
| + public: |
| + URLFetcherPostStreamTest() : create_stream_count_(0) {} |
| + |
| + // URLFetcherTest: |
| + void CreateFetcher(const GURL& url) override; |
| + |
| + // URLFetcherDelegate: |
| + void OnURLFetchComplete(const URLFetcher* source) override; |
| + |
| + protected: |
| + // Count of calling CreateStream. |
| + size_t create_stream_count_; |
| +}; |
| + |
| // Version of URLFetcherTest that does a POST instead with empty upload body |
| class URLFetcherEmptyPostTest : public URLFetcherTest { |
| public: |
| @@ -571,6 +601,24 @@ void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { |
| URLFetcherTest::OnURLFetchComplete(source); |
| } |
| +void URLFetcherPostStreamTest::CreateFetcher(const GURL& url) { |
| + fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| + fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| + io_message_loop_proxy().get(), request_context())); |
| + fetcher_->SetUploadStreamFactory( |
| + "text/plain", base::Bind(&CreateUploadStream, &create_stream_count_)); |
| + fetcher_->SetAutomaticallyRetryOn5xx(true); |
| + fetcher_->SetMaxRetriesOn5xx(1); |
| + fetcher_->Start(); |
| +} |
| + |
| +void URLFetcherPostStreamTest::OnURLFetchComplete(const URLFetcher* source) { |
| + std::string data; |
| + EXPECT_TRUE(source->GetResponseAsString(&data)); |
| + EXPECT_EQ(std::string("bobsyeruncle\n"), data); |
|
mmenke
2015/01/15 15:38:54
nit: set::string isn't needed here.
hirono
2015/01/16 03:55:58
Done.
|
| + URLFetcherTest::OnURLFetchComplete(source); |
| +} |
| + |
| void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
| fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| @@ -1085,6 +1133,28 @@ TEST_F(URLFetcherPostFileTest, Range) { |
| base::MessageLoop::current()->Run(); |
| } |
| +TEST_F(URLFetcherPostStreamTest, Basic) { |
| + SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| + SpawnedTestServer::kLocalhost, |
| + base::FilePath(kDocRoot)); |
| + ASSERT_TRUE(test_server.Start()); |
| + |
| + CreateFetcher(test_server.GetURL("echo")); |
| + base::MessageLoop::current()->Run(); |
| + ASSERT_EQ(1u, create_stream_count_); |
| +} |
| + |
| +TEST_F(URLFetcherPostStreamTest, Retry) { |
| + SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| + SpawnedTestServer::kLocalhost, |
| + base::FilePath(kDocRoot)); |
| + ASSERT_TRUE(test_server.Start()); |
| + expected_status_code_ = 500; |
| + CreateFetcher(test_server.GetURL("echo?status=500")); |
| + base::MessageLoop::current()->Run(); |
| + ASSERT_EQ(2u, create_stream_count_); |
| +} |
| + |
| TEST_F(URLFetcherEmptyPostTest, Basic) { |
| SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| SpawnedTestServer::kLocalhost, |