Chromium Code Reviews| OLD | NEW |
|---|---|
| (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> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "base/test/test_mock_time_task_runner.h" | |
| 14 #include "base/time/tick_clock.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "base/values.h" | |
| 17 #include "components/ntp_snippets/breaking_news/subscription_request_params.h" | |
| 18 #include "components/ntp_snippets/features.h" | |
| 19 #include "components/ntp_snippets/ntp_snippets_constants.h" | |
| 20 #include "components/prefs/testing_pref_service.h" | |
| 21 #include "components/variations/variations_params_manager.h" | |
| 22 #include "net/url_request/test_url_fetcher_factory.h" | |
| 23 #include "net/url_request/url_request_test_util.h" | |
| 24 #include "testing/gmock/include/gmock/gmock.h" | |
| 25 #include "testing/gtest/include/gtest/gtest.h" | |
| 26 | |
| 27 namespace ntp_snippets { | |
| 28 | |
| 29 namespace internal { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 using testing::_; | |
| 34 using testing::Eq; | |
| 35 using testing::Not; | |
| 36 using testing::NotNull; | |
| 37 using testing::StrEq; | |
| 38 | |
| 39 MATCHER_P(EqualsJSON, json, "equals JSON") { | |
| 40 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json); | |
| 41 if (!expected) { | |
| 42 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON"; | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 std::string err_msg; | |
| 47 int err_line, err_col; | |
| 48 std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError( | |
| 49 arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col); | |
| 50 if (!actual) { | |
| 51 *result_listener << "input:" << err_line << ":" << err_col << ": " | |
| 52 << "parse error: " << err_msg; | |
| 53 return false; | |
| 54 } | |
| 55 return base::Value::Equals(actual.get(), expected.get()); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 class SubscriptionJsonRequestTest : public testing::Test { | |
| 61 public: | |
| 62 SubscriptionJsonRequestTest() | |
| 63 : pref_service_(base::MakeUnique<TestingPrefServiceSimple>()), | |
| 64 mock_task_runner_(new base::TestMockTimeTaskRunner()), | |
| 65 request_context_getter_( | |
| 66 new net::TestURLRequestContextGetter(mock_task_runner_.get())) {} | |
| 67 | |
| 68 SubscriptionJsonRequest::Builder CreateMinimalBuilder() { | |
| 69 SubscriptionJsonRequest::Builder builder; | |
| 70 builder.SetUrl(GURL("http://valid-url.test")) | |
| 71 .SetUrlRequestContextGetter(request_context_getter_.get()); | |
| 72 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.
| |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | |
| 77 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; | |
| 78 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | |
| 79 net::TestURLFetcherFactory fetcher_factory_; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequestTest); | |
| 82 }; | |
| 83 | |
| 84 TEST_F(SubscriptionJsonRequestTest, BuildRequest) { | |
| 85 SubscriptionJsonRequest::Builder builder = CreateMinimalBuilder(); | |
| 86 SubscriptionRequestParams params; | |
| 87 params.token = "1234567890"; | |
| 88 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.
| |
| 89 | |
| 90 EXPECT_THAT(builder.PreviewRequestHeadersForTesting(), | |
| 91 StrEq("Content-Type: application/json; charset=UTF-8\r\n" | |
| 92 "\r\n")); | |
| 93 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), | |
| 94 EqualsJSON("{" | |
| 95 " \"token\": " | |
| 96 " \"1234567890\"" | |
| 97 "}")); | |
| 98 } | |
| 99 | |
| 100 } // namespace internal | |
| 101 | |
| 102 } // namespace ntp_snippets | |
| OLD | NEW |