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 "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(""); | |
|
Bernhard Bauer
2017/06/01 16:54:46
Use std::string() instead of a literal.
mamir
2017/06/01 17:13:01
Done.
| |
| 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) { | |
|
fhorschig
2017/06/01 16:25:22
Nit: It would be nice if you split this test.
Besi
mamir
2017/06/01 17:13:00
Done.
| |
| 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 // Destroy the request before getting any response. | |
| 128 request.reset(); | |
| 129 | |
| 130 EXPECT_EQ(status.code, StatusCode::TEMPORARY_ERROR); | |
| 131 EXPECT_EQ(status.message, "cancelled"); | |
| 132 } | |
| 133 | |
| 134 TEST_F(SubscriptionJsonRequestTest, SubscribeWithoutErrors) { | |
| 135 std::string token = "1234567890"; | |
| 136 GURL url("http://valid-url.test"); | |
| 137 | |
| 138 base::MockCallback<SubscriptionJsonRequest::CompletedCallback> callback; | |
| 139 ntp_snippets::Status status(StatusCode::PERMANENT_ERROR, "initial"); | |
| 140 EXPECT_CALL(callback, Run(_)).WillOnce(SaveArg<0>(&status)); | |
| 141 | |
| 142 SubscriptionJsonRequest::Builder builder; | |
| 143 std::unique_ptr<SubscriptionJsonRequest> request = | |
| 144 builder.SetToken(token) | |
| 145 .SetUrl(url) | |
| 146 .SetUrlRequestContextGetter(GetRequestContext()) | |
| 147 .Build(); | |
| 148 request->Start(callback.Get()); | |
| 149 | |
| 150 RespondWithData("{}"); | |
| 151 | |
| 152 EXPECT_EQ(status.code, StatusCode::SUCCESS); | |
| 153 } | |
| 154 | |
| 155 TEST_F(SubscriptionJsonRequestTest, SubscribeWithErrors) { | |
| 156 std::string token = "1234567890"; | |
| 157 GURL url("http://valid-url.test"); | |
| 158 | |
| 159 base::MockCallback<SubscriptionJsonRequest::CompletedCallback> callback; | |
| 160 ntp_snippets::Status status(StatusCode::SUCCESS, "initial"); | |
| 161 EXPECT_CALL(callback, Run(_)).WillOnce(SaveArg<0>(&status)); | |
| 162 | |
| 163 SubscriptionJsonRequest::Builder builder; | |
| 164 std::unique_ptr<SubscriptionJsonRequest> request = | |
| 165 builder.SetToken(token) | |
| 166 .SetUrl(url) | |
| 167 .SetUrlRequestContextGetter(GetRequestContext()) | |
| 168 .Build(); | |
| 169 request->Start(callback.Get()); | |
| 170 | |
| 171 RespondWithError(net::ERR_TIMED_OUT); | |
| 172 | |
| 173 EXPECT_EQ(status.code, StatusCode::TEMPORARY_ERROR); | |
| 174 } | |
| 175 | |
| 176 TEST_F(SubscriptionJsonRequestTest, StartTheRequestTwice) { | |
| 177 std::string token = "1234567890"; | |
| 178 GURL url("http://valid-url.test"); | |
| 179 | |
| 180 base::MockCallback<SubscriptionJsonRequest::CompletedCallback> callback; | |
| 181 ntp_snippets::Status status(StatusCode::SUCCESS, "initial"); | |
| 182 EXPECT_CALL(callback, Run(_)).WillOnce(SaveArg<0>(&status)); | |
| 183 | |
| 184 SubscriptionJsonRequest::Builder builder; | |
| 185 std::unique_ptr<SubscriptionJsonRequest> request = | |
| 186 builder.SetToken(token) | |
| 187 .SetUrl(url) | |
| 188 .SetUrlRequestContextGetter(GetRequestContext()) | |
| 189 .Build(); | |
| 190 request->Start(callback.Get()); | |
| 191 EXPECT_DCHECK_DEATH(request->Start(callback.Get())); | |
|
fhorschig
2017/06/01 16:25:21
Cool!
Bernhard Bauer
2017/06/01 16:54:46
I feel like I missed a tech debt meeting ;-)
I ha
mamir
2017/06/01 17:13:01
Well, I heard similar feedback about using DCHECK.
Bernhard Bauer
2017/06/01 20:34:51
As discussed off-review, I would leave out this te
fhorschig
2017/06/02 08:13:40
Actually, death tests are on next week's meeting a
mamir
2017/06/02 09:06:49
Done.
| |
| 192 } | |
| 193 | |
| 194 } // namespace internal | |
| 195 | |
| 196 } // namespace ntp_snippets | |
| OLD | NEW |