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
) {
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",
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",
159 base::Bind(&RequestSenderTest::RequestSenderComplete
,
160 base::Unretained(this)));
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(post_interceptor_1
->ExpectRequest(new PartialMatch("test"), 403));
181 EXPECT_TRUE(post_interceptor_2
->ExpectRequest(new PartialMatch("test"), 403));
183 std::vector
<GURL
> urls
;
184 urls
.push_back(GURL(kUrl1
));
185 urls
.push_back(GURL(kUrl2
));
186 request_sender_
.reset(new RequestSender(*config_
));
187 request_sender_
->Send("test",
189 base::Bind(&RequestSenderTest::RequestSenderComplete
,
190 base::Unretained(this)));
193 EXPECT_EQ(1, post_interceptor_1
->GetHitCount())
194 << post_interceptor_1
->GetRequestsAsString();
195 EXPECT_EQ(1, post_interceptor_1
->GetCount())
196 << post_interceptor_1
->GetRequestsAsString();
197 EXPECT_EQ(1, post_interceptor_2
->GetHitCount())
198 << post_interceptor_2
->GetRequestsAsString();
199 EXPECT_EQ(1, post_interceptor_2
->GetCount())
200 << post_interceptor_2
->GetRequestsAsString();
202 EXPECT_STREQ("test", post_interceptor_1
->GetRequests()[0].c_str());
203 EXPECT_STREQ("test", post_interceptor_2
->GetRequests()[0].c_str());
204 EXPECT_EQ(GURL(kUrl2
), url_fetcher_source_
->GetOriginalURL());
205 EXPECT_EQ(403, url_fetcher_source_
->GetResponseCode());
208 } // namespace component_updater