| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "mojo/shell/fetcher/network_fetcher.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/at_exit.h" | |
| 12 #include "base/auto_reset.h" | |
| 13 #include "base/bind.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/message_loop/message_loop.h" | |
| 18 #include "base/run_loop.h" | |
| 19 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 20 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | |
| 21 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 namespace mojo { | |
| 25 namespace shell { | |
| 26 namespace { | |
| 27 | |
| 28 const char k200Request[] = "http://request_expect_200"; | |
| 29 const char k404Request[] = "http://request_expect_404"; | |
| 30 const char k504Request[] = "http://request_expect_504"; | |
| 31 const char kErrorRequest[] = "http://request_expect_error"; | |
| 32 | |
| 33 class TestURLLoaderImpl : public URLLoader { | |
| 34 public: | |
| 35 explicit TestURLLoaderImpl(InterfaceRequest<URLLoader> request) | |
| 36 : binding_(this, std::move(request)) {} | |
| 37 ~TestURLLoaderImpl() override {} | |
| 38 | |
| 39 private: | |
| 40 // URLLoader implementation. | |
| 41 void Start(URLRequestPtr request, | |
| 42 const Callback<void(URLResponsePtr)>& callback) override { | |
| 43 URLResponsePtr response(URLResponse::New()); | |
| 44 response->url = request->url; | |
| 45 if (request->url == std::string(k200Request)) { | |
| 46 response->mime_type = "text/html"; | |
| 47 response->status_code = 200; | |
| 48 } else if (request->url == std::string(k404Request)) { | |
| 49 response->mime_type = "text/html"; | |
| 50 response->status_code = 404; | |
| 51 } else if (request->url == std::string(k504Request)) { | |
| 52 response->mime_type = "text/html"; | |
| 53 response->status_code = 504; | |
| 54 } else { | |
| 55 response->error = NetworkError::New(); | |
| 56 response->error->code = -2; | |
| 57 } | |
| 58 callback.Run(std::move(response)); | |
| 59 } | |
| 60 void FollowRedirect(const Callback<void(URLResponsePtr)>& callback) override { | |
| 61 NOTREACHED(); | |
| 62 } | |
| 63 void QueryStatus( | |
| 64 const Callback<void(URLLoaderStatusPtr)>& callback) override { | |
| 65 NOTREACHED(); | |
| 66 } | |
| 67 | |
| 68 StrongBinding<URLLoader> binding_; | |
| 69 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderImpl); | |
| 70 }; | |
| 71 | |
| 72 class TestURLLoaderFactoryImpl : public URLLoaderFactory { | |
| 73 public: | |
| 74 explicit TestURLLoaderFactoryImpl(InterfaceRequest<URLLoaderFactory> request) | |
| 75 : binding_(this, std::move(request)) {} | |
| 76 ~TestURLLoaderFactoryImpl() override {} | |
| 77 | |
| 78 private: | |
| 79 // URLLoaderFactory implementation. | |
| 80 void CreateURLLoader(InterfaceRequest<URLLoader> loader) override { | |
| 81 new TestURLLoaderImpl(std::move(loader)); | |
| 82 } | |
| 83 | |
| 84 StrongBinding<URLLoaderFactory> binding_; | |
| 85 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactoryImpl); | |
| 86 }; | |
| 87 | |
| 88 class FetchCallbackHelper { | |
| 89 public: | |
| 90 FetchCallbackHelper() : run_loop_(nullptr) {} | |
| 91 ~FetchCallbackHelper() {} | |
| 92 | |
| 93 Fetcher::FetchCallback GetCallback() { | |
| 94 return base::Bind(&FetchCallbackHelper::CallbackHandler, | |
| 95 base::Unretained(this)); | |
| 96 } | |
| 97 | |
| 98 void WaitForCallback() { | |
| 99 base::RunLoop run_loop; | |
| 100 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); | |
| 101 run_loop.Run(); | |
| 102 } | |
| 103 | |
| 104 Fetcher* fetcher() const { return fetcher_.get(); } | |
| 105 | |
| 106 private: | |
| 107 void CallbackHandler(scoped_ptr<Fetcher> fetcher) { | |
| 108 fetcher_ = std::move(fetcher); | |
| 109 if (run_loop_) | |
| 110 run_loop_->Quit(); | |
| 111 } | |
| 112 | |
| 113 // If it is not null, it points to a stack-allocated base::RunLoop instance in | |
| 114 // WaitForCallback(). | |
| 115 base::RunLoop* run_loop_; | |
| 116 scoped_ptr<Fetcher> fetcher_; | |
| 117 DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper); | |
| 118 }; | |
| 119 | |
| 120 class NetworkFetcherTest : public testing::Test { | |
| 121 public: | |
| 122 NetworkFetcherTest() {} | |
| 123 ~NetworkFetcherTest() override {} | |
| 124 | |
| 125 protected: | |
| 126 // Overridden from testing::Test: | |
| 127 void SetUp() override { | |
| 128 // Automatically destroyed when |url_loader_factory_| is closed. | |
| 129 new TestURLLoaderFactoryImpl(GetProxy(&url_loader_factory_)); | |
| 130 } | |
| 131 | |
| 132 // When |expect_fetch_success| is false, |expected_status_code| is ignored. | |
| 133 void TestFetchURL(const std::string& url, | |
| 134 bool expect_fetch_success, | |
| 135 uint32_t expected_status_code) { | |
| 136 FetchCallbackHelper helper; | |
| 137 | |
| 138 URLRequestPtr request(URLRequest::New()); | |
| 139 request->url = url; | |
| 140 new NetworkFetcher(true, std::move(request), url_loader_factory_.get(), | |
| 141 helper.GetCallback()); | |
| 142 helper.WaitForCallback(); | |
| 143 | |
| 144 if (!expect_fetch_success) { | |
| 145 ASSERT_FALSE(helper.fetcher()); | |
| 146 } else { | |
| 147 ASSERT_TRUE(helper.fetcher()); | |
| 148 URLResponsePtr response = helper.fetcher()->AsURLResponse(nullptr, 0); | |
| 149 ASSERT_TRUE(response); | |
| 150 EXPECT_EQ(url, response->url); | |
| 151 EXPECT_EQ(expected_status_code, response->status_code); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 private: | |
| 156 base::ShadowingAtExitManager at_exit_; | |
| 157 base::MessageLoop loop_; | |
| 158 URLLoaderFactoryPtr url_loader_factory_; | |
| 159 | |
| 160 DISALLOW_COPY_AND_ASSIGN(NetworkFetcherTest); | |
| 161 }; | |
| 162 | |
| 163 TEST_F(NetworkFetcherTest, FetchSucceeded200) { | |
| 164 TestFetchURL(k200Request, true, 200u); | |
| 165 } | |
| 166 | |
| 167 TEST_F(NetworkFetcherTest, FetchSucceeded404) { | |
| 168 TestFetchURL(k404Request, true, 404u); | |
| 169 } | |
| 170 | |
| 171 TEST_F(NetworkFetcherTest, FetchSucceeded504) { | |
| 172 TestFetchURL(k504Request, true, 504u); | |
| 173 } | |
| 174 | |
| 175 TEST_F(NetworkFetcherTest, FetchFailed) { | |
| 176 TestFetchURL(kErrorRequest, false, 0u); | |
| 177 } | |
| 178 | |
| 179 } // namespace | |
| 180 } // namespace shell | |
| 181 } // namespace mojo | |
| OLD | NEW |