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

Unified Diff: components/offline_pages/core/prefetch/get_operation_request_unittest.cc

Issue 2889453003: [Offline Prefetech] Send GetOperationReqest to the server (Closed)
Patch Set: Rebase Created 3 years, 7 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: components/offline_pages/core/prefetch/get_operation_request_unittest.cc
diff --git a/components/offline_pages/core/prefetch/get_operation_request_unittest.cc b/components/offline_pages/core/prefetch/get_operation_request_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e0535d9aeeef76f38ff9443d99714fe7f7616155
--- /dev/null
+++ b/components/offline_pages/core/prefetch/get_operation_request_unittest.cc
@@ -0,0 +1,83 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/offline_pages/core/prefetch/get_operation_request.h"
+
+#include "base/test/mock_callback.h"
+#include "components/offline_pages/core/prefetch/prefetch_request_test_base.h"
+#include "components/offline_pages/core/prefetch/prefetch_types.h"
+#include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h"
+#include "net/http/http_status_code.h"
+#include "net/url_request/url_request_status.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "url/gurl.h"
+
+using testing::_;
+using testing::DoAll;
+using testing::Eq;
+using testing::SaveArg;
+
+namespace offline_pages {
+
+namespace {
+const char kTestMethodName[] = "Test name";
+} // namespace
+
+// All tests cases here only validate the request data and check for general
+// http response. The tests for the Operation proto data returned in the http
+// response are covered in PrefetchRequestOperationResponseTest.
+class GetOperationRequestTest : public PrefetchRequestTestBase {
+ public:
+ std::unique_ptr<GetOperationRequest> CreateRequest(
+ const PrefetchRequestFinishedCallback& callback) {
+ return std::unique_ptr<GetOperationRequest>(
+ new GetOperationRequest(kTestMethodName, request_context(), callback));
+ }
+};
+
+TEST_F(GetOperationRequestTest, RequestData) {
+ base::MockCallback<PrefetchRequestFinishedCallback> callback;
+ std::unique_ptr<GetOperationRequest> request(CreateRequest(callback.Get()));
+
+ net::TestURLFetcher* fetcher = GetRunningFetcher();
+ net::HttpRequestHeaders headers;
+ fetcher->GetExtraRequestHeaders(&headers);
+ std::string content_type_header;
+ headers.GetHeader(net::HttpRequestHeaders::kContentType,
+ &content_type_header);
+ EXPECT_EQ("application/x-protobuf", content_type_header);
+
+ EXPECT_TRUE(fetcher->upload_content_type().empty());
+ EXPECT_TRUE(fetcher->upload_data().empty());
+}
+
+TEST_F(GetOperationRequestTest, EmptyResponse) {
+ base::MockCallback<PrefetchRequestFinishedCallback> callback;
+ std::unique_ptr<GetOperationRequest> request(CreateRequest(callback.Get()));
+
+ PrefetchRequestStatus status;
+ std::vector<RenderPageInfo> pages;
+ EXPECT_CALL(callback, Run(_, _))
+ .WillOnce(DoAll(SaveArg<0>(&status), SaveArg<1>(&pages)));
+ RespondWithData("");
+
+ EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, status);
+ EXPECT_TRUE(pages.empty());
+}
+
+TEST_F(GetOperationRequestTest, InvalidResponse) {
+ base::MockCallback<PrefetchRequestFinishedCallback> callback;
+ std::unique_ptr<GetOperationRequest> request(CreateRequest(callback.Get()));
+
+ PrefetchRequestStatus status;
+ std::vector<RenderPageInfo> pages;
+ EXPECT_CALL(callback, Run(_, _))
+ .WillOnce(DoAll(SaveArg<0>(&status), SaveArg<1>(&pages)));
+ RespondWithData("Some invalid data");
+
+ EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, status);
+ EXPECT_TRUE(pages.empty());
+}
+
+} // namespace offline_pages

Powered by Google App Engine
This is Rietveld 408576698