OLD | NEW |
| (Empty) |
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. | |
4 | |
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" | |
16 | |
17 namespace update_client { | |
18 | |
19 namespace { | |
20 | |
21 const char kUrl1[] = "https://localhost2/path1"; | |
22 const char kUrl2[] = "https://localhost2/path2"; | |
23 const char kUrlPath1[] = "path1"; | |
24 const char kUrlPath2[] = "path2"; | |
25 | |
26 } // namespace | |
27 | |
28 class RequestSenderTest : public testing::Test { | |
29 public: | |
30 RequestSenderTest(); | |
31 ~RequestSenderTest() override; | |
32 | |
33 // Overrides from testing::Test. | |
34 void SetUp() override; | |
35 void TearDown() override; | |
36 | |
37 void RequestSenderComplete(const net::URLFetcher* source); | |
38 | |
39 protected: | |
40 void Quit(); | |
41 void RunThreads(); | |
42 void RunThreadsUntilIdle(); | |
43 | |
44 scoped_refptr<TestConfigurator> config_; | |
45 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 | |
60 RequestSenderTest::RequestSenderTest() | |
61 : post_interceptor_1(NULL), | |
62 post_interceptor_2(NULL), | |
63 url_fetcher_source_(NULL) { | |
64 } | |
65 | |
66 RequestSenderTest::~RequestSenderTest() { | |
67 } | |
68 | |
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())); | |
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 | |
84 void RequestSenderTest::TearDown() { | |
85 request_sender_.reset(); | |
86 | |
87 post_interceptor_1 = NULL; | |
88 post_interceptor_2 = NULL; | |
89 | |
90 interceptor_factory_.reset(); | |
91 | |
92 config_ = nullptr; | |
93 | |
94 RunThreadsUntilIdle(); | |
95 } | |
96 | |
97 void 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 | |
109 void RequestSenderTest::RunThreadsUntilIdle() { | |
110 base::RunLoop().RunUntilIdle(); | |
111 } | |
112 | |
113 void RequestSenderTest::Quit() { | |
114 if (!quit_closure_.is_null()) | |
115 quit_closure_.Run(); | |
116 } | |
117 | |
118 void 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. | |
125 TEST_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_)); | |
132 request_sender_->Send("test", urls, | |
133 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. | |
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"))); | |
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_)); | |
157 request_sender_->Send("test", urls, | |
158 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. | |
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)); | |
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_)); | |
186 request_sender_->Send("test", urls, | |
187 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 | |
206 } // namespace update_client | |
OLD | NEW |