blob: 4c8e1db1c2872706f9beef31af9e4eae201814d0 [file] [log] [blame]
sorin395c2ac2014-09-16 21:31:071// 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.
4
sorin395c2ac2014-09-16 21:31:075#include "base/compiler_specific.h"
sorin5cb1f5492014-09-23 04:07:446#include "base/macros.h"
sorin9797aba2015-04-17 17:15:037#include "base/memory/ref_counted.h"
sorin395c2ac2014-09-16 21:31:078#include "base/memory/scoped_ptr.h"
9#include "base/message_loop/message_loop.h"
10#include "base/run_loop.h"
sorin52ac0882015-01-24 01:15:0011#include "components/update_client/request_sender.h"
sorinb120440b2015-04-27 16:34:1512#include "components/update_client/test_configurator.h"
13#include "components/update_client/url_request_post_interceptor.h"
sorin395c2ac2014-09-16 21:31:0714#include "net/url_request/url_fetcher.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
sorin52ac0882015-01-24 01:15:0017namespace update_client {
sorin395c2ac2014-09-16 21:31:0718
19namespace {
20
21const char kUrl1[] = "https://localhost2/path1";
22const char kUrl2[] = "https://localhost2/path2";
23const char kUrlPath1[] = "path1";
24const char kUrlPath2[] = "path2";
25
26} // namespace
27
28class RequestSenderTest : public testing::Test {
29 public:
30 RequestSenderTest();
dcheng30a1b1542014-10-29 21:27:5031 ~RequestSenderTest() override;
sorin395c2ac2014-09-16 21:31:0732
33 // Overrides from testing::Test.
dcheng30a1b1542014-10-29 21:27:5034 void SetUp() override;
35 void TearDown() override;
sorin395c2ac2014-09-16 21:31:0736
37 void RequestSenderComplete(const net::URLFetcher* source);
38
39 protected:
40 void Quit();
41 void RunThreads();
42 void RunThreadsUntilIdle();
43
sorin9797aba2015-04-17 17:15:0344 scoped_refptr<TestConfigurator> config_;
sorin395c2ac2014-09-16 21:31:0745 scoped_ptr<RequestSender> request_sender_;
46 scoped_ptr<InterceptorFactory> interceptor_factory_;
47
48 URLRequestPostInterceptor* post_interceptor_1; // Owned by the factory.
49 URLRequestPostInterceptor* post_interceptor_2; // Owned by the factory.
50
51 const net::URLFetcher* url_fetcher_source_;
52
53 private:
54 base::MessageLoopForIO loop_;
55 base::Closure quit_closure_;
56
57 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest);
58};
59
60RequestSenderTest::RequestSenderTest()
61 : post_interceptor_1(NULL),
62 post_interceptor_2(NULL),
63 url_fetcher_source_(NULL) {
sorin395c2ac2014-09-16 21:31:0764}
65
66RequestSenderTest::~RequestSenderTest() {
sorin395c2ac2014-09-16 21:31:0767}
68
69void RequestSenderTest::SetUp() {
sorin9797aba2015-04-17 17:15:0370 config_ = new TestConfigurator(base::MessageLoopProxy::current(),
71 base::MessageLoopProxy::current());
sorin395c2ac2014-09-16 21:31:0772 interceptor_factory_.reset(
73 new InterceptorFactory(base::MessageLoopProxy::current()));
74 post_interceptor_1 =
75 interceptor_factory_->CreateInterceptorForPath(kUrlPath1);
76 post_interceptor_2 =
77 interceptor_factory_->CreateInterceptorForPath(kUrlPath2);
78 EXPECT_TRUE(post_interceptor_1);
79 EXPECT_TRUE(post_interceptor_2);
80
81 request_sender_.reset();
82}
83
84void RequestSenderTest::TearDown() {
85 request_sender_.reset();
86
87 post_interceptor_1 = NULL;
88 post_interceptor_2 = NULL;
89
90 interceptor_factory_.reset();
91
sorin9797aba2015-04-17 17:15:0392 config_ = nullptr;
sorin395c2ac2014-09-16 21:31:0793
94 RunThreadsUntilIdle();
95}
96
97void RequestSenderTest::RunThreads() {
98 base::RunLoop runloop;
99 quit_closure_ = runloop.QuitClosure();
100 runloop.Run();
101
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();
107}
108
109void RequestSenderTest::RunThreadsUntilIdle() {
110 base::RunLoop().RunUntilIdle();
111}
112
113void RequestSenderTest::Quit() {
114 if (!quit_closure_.is_null())
115 quit_closure_.Run();
116}
117
118void RequestSenderTest::RequestSenderComplete(const net::URLFetcher* source) {
119 url_fetcher_source_ = source;
120 Quit();
121}
122
123// Tests that when a request to the first url succeeds, the subsequent urls are
124// not tried.
125TEST_F(RequestSenderTest, RequestSendSuccess) {
126 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test")));
127
128 std::vector<GURL> urls;
129 urls.push_back(GURL(kUrl1));
130 urls.push_back(GURL(kUrl2));
131 request_sender_.reset(new RequestSender(*config_));
sorin52ac0882015-01-24 01:15:00132 request_sender_->Send("test", urls,
sorin395c2ac2014-09-16 21:31:07133 base::Bind(&RequestSenderTest::RequestSenderComplete,
134 base::Unretained(this)));
135 RunThreads();
136
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();
141
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());
145}
146
147// Tests that the request succeeds using the second url after the first url
148// has failed.
149TEST_F(RequestSenderTest, RequestSendSuccessWithFallback) {
150 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test"), 403));
151 EXPECT_TRUE(post_interceptor_2->ExpectRequest(new PartialMatch("test")));
152
153 std::vector<GURL> urls;
154 urls.push_back(GURL(kUrl1));
155 urls.push_back(GURL(kUrl2));
156 request_sender_.reset(new RequestSender(*config_));
sorin52ac0882015-01-24 01:15:00157 request_sender_->Send("test", urls,
sorin395c2ac2014-09-16 21:31:07158 base::Bind(&RequestSenderTest::RequestSenderComplete,
159 base::Unretained(this)));
160 RunThreads();
161
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();
170
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());
175}
176
177// Tests that the request fails when both urls have failed.
178TEST_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));
181
182 std::vector<GURL> urls;
183 urls.push_back(GURL(kUrl1));
184 urls.push_back(GURL(kUrl2));
185 request_sender_.reset(new RequestSender(*config_));
sorin52ac0882015-01-24 01:15:00186 request_sender_->Send("test", urls,
sorin395c2ac2014-09-16 21:31:07187 base::Bind(&RequestSenderTest::RequestSenderComplete,
188 base::Unretained(this)));
189 RunThreads();
190
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();
199
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());
204}
205
sorin52ac0882015-01-24 01:15:00206} // namespace update_client