Roll src/third_party/skia de7665a:76033be
[chromium-blink-merge.git] / components / update_client / test / request_sender_unittest.cc
blobf8ad042fc6c2a33696312aa27657b4a28eb778ad
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/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "components/update_client/request_sender.h"
11 #include "components/update_client/test/test_configurator.h"
12 #include "components/update_client/test/url_request_post_interceptor.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace update_client {
18 namespace {
20 const char kUrl1[] = "https://localhost2/path1";
21 const char kUrl2[] = "https://localhost2/path2";
22 const char kUrlPath1[] = "path1";
23 const char kUrlPath2[] = "path2";
25 } // namespace
27 class RequestSenderTest : public testing::Test {
28 public:
29 RequestSenderTest();
30 ~RequestSenderTest() override;
32 // Overrides from testing::Test.
33 void SetUp() override;
34 void TearDown() override;
36 void RequestSenderComplete(const net::URLFetcher* source);
38 protected:
39 void Quit();
40 void RunThreads();
41 void RunThreadsUntilIdle();
43 scoped_ptr<TestConfigurator> config_;
44 scoped_ptr<RequestSender> request_sender_;
45 scoped_ptr<InterceptorFactory> interceptor_factory_;
47 URLRequestPostInterceptor* post_interceptor_1; // Owned by the factory.
48 URLRequestPostInterceptor* post_interceptor_2; // Owned by the factory.
50 const net::URLFetcher* url_fetcher_source_;
52 private:
53 base::MessageLoopForIO loop_;
54 base::Closure quit_closure_;
56 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest);
59 RequestSenderTest::RequestSenderTest()
60 : post_interceptor_1(NULL),
61 post_interceptor_2(NULL),
62 url_fetcher_source_(NULL) {
65 RequestSenderTest::~RequestSenderTest() {
68 void RequestSenderTest::SetUp() {
69 config_.reset(new TestConfigurator(base::MessageLoopProxy::current(),
70 base::MessageLoopProxy::current()));
71 interceptor_factory_.reset(
72 new InterceptorFactory(base::MessageLoopProxy::current()));
73 post_interceptor_1 =
74 interceptor_factory_->CreateInterceptorForPath(kUrlPath1);
75 post_interceptor_2 =
76 interceptor_factory_->CreateInterceptorForPath(kUrlPath2);
77 EXPECT_TRUE(post_interceptor_1);
78 EXPECT_TRUE(post_interceptor_2);
80 request_sender_.reset();
83 void RequestSenderTest::TearDown() {
84 request_sender_.reset();
86 post_interceptor_1 = NULL;
87 post_interceptor_2 = NULL;
89 interceptor_factory_.reset();
91 config_.reset();
93 RunThreadsUntilIdle();
96 void RequestSenderTest::RunThreads() {
97 base::RunLoop runloop;
98 quit_closure_ = runloop.QuitClosure();
99 runloop.Run();
101 // Since some tests need to drain currently enqueued tasks such as network
102 // intercepts on the IO thread, run the threads until they are
103 // idle. The component updater service won't loop again until the loop count
104 // is set and the service is started.
105 RunThreadsUntilIdle();
108 void RequestSenderTest::RunThreadsUntilIdle() {
109 base::RunLoop().RunUntilIdle();
112 void RequestSenderTest::Quit() {
113 if (!quit_closure_.is_null())
114 quit_closure_.Run();
117 void RequestSenderTest::RequestSenderComplete(const net::URLFetcher* source) {
118 url_fetcher_source_ = source;
119 Quit();
122 // Tests that when a request to the first url succeeds, the subsequent urls are
123 // not tried.
124 TEST_F(RequestSenderTest, RequestSendSuccess) {
125 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test")));
127 std::vector<GURL> urls;
128 urls.push_back(GURL(kUrl1));
129 urls.push_back(GURL(kUrl2));
130 request_sender_.reset(new RequestSender(*config_));
131 request_sender_->Send("test", urls,
132 base::Bind(&RequestSenderTest::RequestSenderComplete,
133 base::Unretained(this)));
134 RunThreads();
136 EXPECT_EQ(1, post_interceptor_1->GetHitCount())
137 << post_interceptor_1->GetRequestsAsString();
138 EXPECT_EQ(1, post_interceptor_1->GetCount())
139 << post_interceptor_1->GetRequestsAsString();
141 EXPECT_STREQ("test", post_interceptor_1->GetRequests()[0].c_str());
142 EXPECT_EQ(GURL(kUrl1), url_fetcher_source_->GetOriginalURL());
143 EXPECT_EQ(200, url_fetcher_source_->GetResponseCode());
146 // Tests that the request succeeds using the second url after the first url
147 // has failed.
148 TEST_F(RequestSenderTest, RequestSendSuccessWithFallback) {
149 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test"), 403));
150 EXPECT_TRUE(post_interceptor_2->ExpectRequest(new PartialMatch("test")));
152 std::vector<GURL> urls;
153 urls.push_back(GURL(kUrl1));
154 urls.push_back(GURL(kUrl2));
155 request_sender_.reset(new RequestSender(*config_));
156 request_sender_->Send("test", urls,
157 base::Bind(&RequestSenderTest::RequestSenderComplete,
158 base::Unretained(this)));
159 RunThreads();
161 EXPECT_EQ(1, post_interceptor_1->GetHitCount())
162 << post_interceptor_1->GetRequestsAsString();
163 EXPECT_EQ(1, post_interceptor_1->GetCount())
164 << post_interceptor_1->GetRequestsAsString();
165 EXPECT_EQ(1, post_interceptor_2->GetHitCount())
166 << post_interceptor_2->GetRequestsAsString();
167 EXPECT_EQ(1, post_interceptor_2->GetCount())
168 << post_interceptor_2->GetRequestsAsString();
170 EXPECT_STREQ("test", post_interceptor_1->GetRequests()[0].c_str());
171 EXPECT_STREQ("test", post_interceptor_2->GetRequests()[0].c_str());
172 EXPECT_EQ(GURL(kUrl2), url_fetcher_source_->GetOriginalURL());
173 EXPECT_EQ(200, url_fetcher_source_->GetResponseCode());
176 // Tests that the request fails when both urls have failed.
177 TEST_F(RequestSenderTest, RequestSendFailed) {
178 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test"), 403));
179 EXPECT_TRUE(post_interceptor_2->ExpectRequest(new PartialMatch("test"), 403));
181 std::vector<GURL> urls;
182 urls.push_back(GURL(kUrl1));
183 urls.push_back(GURL(kUrl2));
184 request_sender_.reset(new RequestSender(*config_));
185 request_sender_->Send("test", urls,
186 base::Bind(&RequestSenderTest::RequestSenderComplete,
187 base::Unretained(this)));
188 RunThreads();
190 EXPECT_EQ(1, post_interceptor_1->GetHitCount())
191 << post_interceptor_1->GetRequestsAsString();
192 EXPECT_EQ(1, post_interceptor_1->GetCount())
193 << post_interceptor_1->GetRequestsAsString();
194 EXPECT_EQ(1, post_interceptor_2->GetHitCount())
195 << post_interceptor_2->GetRequestsAsString();
196 EXPECT_EQ(1, post_interceptor_2->GetCount())
197 << post_interceptor_2->GetRequestsAsString();
199 EXPECT_STREQ("test", post_interceptor_1->GetRequests()[0].c_str());
200 EXPECT_STREQ("test", post_interceptor_2->GetRequests()[0].c_str());
201 EXPECT_EQ(GURL(kUrl2), url_fetcher_source_->GetOriginalURL());
202 EXPECT_EQ(403, url_fetcher_source_->GetResponseCode());
205 } // namespace update_client