Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Unified Diff: net/url_request/url_fetcher_impl_unittest.cc

Issue 863253002: Update from https://crrev.com/312600 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..faea357e5d60dcdc81378daf688f6da4f2e62e5b 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"
@@ -81,7 +85,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 +139,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 +171,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 +264,34 @@ class URLFetcherPostFileTest : public URLFetcherTest {
uint64 range_length_;
};
+class URLFetcherSetUploadFactoryTest : public URLFetcherTest {
+ public:
+ URLFetcherSetUploadFactoryTest() : create_stream_count_(0) {}
+
+ // URLFetcherTest:
+ void CreateFetcher(const GURL& url) override;
+
+ // URLFetcherDelegate:
+ void OnURLFetchComplete(const URLFetcher* source) override;
+
+ // Callback passed to URLFetcher to create upload stream.
+ scoped_ptr<UploadDataStream> CreateUploadStream() {
+ ++create_stream_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);
+ }
+
+ size_t create_stream_count() const { return create_stream_count_; }
+
+ private:
+ // 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 +604,27 @@ void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) {
URLFetcherTest::OnURLFetchComplete(source);
}
+void URLFetcherSetUploadFactoryTest::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(&URLFetcherSetUploadFactoryTest::CreateUploadStream,
+ base::Unretained(this)));
+ fetcher_->SetAutomaticallyRetryOn5xx(true);
+ fetcher_->SetMaxRetriesOn5xx(1);
+ fetcher_->Start();
+}
+
+void URLFetcherSetUploadFactoryTest::OnURLFetchComplete(
+ const URLFetcher* source) {
+ std::string data;
+ EXPECT_TRUE(source->GetResponseAsString(&data));
+ EXPECT_EQ("bobsyeruncle\n", data);
+ URLFetcherTest::OnURLFetchComplete(source);
+}
+
void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
fetcher_->SetRequestContext(new TestURLRequestContextGetter(
@@ -1085,6 +1139,28 @@ TEST_F(URLFetcherPostFileTest, Range) {
base::MessageLoop::current()->Run();
}
+TEST_F(URLFetcherSetUploadFactoryTest, 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(URLFetcherSetUploadFactoryTest, 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,

Powered by Google App Engine
This is Rietveld 408576698