sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 5 | #include "base/compiler_specific.h" |
sorin | 5cb1f549 | 2014-09-23 04:07:44 | [diff] [blame] | 6 | #include "base/macros.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 7 | #include "base/memory/ref_counted.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
| 9 | #include "base/message_loop/message_loop.h" |
| 10 | #include "base/run_loop.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 11 | #include "components/update_client/request_sender.h" |
sorin | b120440b | 2015-04-27 16:34:15 | [diff] [blame^] | 12 | #include "components/update_client/test_configurator.h" |
| 13 | #include "components/update_client/url_request_post_interceptor.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 14 | #include "net/url_request/url_fetcher.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 17 | namespace update_client { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 31 | ~RequestSenderTest() override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 32 | |
| 33 | // Overrides from testing::Test. |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 34 | void SetUp() override; |
| 35 | void TearDown() override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 36 | |
| 37 | void RequestSenderComplete(const net::URLFetcher* source); |
| 38 | |
| 39 | protected: |
| 40 | void Quit(); |
| 41 | void RunThreads(); |
| 42 | void RunThreadsUntilIdle(); |
| 43 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 44 | scoped_refptr<TestConfigurator> config_; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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) { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | RequestSenderTest::~RequestSenderTest() { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void RequestSenderTest::SetUp() { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 70 | config_ = new TestConfigurator(base::MessageLoopProxy::current(), |
| 71 | base::MessageLoopProxy::current()); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 92 | config_ = nullptr; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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_)); |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 132 | request_sender_->Send("test", urls, |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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_)); |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 157 | request_sender_->Send("test", urls, |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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_)); |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 186 | request_sender_->Send("test", urls, |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 206 | } // namespace update_client |