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

Side by Side 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, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "components/ntp_snippets/breaking_news/subscription_json_request.h"
6
7 #include <set>
fhorschig 2017/06/01 09:23:56 unused
mamir 2017/06/01 14:52:32 Acknowledged.
8 #include <utility>
fhorschig 2017/06/01 09:23:56 unused?
mamir 2017/06/01 14:52:32 Acknowledged.
9
10 #include "base/json/json_reader.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/test/test_mock_time_task_runner.h"
13 #include "base/values.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace ntp_snippets {
20
21 namespace internal {
22
23 namespace {
24
25 using testing::StrEq;
26
27 MATCHER_P(EqualsJSON, json, "equals JSON") {
28 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json);
29 if (!expected) {
30 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON";
31 return false;
32 }
33
34 std::string err_msg;
35 int err_line, err_col;
36 std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError(
37 arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col);
38 if (!actual) {
39 *result_listener << "input:" << err_line << ":" << err_col << ": "
40 << "parse error: " << err_msg;
41 return false;
42 }
43 return base::Value::Equals(actual.get(), expected.get());
44 }
45
46 } // namespace
47
48 class SubscriptionJsonRequestTest : public testing::Test {
49 public:
50 SubscriptionJsonRequestTest()
51 : mock_task_runner_(new base::TestMockTimeTaskRunner()),
52 request_context_getter_(
53 new net::TestURLRequestContextGetter(mock_task_runner_.get())) {}
fhorschig 2017/06/01 09:23:56 A message loop in a test should be easier: https:/
mamir 2017/06/01 14:52:32 Done.
54
55 SubscriptionJsonRequest::Builder CreateMinimalBuilder() {
56 SubscriptionJsonRequest::Builder builder;
57 builder.SetUrl(GURL("http://valid-url.test"))
58 .SetUrlRequestContextGetter(request_context_getter_.get());
59 return builder;
60 }
61
62 private:
63 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_;
64 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
65 net::TestURLFetcherFactory fetcher_factory_;
fhorschig 2017/06/01 09:23:56 It would be nice if you used it. e.g. Test what ha
mamir 2017/06/01 14:52:32 Done.
66
67 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequestTest);
68 };
69
70 TEST_F(SubscriptionJsonRequestTest, BuildRequest) {
71 SubscriptionJsonRequest::Builder builder = CreateMinimalBuilder();
72 std::string token = "1234567890";
73 builder.SetToken(token);
74
75 EXPECT_THAT(builder.PreviewRequestHeadersForTesting(),
76 StrEq("Content-Type: application/json; charset=UTF-8\r\n"
77 "\r\n"));
78 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
79 EqualsJSON("{"
80 " \"token\": "
81 " \"1234567890\""
82 "}"));
83 }
84
85 } // namespace internal
86
87 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698