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
{
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
) {
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()));
74 interceptor_factory_
->CreateInterceptorForPath(kUrlPath1
);
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();
93 RunThreadsUntilIdle();
96 void RequestSenderTest::RunThreads() {
97 base::RunLoop runloop
;
98 quit_closure_
= runloop
.QuitClosure();
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())
117 void RequestSenderTest::RequestSenderComplete(const net::URLFetcher
* source
) {
118 url_fetcher_source_
= source
;
122 // Tests that when a request to the first url succeeds, the subsequent urls are
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)));
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
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)));
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)));
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