| 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 "base/json/json_reader.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/test/gtest_util.h" |
| 11 #include "base/test/mock_callback.h" |
| 12 #include "base/values.h" |
| 13 #include "net/url_request/test_url_fetcher_factory.h" |
| 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace ntp_snippets { |
| 19 |
| 20 namespace internal { |
| 21 |
| 22 namespace { |
| 23 |
| 24 using testing::_; |
| 25 using testing::SaveArg; |
| 26 |
| 27 // TODO(mamir): Create a test_helper.cc file instead of duplicating all this |
| 28 // code. |
| 29 MATCHER_P(EqualsJSON, json, "equals JSON") { |
| 30 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json); |
| 31 if (!expected) { |
| 32 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON"; |
| 33 return false; |
| 34 } |
| 35 |
| 36 std::string err_msg; |
| 37 int err_line, err_col; |
| 38 std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError( |
| 39 arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col); |
| 40 if (!actual) { |
| 41 *result_listener << "input:" << err_line << ":" << err_col << ": " |
| 42 << "parse error: " << err_msg; |
| 43 return false; |
| 44 } |
| 45 return base::Value::Equals(actual.get(), expected.get()); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 class SubscriptionJsonRequestTest : public testing::Test { |
| 51 public: |
| 52 SubscriptionJsonRequestTest() |
| 53 : request_context_getter_( |
| 54 new net::TestURLRequestContextGetter(message_loop_.task_runner())) { |
| 55 } |
| 56 |
| 57 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() { |
| 58 return request_context_getter_.get(); |
| 59 } |
| 60 |
| 61 net::TestURLFetcher* GetRunningFetcher() { |
| 62 // All created TestURLFetchers have ID 0 by default. |
| 63 net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 64 DCHECK(url_fetcher); |
| 65 return url_fetcher; |
| 66 } |
| 67 |
| 68 void RespondWithData(const std::string& data) { |
| 69 net::TestURLFetcher* url_fetcher = GetRunningFetcher(); |
| 70 url_fetcher->set_status(net::URLRequestStatus()); |
| 71 url_fetcher->set_response_code(net::HTTP_OK); |
| 72 url_fetcher->SetResponseString(data); |
| 73 // Call the URLFetcher delegate to continue the test. |
| 74 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 75 } |
| 76 |
| 77 void RespondWithError(int error_code) { |
| 78 net::TestURLFetcher* url_fetcher = GetRunningFetcher(); |
| 79 url_fetcher->set_status(net::URLRequestStatus::FromError(error_code)); |
| 80 url_fetcher->SetResponseString(std::string()); |
| 81 // Call the URLFetcher delegate to continue the test. |
| 82 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 83 } |
| 84 |
| 85 private: |
| 86 base::MessageLoop message_loop_; |
| 87 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 88 net::TestURLFetcherFactory url_fetcher_factory_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequestTest); |
| 91 }; |
| 92 |
| 93 TEST_F(SubscriptionJsonRequestTest, BuildRequest) { |
| 94 std::string token = "1234567890"; |
| 95 GURL url("http://valid-url.test"); |
| 96 |
| 97 base::MockCallback<SubscriptionJsonRequest::CompletedCallback> callback; |
| 98 ntp_snippets::Status status(StatusCode::SUCCESS, "initial"); |
| 99 EXPECT_CALL(callback, Run(_)).WillOnce(SaveArg<0>(&status)); |
| 100 |
| 101 SubscriptionJsonRequest::Builder builder; |
| 102 std::unique_ptr<SubscriptionJsonRequest> request = |
| 103 builder.SetToken(token) |
| 104 .SetUrl(url) |
| 105 .SetUrlRequestContextGetter(GetRequestContext()) |
| 106 .Build(); |
| 107 request->Start(callback.Get()); |
| 108 |
| 109 net::TestURLFetcher* url_fetcher = GetRunningFetcher(); |
| 110 |
| 111 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); |
| 112 |
| 113 net::HttpRequestHeaders headers; |
| 114 url_fetcher->GetExtraRequestHeaders(&headers); |
| 115 |
| 116 std::string header; |
| 117 EXPECT_FALSE(headers.GetHeader("Authorization", &header)); |
| 118 EXPECT_TRUE(headers.GetHeader("Content-Type", &header)); |
| 119 EXPECT_EQ(header, "application/json; charset=UTF-8"); |
| 120 |
| 121 std::string expected_body = |
| 122 "{" |
| 123 " \"token\": " |
| 124 " \"1234567890\"" |
| 125 "}"; |
| 126 EXPECT_THAT(url_fetcher->upload_data(), EqualsJSON(expected_body)); |
| 127 } |
| 128 |
| 129 TEST_F(SubscriptionJsonRequestTest, InvokesCallbackWhenCancelled) { |
| 130 std::string token = "1234567890"; |
| 131 GURL url("http://valid-url.test"); |
| 132 |
| 133 base::MockCallback<SubscriptionJsonRequest::CompletedCallback> callback; |
| 134 ntp_snippets::Status status(StatusCode::SUCCESS, "initial"); |
| 135 EXPECT_CALL(callback, Run(_)).WillOnce(SaveArg<0>(&status)); |
| 136 |
| 137 SubscriptionJsonRequest::Builder builder; |
| 138 std::unique_ptr<SubscriptionJsonRequest> request = |
| 139 builder.SetToken(token) |
| 140 .SetUrl(url) |
| 141 .SetUrlRequestContextGetter(GetRequestContext()) |
| 142 .Build(); |
| 143 request->Start(callback.Get()); |
| 144 |
| 145 // Destroy the request before getting any response. |
| 146 request.reset(); |
| 147 |
| 148 EXPECT_EQ(status.code, StatusCode::TEMPORARY_ERROR); |
| 149 EXPECT_EQ(status.message, "cancelled"); |
| 150 } |
| 151 |
| 152 TEST_F(SubscriptionJsonRequestTest, SubscribeWithoutErrors) { |
| 153 std::string token = "1234567890"; |
| 154 GURL url("http://valid-url.test"); |
| 155 |
| 156 base::MockCallback<SubscriptionJsonRequest::CompletedCallback> callback; |
| 157 ntp_snippets::Status status(StatusCode::PERMANENT_ERROR, "initial"); |
| 158 EXPECT_CALL(callback, Run(_)).WillOnce(SaveArg<0>(&status)); |
| 159 |
| 160 SubscriptionJsonRequest::Builder builder; |
| 161 std::unique_ptr<SubscriptionJsonRequest> request = |
| 162 builder.SetToken(token) |
| 163 .SetUrl(url) |
| 164 .SetUrlRequestContextGetter(GetRequestContext()) |
| 165 .Build(); |
| 166 request->Start(callback.Get()); |
| 167 |
| 168 RespondWithData("{}"); |
| 169 |
| 170 EXPECT_EQ(status.code, StatusCode::SUCCESS); |
| 171 } |
| 172 |
| 173 TEST_F(SubscriptionJsonRequestTest, SubscribeWithErrors) { |
| 174 std::string token = "1234567890"; |
| 175 GURL url("http://valid-url.test"); |
| 176 |
| 177 base::MockCallback<SubscriptionJsonRequest::CompletedCallback> callback; |
| 178 ntp_snippets::Status status(StatusCode::SUCCESS, "initial"); |
| 179 EXPECT_CALL(callback, Run(_)).WillOnce(SaveArg<0>(&status)); |
| 180 |
| 181 SubscriptionJsonRequest::Builder builder; |
| 182 std::unique_ptr<SubscriptionJsonRequest> request = |
| 183 builder.SetToken(token) |
| 184 .SetUrl(url) |
| 185 .SetUrlRequestContextGetter(GetRequestContext()) |
| 186 .Build(); |
| 187 request->Start(callback.Get()); |
| 188 |
| 189 RespondWithError(net::ERR_TIMED_OUT); |
| 190 |
| 191 EXPECT_EQ(status.code, StatusCode::TEMPORARY_ERROR); |
| 192 } |
| 193 |
| 194 } // namespace internal |
| 195 |
| 196 } // namespace ntp_snippets |
| OLD | NEW |