Chromium Code Reviews| Index: content/network/url_loader_unittest.cc |
| diff --git a/content/network/url_loader_unittest.cc b/content/network/url_loader_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f5059a049d19bf1289a0c257522766764e468657 |
| --- /dev/null |
| +++ b/content/network/url_loader_unittest.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/files/file_util.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/path_service.h" |
| +#include "base/run_loop.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "content/browser/loader/test_url_loader_client.h" |
| +#include "content/network/network_context.h" |
| +#include "content/network/url_loader_impl.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/common/content_paths.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "mojo/public/c/system/data_pipe.h" |
| +#include "mojo/public/cpp/system/wait.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +static ResourceRequest CreateResourceRequest(const char* method, |
| + ResourceType type, |
| + const GURL& url) { |
| + ResourceRequest request; |
| + request.method = std::string(method); |
| + request.url = url; |
| + request.first_party_for_cookies = url; // bypass third-party cookie blocking |
| + request.request_initiator = url::Origin(url); // ensure initiator is set |
| + request.referrer_policy = blink::kWebReferrerPolicyDefault; |
| + request.load_flags = 0; |
| + request.origin_pid = 0; |
| + request.resource_type = type; |
| + request.request_context = 0; |
| + request.appcache_host_id = kAppCacheNoHostId; |
| + request.download_to_file = false; |
| + request.should_reset_appcache = false; |
| + request.is_main_frame = true; |
| + request.parent_is_main_frame = false; |
| + request.transition_type = ui::PAGE_TRANSITION_LINK; |
| + request.allow_download = true; |
| + return request; |
| +} |
| + |
| +std::string ReadData(MojoHandle consumer, size_t size) { |
| + CHECK_EQ(mojo::Wait(mojo::Handle(consumer), MOJO_HANDLE_SIGNAL_READABLE), |
| + MOJO_RESULT_OK); |
| + std::vector<char> buffer(size); |
| + uint32_t num_bytes = static_cast<uint32_t>(size); |
| + CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes, |
|
yzshen1
2017/04/11 21:58:04
FYI, although this probably always works, there is
jam
2017/04/11 22:58:19
Are you sure how it's used here can fail? Given th
yzshen1
2017/04/11 23:22:43
I am pretty sure that this relies on some undocume
|
| + MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), |
| + MOJO_RESULT_OK); |
| + CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); |
| + |
| + return std::string(buffer.data(), buffer.size()); |
| +} |
| + |
| +} // namespace |
| + |
| +class URLLoaderImplTest : public testing::Test { |
| + public: |
| + URLLoaderImplTest() : io_browser_thread_(BrowserThread::IO, &message_loop_) {} |
| + ~URLLoaderImplTest() override {} |
| + |
| + void SetUp() override { |
| + test_server_.ServeFilesFromSourceDirectory( |
| + base::FilePath(FILE_PATH_LITERAL("content/test/data"))); |
| + test_server_.Start(); |
| + } |
| + |
| + void LoadAndCompareFile(const std::string& path) { |
| + TestURLLoaderClient client; |
| + mojom::URLLoaderPtr loader; |
| + |
| + ResourceRequest request = |
| + CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME, |
| + test_server()->GetURL(std::string("/") + path)); |
| + |
| + URLLoaderImpl loader_impl(context(), mojo::MakeRequest(&loader), request, |
| + client.CreateInterfacePtr()); |
| + |
| + client.RunUntilComplete(); |
| + |
| + base::FilePath file; |
| + PathService::Get(content::DIR_TEST_DATA, &file); |
| + file = file.AppendASCII(path); |
| + |
| + std::string file_contents; |
| + if (!base::ReadFileToString(file, &file_contents)) { |
| + ADD_FAILURE() << "File not found: " << file.value(); |
| + return; |
| + } |
| + |
| + std::string data = |
| + ReadData(client.response_body().value(), file_contents.size()); |
| + CHECK_EQ(data, file_contents); |
| + } |
| + |
| + net::EmbeddedTestServer* test_server() { return &test_server_; } |
| + NetworkContext* context() { return &context_; } |
| + |
| + private: |
| + base::MessageLoop message_loop_; |
| + TestBrowserThread io_browser_thread_; |
| + net::EmbeddedTestServer test_server_; |
| + NetworkContext context_; |
| +}; |
| + |
| +TEST_F(URLLoaderImplTest, Basic) { |
| + LoadAndCompareFile("simple_page.html"); |
| +} |
| + |
| +} // namespace content |