Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / update_client / request_sender_unittest.cc
blob4f7a309149dc81cff6425ff2603f21ec82d4fc28
1 // Copyright 2014 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.
5 #include "base/compiler_specific.h"
6 #include "base/macros.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "components/update_client/request_sender.h"
12 #include "components/update_client/test_configurator.h"
13 #include "components/update_client/url_request_post_interceptor.h"
14 #include "net/url_request/url_fetcher.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace update_client {
19 namespace {
21 const char kUrl1[] = "https://localhost2/path1";
22 const char kUrl2[] = "https://localhost2/path2";
23 const char kUrlPath1[] = "path1";
24 const char kUrlPath2[] = "path2";
26 } // namespace
28 class RequestSenderTest : public testing::Test {
29 public:
30 RequestSenderTest();
31 ~RequestSenderTest() override;
33 // Overrides from testing::Test.
34 void SetUp() override;
35 void TearDown() override;
37 void RequestSenderComplete(const net::URLFetcher* source);
39 protected:
40 void Quit();
41 void RunThreads();
42 void RunThreadsUntilIdle();
44 scoped_refptr<TestConfigurator> config_;
45 scoped_ptr<RequestSender> request_sender_;
46 scoped_ptr<InterceptorFactory> interceptor_factory_;
48 URLRequestPostInterceptor* post_interceptor_1_; // Owned by the factory.
49 URLRequestPostInterceptor* post_interceptor_2_; // Owned by the factory.
51 const net::URLFetcher* url_fetcher_source_;
53 private:
54 base::MessageLoopForIO loop_;
55 base::Closure quit_closure_;
57 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest);
60 RequestSenderTest::RequestSenderTest()
61 : post_interceptor_1_(nullptr),
62 post_interceptor_2_(nullptr),
63 url_fetcher_source_(nullptr) {
66 RequestSenderTest::~RequestSenderTest() {
69 void RequestSenderTest::SetUp() {
70 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(),
71 base::ThreadTaskRunnerHandle::Get());
72 interceptor_factory_.reset(
73 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get()));
74 post_interceptor_1_ =
75 interceptor_factory_->CreateInterceptorForPath(kUrlPath1);
76 post_interceptor_2_ =
77 interceptor_factory_->CreateInterceptorForPath(kUrlPath2);
78 EXPECT_TRUE(post_interceptor_1_);
79 EXPECT_TRUE(post_interceptor_2_);
81 request_sender_.reset();
84 void RequestSenderTest::TearDown() {
85 request_sender_.reset();
87 post_interceptor_1_ = nullptr;
88 post_interceptor_2_ = nullptr;
90 interceptor_factory_.reset();
92 config_ = nullptr;
94 RunThreadsUntilIdle();
97 void RequestSenderTest::RunThreads() {
98 base::RunLoop runloop;
99 quit_closure_ = runloop.QuitClosure();
100 runloop.Run();
102 // Since some tests need to drain currently enqueued tasks such as network
103 // intercepts on the IO thread, run the threads until they are
104 // idle. The component updater service won't loop again until the loop count
105 // is set and the service is started.
106 RunThreadsUntilIdle();
109 void RequestSenderTest::RunThreadsUntilIdle() {
110 base::RunLoop().RunUntilIdle();
113 void RequestSenderTest::Quit() {
114 if (!quit_closure_.is_null())
115 quit_closure_.Run();
118 void RequestSenderTest::RequestSenderComplete(const net::URLFetcher* source) {
119 url_fetcher_source_ = source;
120 Quit();
123 // Tests that when a request to the first url succeeds, the subsequent urls are
124 // not tried.
125 TEST_F(RequestSenderTest, RequestSendSuccess) {
126 EXPECT_TRUE(post_interceptor_1_->ExpectRequest(new PartialMatch("test")));
128 std::vector<GURL> urls;
129 urls.push_back(GURL(kUrl1));
130 urls.push_back(GURL(kUrl2));
131 request_sender_.reset(new RequestSender(*config_));
132 request_sender_->Send("test", urls,
133 base::Bind(&RequestSenderTest::RequestSenderComplete,
134 base::Unretained(this)));
135 RunThreads();
137 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
138 << post_interceptor_1_->GetRequestsAsString();
139 EXPECT_EQ(1, post_interceptor_1_->GetCount())
140 << post_interceptor_1_->GetRequestsAsString();
142 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str());
143 EXPECT_EQ(GURL(kUrl1), url_fetcher_source_->GetOriginalURL());
144 EXPECT_EQ(200, url_fetcher_source_->GetResponseCode());
147 // Tests that the request succeeds using the second url after the first url
148 // has failed.
149 TEST_F(RequestSenderTest, RequestSendSuccessWithFallback) {
150 EXPECT_TRUE(
151 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403));
152 EXPECT_TRUE(post_interceptor_2_->ExpectRequest(new PartialMatch("test")));
154 std::vector<GURL> urls;
155 urls.push_back(GURL(kUrl1));
156 urls.push_back(GURL(kUrl2));
157 request_sender_.reset(new RequestSender(*config_));
158 request_sender_->Send("test", urls,
159 base::Bind(&RequestSenderTest::RequestSenderComplete,
160 base::Unretained(this)));
161 RunThreads();
163 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
164 << post_interceptor_1_->GetRequestsAsString();
165 EXPECT_EQ(1, post_interceptor_1_->GetCount())
166 << post_interceptor_1_->GetRequestsAsString();
167 EXPECT_EQ(1, post_interceptor_2_->GetHitCount())
168 << post_interceptor_2_->GetRequestsAsString();
169 EXPECT_EQ(1, post_interceptor_2_->GetCount())
170 << post_interceptor_2_->GetRequestsAsString();
172 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str());
173 EXPECT_STREQ("test", post_interceptor_2_->GetRequests()[0].c_str());
174 EXPECT_EQ(GURL(kUrl2), url_fetcher_source_->GetOriginalURL());
175 EXPECT_EQ(200, url_fetcher_source_->GetResponseCode());
178 // Tests that the request fails when both urls have failed.
179 TEST_F(RequestSenderTest, RequestSendFailed) {
180 EXPECT_TRUE(
181 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403));
182 EXPECT_TRUE(
183 post_interceptor_2_->ExpectRequest(new PartialMatch("test"), 403));
185 std::vector<GURL> urls;
186 urls.push_back(GURL(kUrl1));
187 urls.push_back(GURL(kUrl2));
188 request_sender_.reset(new RequestSender(*config_));
189 request_sender_->Send("test", urls,
190 base::Bind(&RequestSenderTest::RequestSenderComplete,
191 base::Unretained(this)));
192 RunThreads();
194 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
195 << post_interceptor_1_->GetRequestsAsString();
196 EXPECT_EQ(1, post_interceptor_1_->GetCount())
197 << post_interceptor_1_->GetRequestsAsString();
198 EXPECT_EQ(1, post_interceptor_2_->GetHitCount())
199 << post_interceptor_2_->GetRequestsAsString();
200 EXPECT_EQ(1, post_interceptor_2_->GetCount())
201 << post_interceptor_2_->GetRequestsAsString();
203 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str());
204 EXPECT_STREQ("test", post_interceptor_2_->GetRequests()[0].c_str());
205 EXPECT_EQ(GURL(kUrl2), url_fetcher_source_->GetOriginalURL());
206 EXPECT_EQ(403, url_fetcher_source_->GetResponseCode());
209 // Tests that the request fails when no urls are provided.
210 TEST_F(RequestSenderTest, RequestSendFailedNoUrls) {
211 std::vector<GURL> urls;
212 request_sender_.reset(new RequestSender(*config_));
213 request_sender_->Send("test", urls,
214 base::Bind(&RequestSenderTest::RequestSenderComplete,
215 base::Unretained(this)));
216 RunThreads();
218 EXPECT_EQ(nullptr, url_fetcher_source_);
221 } // namespace update_client