Chromium Code Reviews| 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..f6f8ff3959ed4e428cb634b9ca2166357926874d |
| --- /dev/null |
| +++ b/components/ntp_snippets/breaking_news/subscription_json_request_unittest.cc |
| @@ -0,0 +1,87 @@ |
| +// 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> |
|
fhorschig
2017/06/01 09:23:56
unused
mamir
2017/06/01 14:52:32
Acknowledged.
|
| +#include <utility> |
|
fhorschig
2017/06/01 09:23:56
unused?
mamir
2017/06/01 14:52:32
Acknowledged.
|
| + |
| +#include "base/json/json_reader.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/test/test_mock_time_task_runner.h" |
| +#include "base/values.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::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() |
| + : mock_task_runner_(new base::TestMockTimeTaskRunner()), |
| + request_context_getter_( |
| + 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.
|
| + |
| + SubscriptionJsonRequest::Builder CreateMinimalBuilder() { |
| + SubscriptionJsonRequest::Builder builder; |
| + builder.SetUrl(GURL("http://valid-url.test")) |
| + .SetUrlRequestContextGetter(request_context_getter_.get()); |
| + return builder; |
| + } |
| + |
| + private: |
| + scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; |
| + scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| + 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.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequestTest); |
| +}; |
| + |
| +TEST_F(SubscriptionJsonRequestTest, BuildRequest) { |
| + SubscriptionJsonRequest::Builder builder = CreateMinimalBuilder(); |
| + std::string token = "1234567890"; |
| + builder.SetToken(token); |
| + |
| + 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 |