Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(481)

Side by Side Diff: content/network/url_loader_unittest.cc

Issue 2817453002: Bring back the URLLoader from the old Mandoline network service. (Closed)
Patch Set: fix checkdeps Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/network/url_loader_impl.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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/files/file_util.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/path_service.h"
8 #include "base/run_loop.h"
9 #include "base/threading/thread_task_runner_handle.h"
10 #include "content/browser/loader/test_url_loader_client.h"
11 #include "content/network/network_context.h"
12 #include "content/network/url_loader_impl.h"
13 #include "content/public/common/content_paths.h"
14 #include "mojo/public/c/system/data_pipe.h"
15 #include "mojo/public/cpp/system/wait.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace content {
20
21 namespace {
22
23 static ResourceRequest CreateResourceRequest(const char* method,
24 ResourceType type,
25 const GURL& url) {
26 ResourceRequest request;
27 request.method = std::string(method);
28 request.url = url;
29 request.first_party_for_cookies = url; // bypass third-party cookie blocking
30 request.request_initiator = url::Origin(url); // ensure initiator is set
31 request.referrer_policy = blink::kWebReferrerPolicyDefault;
32 request.load_flags = 0;
33 request.origin_pid = 0;
34 request.resource_type = type;
35 request.request_context = 0;
36 request.appcache_host_id = kAppCacheNoHostId;
37 request.download_to_file = false;
38 request.should_reset_appcache = false;
39 request.is_main_frame = true;
40 request.parent_is_main_frame = false;
41 request.transition_type = ui::PAGE_TRANSITION_LINK;
42 request.allow_download = true;
43 return request;
44 }
45
46 std::string ReadData(MojoHandle consumer, size_t size) {
47 CHECK_EQ(mojo::Wait(mojo::Handle(consumer), MOJO_HANDLE_SIGNAL_READABLE),
48 MOJO_RESULT_OK);
49 std::vector<char> buffer(size);
50 uint32_t num_bytes = static_cast<uint32_t>(size);
51 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes,
52 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE),
53 MOJO_RESULT_OK);
54 CHECK_EQ(num_bytes, static_cast<uint32_t>(size));
55
56 return std::string(buffer.data(), buffer.size());
57 }
58
59 } // namespace
60
61 class URLLoaderImplTest : public testing::Test {
62 public:
63 URLLoaderImplTest() {}
64 ~URLLoaderImplTest() override {}
65
66 void SetUp() override {
67 test_server_.ServeFilesFromSourceDirectory(
68 base::FilePath(FILE_PATH_LITERAL("content/test/data")));
69 ASSERT_TRUE(test_server_.Start());
70 }
71
72 void LoadAndCompareFile(const std::string& path) {
73 TestURLLoaderClient client;
74 mojom::URLLoaderPtr loader;
75
76 ResourceRequest request =
77 CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME,
78 test_server()->GetURL(std::string("/") + path));
79
80 URLLoaderImpl loader_impl(context(), mojo::MakeRequest(&loader), request,
81 client.CreateInterfacePtr());
82
83 client.RunUntilComplete();
84
85 base::FilePath file;
86 PathService::Get(content::DIR_TEST_DATA, &file);
87 file = file.AppendASCII(path);
88
89 std::string file_contents;
90 if (!base::ReadFileToString(file, &file_contents)) {
91 ADD_FAILURE() << "File not found: " << file.value();
92 return;
93 }
94
95 std::string data =
96 ReadData(client.response_body().value(), file_contents.size());
97 CHECK_EQ(data, file_contents);
98 }
99
100 net::EmbeddedTestServer* test_server() { return &test_server_; }
101 NetworkContext* context() { return &context_; }
102
103 private:
104 base::MessageLoopForIO message_loop_;
105 net::EmbeddedTestServer test_server_;
106 NetworkContext context_;
107 };
108
109 TEST_F(URLLoaderImplTest, Basic) {
110 LoadAndCompareFile("simple_page.html");
111 }
112
113 } // namespace content
OLDNEW
« no previous file with comments | « content/network/url_loader_impl.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698