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