| 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/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" | |
| 15 | |
| 16 namespace component_updater { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kUrl1[] = "https://localhost2/path1"; | |
| 21 const char kUrl2[] = "https://localhost2/path2"; | |
| 22 const char kUrlPath1[] = "path1"; | |
| 23 const char kUrlPath2[] = "path2"; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 class RequestSenderTest : public testing::Test { | |
| 28 public: | |
| 29 RequestSenderTest(); | |
| 30 ~RequestSenderTest() override; | |
| 31 | |
| 32 // Overrides from testing::Test. | |
| 33 void SetUp() override; | |
| 34 void TearDown() override; | |
| 35 | |
| 36 void RequestSenderComplete(const net::URLFetcher* source); | |
| 37 | |
| 38 protected: | |
| 39 void Quit(); | |
| 40 void RunThreads(); | |
| 41 void RunThreadsUntilIdle(); | |
| 42 | |
| 43 scoped_ptr<TestConfigurator> config_; | |
| 44 scoped_ptr<RequestSender> request_sender_; | |
| 45 scoped_ptr<InterceptorFactory> interceptor_factory_; | |
| 46 | |
| 47 URLRequestPostInterceptor* post_interceptor_1; // Owned by the factory. | |
| 48 URLRequestPostInterceptor* post_interceptor_2; // Owned by the factory. | |
| 49 | |
| 50 const net::URLFetcher* url_fetcher_source_; | |
| 51 | |
| 52 private: | |
| 53 base::MessageLoopForIO loop_; | |
| 54 base::Closure quit_closure_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest); | |
| 57 }; | |
| 58 | |
| 59 RequestSenderTest::RequestSenderTest() | |
| 60 : post_interceptor_1(NULL), | |
| 61 post_interceptor_2(NULL), | |
| 62 url_fetcher_source_(NULL) { | |
| 63 } | |
| 64 | |
| 65 RequestSenderTest::~RequestSenderTest() { | |
| 66 } | |
| 67 | |
| 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())); | |
| 73 post_interceptor_1 = | |
| 74 interceptor_factory_->CreateInterceptorForPath(kUrlPath1); | |
| 75 post_interceptor_2 = | |
| 76 interceptor_factory_->CreateInterceptorForPath(kUrlPath2); | |
| 77 EXPECT_TRUE(post_interceptor_1); | |
| 78 EXPECT_TRUE(post_interceptor_2); | |
| 79 | |
| 80 request_sender_.reset(); | |
| 81 } | |
| 82 | |
| 83 void RequestSenderTest::TearDown() { | |
| 84 request_sender_.reset(); | |
| 85 | |
| 86 post_interceptor_1 = NULL; | |
| 87 post_interceptor_2 = NULL; | |
| 88 | |
| 89 interceptor_factory_.reset(); | |
| 90 | |
| 91 config_.reset(); | |
| 92 | |
| 93 RunThreadsUntilIdle(); | |
| 94 } | |
| 95 | |
| 96 void RequestSenderTest::RunThreads() { | |
| 97 base::RunLoop runloop; | |
| 98 quit_closure_ = runloop.QuitClosure(); | |
| 99 runloop.Run(); | |
| 100 | |
| 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(); | |
| 106 } | |
| 107 | |
| 108 void RequestSenderTest::RunThreadsUntilIdle() { | |
| 109 base::RunLoop().RunUntilIdle(); | |
| 110 } | |
| 111 | |
| 112 void RequestSenderTest::Quit() { | |
| 113 if (!quit_closure_.is_null()) | |
| 114 quit_closure_.Run(); | |
| 115 } | |
| 116 | |
| 117 void RequestSenderTest::RequestSenderComplete(const net::URLFetcher* source) { | |
| 118 url_fetcher_source_ = source; | |
| 119 Quit(); | |
| 120 } | |
| 121 | |
| 122 // Tests that when a request to the first url succeeds, the subsequent urls are | |
| 123 // not tried. | |
| 124 TEST_F(RequestSenderTest, RequestSendSuccess) { | |
| 125 EXPECT_TRUE(post_interceptor_1->ExpectRequest(new PartialMatch("test"))); | |
| 126 | |
| 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", | |
| 132 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", | |
| 158 urls, | |
| 159 base::Bind(&RequestSenderTest::RequestSenderComplete, | |
| 160 base::Unretained(this))); | |
| 161 RunThreads(); | |
| 162 | |
| 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(); | |
| 171 | |
| 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()); | |
| 176 } | |
| 177 | |
| 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)); | |
| 182 | |
| 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", | |
| 188 urls, | |
| 189 base::Bind(&RequestSenderTest::RequestSenderComplete, | |
| 190 base::Unretained(this))); | |
| 191 RunThreads(); | |
| 192 | |
| 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(); | |
| 201 | |
| 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()); | |
| 206 } | |
| 207 | |
| 208 } // namespace component_updater | |
| OLD | NEW |