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/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "components/update_client/request_sender.h"
12 #include "components/update_client/test/test_configurator.h"
13 #include "components/update_client/test/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
{
21 const char kUrl1
[] = "https://localhost2/path1";
22 const char kUrl2
[] = "https://localhost2/path2";
23 const char kUrlPath1
[] = "path1";
24 const char kUrlPath2
[] = "path2";
28 class RequestSenderTest
: public testing::Test
{
31 ~RequestSenderTest() override
;
33 // Overrides from testing::Test.
34 void SetUp() override
;
35 void TearDown() override
;
37 void RequestSenderComplete(const net::URLFetcher
* source
);
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_
;
54 base::MessageLoopForIO loop_
;
55 base::Closure quit_closure_
;
57 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest
);
60 RequestSenderTest::RequestSenderTest()
61 : post_interceptor_1(NULL
),
62 post_interceptor_2(NULL
),
63 url_fetcher_source_(NULL
) {
66 RequestSenderTest::~RequestSenderTest() {
69 void RequestSenderTest::SetUp() {
70 config_
= new TestConfigurator(base::MessageLoopProxy::current(),
71 base::MessageLoopProxy::current());
72 interceptor_factory_
.reset(
73 new InterceptorFactory(base::MessageLoopProxy::current()));
75 interceptor_factory_
->CreateInterceptorForPath(kUrlPath1
);
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
= NULL
;
88 post_interceptor_2
= NULL
;
90 interceptor_factory_
.reset();
94 RunThreadsUntilIdle();
97 void RequestSenderTest::RunThreads() {
98 base::RunLoop runloop
;
99 quit_closure_
= runloop
.QuitClosure();
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())
118 void RequestSenderTest::RequestSenderComplete(const net::URLFetcher
* source
) {
119 url_fetcher_source_
= source
;
123 // Tests that when a request to the first url succeeds, the subsequent urls are
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)));
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
149 TEST_F(RequestSenderTest
, RequestSendSuccessWithFallback
) {
150 EXPECT_TRUE(post_interceptor_1
->ExpectRequest(new PartialMatch("test"), 403));
151 EXPECT_TRUE(post_interceptor_2
->ExpectRequest(new PartialMatch("test")));
153 std::vector
<GURL
> urls
;
154 urls
.push_back(GURL(kUrl1
));
155 urls
.push_back(GURL(kUrl2
));
156 request_sender_
.reset(new RequestSender(*config_
));
157 request_sender_
->Send("test", urls
,
158 base::Bind(&RequestSenderTest::RequestSenderComplete
,
159 base::Unretained(this)));
162 EXPECT_EQ(1, post_interceptor_1
->GetHitCount())
163 << post_interceptor_1
->GetRequestsAsString();
164 EXPECT_EQ(1, post_interceptor_1
->GetCount())
165 << post_interceptor_1
->GetRequestsAsString();
166 EXPECT_EQ(1, post_interceptor_2
->GetHitCount())
167 << post_interceptor_2
->GetRequestsAsString();
168 EXPECT_EQ(1, post_interceptor_2
->GetCount())
169 << post_interceptor_2
->GetRequestsAsString();
171 EXPECT_STREQ("test", post_interceptor_1
->GetRequests()[0].c_str());
172 EXPECT_STREQ("test", post_interceptor_2
->GetRequests()[0].c_str());
173 EXPECT_EQ(GURL(kUrl2
), url_fetcher_source_
->GetOriginalURL());
174 EXPECT_EQ(200, url_fetcher_source_
->GetResponseCode());
177 // Tests that the request fails when both urls have failed.
178 TEST_F(RequestSenderTest
, RequestSendFailed
) {
179 EXPECT_TRUE(post_interceptor_1
->ExpectRequest(new PartialMatch("test"), 403));
180 EXPECT_TRUE(post_interceptor_2
->ExpectRequest(new PartialMatch("test"), 403));
182 std::vector
<GURL
> urls
;
183 urls
.push_back(GURL(kUrl1
));
184 urls
.push_back(GURL(kUrl2
));
185 request_sender_
.reset(new RequestSender(*config_
));
186 request_sender_
->Send("test", urls
,
187 base::Bind(&RequestSenderTest::RequestSenderComplete
,
188 base::Unretained(this)));
191 EXPECT_EQ(1, post_interceptor_1
->GetHitCount())
192 << post_interceptor_1
->GetRequestsAsString();
193 EXPECT_EQ(1, post_interceptor_1
->GetCount())
194 << post_interceptor_1
->GetRequestsAsString();
195 EXPECT_EQ(1, post_interceptor_2
->GetHitCount())
196 << post_interceptor_2
->GetRequestsAsString();
197 EXPECT_EQ(1, post_interceptor_2
->GetCount())
198 << post_interceptor_2
->GetRequestsAsString();
200 EXPECT_STREQ("test", post_interceptor_1
->GetRequests()[0].c_str());
201 EXPECT_STREQ("test", post_interceptor_2
->GetRequests()[0].c_str());
202 EXPECT_EQ(GURL(kUrl2
), url_fetcher_source_
->GetOriginalURL());
203 EXPECT_EQ(403, url_fetcher_source_
->GetResponseCode());
206 } // namespace update_client