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

Unified Diff: components/ntp_snippets/breaking_news/subscription_json_request_unittest.cc

Issue 2918513002: [NTP::Push] Add the classes for sending a breaking news subscription request (Closed)
Patch Set: bauerb@ comments. 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/ntp_snippets/breaking_news/subscription_json_request_unittest.cc
diff --git a/components/ntp_snippets/breaking_news/subscription_json_request_unittest.cc b/components/ntp_snippets/breaking_news/subscription_json_request_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b017f6e472be37a675b5a720d92c9edd0db9db23
--- /dev/null
+++ b/components/ntp_snippets/breaking_news/subscription_json_request_unittest.cc
@@ -0,0 +1,102 @@
+// 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/ntp_snippets/breaking_news/subscription_json_request.h"
+
+#include <set>
+#include <utility>
+
+#include "base/json/json_reader.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/stringprintf.h"
+#include "base/test/test_mock_time_task_runner.h"
+#include "base/time/tick_clock.h"
+#include "base/time/time.h"
+#include "base/values.h"
+#include "components/ntp_snippets/breaking_news/subscription_request_params.h"
+#include "components/ntp_snippets/features.h"
+#include "components/ntp_snippets/ntp_snippets_constants.h"
+#include "components/prefs/testing_pref_service.h"
+#include "components/variations/variations_params_manager.h"
+#include "net/url_request/test_url_fetcher_factory.h"
+#include "net/url_request/url_request_test_util.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ntp_snippets {
+
+namespace internal {
+
+namespace {
+
+using testing::_;
+using testing::Eq;
+using testing::Not;
+using testing::NotNull;
+using testing::StrEq;
+
+MATCHER_P(EqualsJSON, json, "equals JSON") {
+ std::unique_ptr<base::Value> expected = base::JSONReader::Read(json);
+ if (!expected) {
+ *result_listener << "INTERNAL ERROR: couldn't parse expected JSON";
+ return false;
+ }
+
+ std::string err_msg;
+ int err_line, err_col;
+ std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError(
+ arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col);
+ if (!actual) {
+ *result_listener << "input:" << err_line << ":" << err_col << ": "
+ << "parse error: " << err_msg;
+ return false;
+ }
+ return base::Value::Equals(actual.get(), expected.get());
+}
+
+} // namespace
+
+class SubscriptionJsonRequestTest : public testing::Test {
+ public:
+ SubscriptionJsonRequestTest()
+ : pref_service_(base::MakeUnique<TestingPrefServiceSimple>()),
+ mock_task_runner_(new base::TestMockTimeTaskRunner()),
+ request_context_getter_(
+ new net::TestURLRequestContextGetter(mock_task_runner_.get())) {}
+
+ SubscriptionJsonRequest::Builder CreateMinimalBuilder() {
+ SubscriptionJsonRequest::Builder builder;
+ builder.SetUrl(GURL("http://valid-url.test"))
+ .SetUrlRequestContextGetter(request_context_getter_.get());
+ return builder;
Bernhard Bauer 2017/05/31 14:45:40 Why not inline these calls? :)
mamir 2017/05/31 19:00:54 because request_context_getter_ is private. No?
fhorschig 2017/06/01 09:23:55 If you write a getter ... GetRequestContext() {
Bernhard Bauer 2017/06/01 09:31:23 I meant `return SubscriptionJsonRequest::Builder()
mamir 2017/06/01 14:52:30 Done.
+ }
+
+ private:
+ std::unique_ptr<TestingPrefServiceSimple> pref_service_;
+ scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_;
+ scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
+ net::TestURLFetcherFactory fetcher_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequestTest);
+};
+
+TEST_F(SubscriptionJsonRequestTest, BuildRequest) {
+ SubscriptionJsonRequest::Builder builder = CreateMinimalBuilder();
+ SubscriptionRequestParams params;
+ params.token = "1234567890";
+ builder.SetParams(params).SetUrl(GURL("http://valid-url.test")).Build();
Bernhard Bauer 2017/05/31 14:45:40 Do you actually need to call Build() here?
fhorschig 2017/05/31 14:51:14 I don't think so, too
mamir 2017/05/31 19:00:54 Done.
+
+ EXPECT_THAT(builder.PreviewRequestHeadersForTesting(),
+ StrEq("Content-Type: application/json; charset=UTF-8\r\n"
+ "\r\n"));
+ EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
+ EqualsJSON("{"
+ " \"token\": "
+ " \"1234567890\""
+ "}"));
+}
+
+} // namespace internal
+
+} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698