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/component_updater/request_sender.h"
11 #include "components/component_updater/test/test_configurator.h"
12 #include "components/component_updater/test/url_request_post_interceptor.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace component_updater {
20 const char kUrl1[] = "https://localhost2/path1";
21 const char kUrl2[] = "https://localhost2/path2";
22 const char kUrlPath1[] = "path1";
23 const char kUrlPath2[] = "path2";
27 class RequestSenderTest : public testing::Test {
30 ~RequestSenderTest() override;
32 // Overrides from testing::Test.
33 void SetUp() override;
34 void TearDown() override;
36 void RequestSenderComplete(const net::URLFetcher* source);
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_;
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) {
63 net::URLFetcher::SetEnableInterceptionForTests(true);
66 RequestSenderTest::~RequestSenderTest() {
67 net::URLFetcher::SetEnableInterceptionForTests(false);
70 void RequestSenderTest::SetUp() {
71 config_.reset(new TestConfigurator(base::MessageLoopProxy::current(),
72 base::MessageLoopProxy::current()));
73 interceptor_factory_.reset(
74 new InterceptorFactory(base::MessageLoopProxy::current()));
76 interceptor_factory_->CreateInterceptorForPath(kUrlPath1);
78 interceptor_factory_->CreateInterceptorForPath(kUrlPath2);
79 EXPECT_TRUE(post_interceptor_1);
80 EXPECT_TRUE(post_interceptor_2);
82 request_sender_.reset();
85 void RequestSenderTest::TearDown() {
86 request_sender_.reset();
88 post_interceptor_1 = NULL;
89 post_interceptor_2 = NULL;
91 interceptor_factory_.reset();
95 RunThreadsUntilIdle();
98 void RequestSenderTest::RunThreads() {
99 base::RunLoop runloop;
100 quit_closure_ = runloop.QuitClosure();
103 // Since some tests need to drain currently enqueued tasks such as network
104 // intercepts on the IO thread, run the threads until they are
105 // idle. The component updater service won't loop again until the loop count
106 // is set and the service is started.
107 RunThreadsUntilIdle();
110 void RequestSenderTest::RunThreadsUntilIdle() {
111 base::RunLoop().RunUntilIdle();
114 void RequestSenderTest::Quit() {
115 if (!quit_closure_.is_null())
119 void RequestSenderTest::RequestSenderComplete(const net::URLFetcher* source) {
120 url_fetcher_source_ = source;
124 // Tests that when a request to the first url succeeds, the subsequent urls are
126 TEST_F(RequestSenderTest, RequestSendSuccess) {
127 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test")));
129 std::vector<GURL> urls;
130 urls.push_back(GURL(kUrl1));
131 urls.push_back(GURL(kUrl2));
132 request_sender_.reset(new RequestSender(*config_));
133 request_sender_->Send("test",
135 base::Bind(&RequestSenderTest::RequestSenderComplete,
136 base::Unretained(this)));
139 EXPECT_EQ(1, post_interceptor_1->GetHitCount())
140 << post_interceptor_1->GetRequestsAsString();
141 EXPECT_EQ(1, post_interceptor_1->GetCount())
142 << post_interceptor_1->GetRequestsAsString();
144 EXPECT_STREQ("test", post_interceptor_1->GetRequests()[0].c_str());
145 EXPECT_EQ(GURL(kUrl1), url_fetcher_source_->GetOriginalURL());
146 EXPECT_EQ(200, url_fetcher_source_->GetResponseCode());
149 // Tests that the request succeeds using the second url after the first url
151 TEST_F(RequestSenderTest, RequestSendSuccessWithFallback) {
152 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test"), 403));
153 EXPECT_TRUE(post_interceptor_2->ExpectRequest(new PartialMatch("test")));
155 std::vector<GURL> urls;
156 urls.push_back(GURL(kUrl1));
157 urls.push_back(GURL(kUrl2));
158 request_sender_.reset(new RequestSender(*config_));
159 request_sender_->Send("test",
161 base::Bind(&RequestSenderTest::RequestSenderComplete,
162 base::Unretained(this)));
165 EXPECT_EQ(1, post_interceptor_1->GetHitCount())
166 << post_interceptor_1->GetRequestsAsString();
167 EXPECT_EQ(1, post_interceptor_1->GetCount())
168 << post_interceptor_1->GetRequestsAsString();
169 EXPECT_EQ(1, post_interceptor_2->GetHitCount())
170 << post_interceptor_2->GetRequestsAsString();
171 EXPECT_EQ(1, post_interceptor_2->GetCount())
172 << post_interceptor_2->GetRequestsAsString();
174 EXPECT_STREQ("test", post_interceptor_1->GetRequests()[0].c_str());
175 EXPECT_STREQ("test", post_interceptor_2->GetRequests()[0].c_str());
176 EXPECT_EQ(GURL(kUrl2), url_fetcher_source_->GetOriginalURL());
177 EXPECT_EQ(200, url_fetcher_source_->GetResponseCode());
180 // Tests that the request fails when both urls have failed.
181 TEST_F(RequestSenderTest, RequestSendFailed) {
182 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test"), 403));
183 EXPECT_TRUE(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",
191 base::Bind(&RequestSenderTest::RequestSenderComplete,
192 base::Unretained(this)));
195 EXPECT_EQ(1, post_interceptor_1->GetHitCount())
196 << post_interceptor_1->GetRequestsAsString();
197 EXPECT_EQ(1, post_interceptor_1->GetCount())
198 << post_interceptor_1->GetRequestsAsString();
199 EXPECT_EQ(1, post_interceptor_2->GetHitCount())
200 << post_interceptor_2->GetRequestsAsString();
201 EXPECT_EQ(1, post_interceptor_2->GetCount())
202 << post_interceptor_2->GetRequestsAsString();
204 EXPECT_STREQ("test", post_interceptor_1->GetRequests()[0].c_str());
205 EXPECT_STREQ("test", post_interceptor_2->GetRequests()[0].c_str());
206 EXPECT_EQ(GURL(kUrl2), url_fetcher_source_->GetOriginalURL());
207 EXPECT_EQ(403, url_fetcher_source_->GetResponseCode());
210 } // namespace component_updater