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

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 analyze 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
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/browser/browser_thread.h"
14 #include "content/public/common/content_paths.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "mojo/public/c/system/data_pipe.h"
17 #include "mojo/public/cpp/system/wait.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace content {
22
23 namespace {
24
25 static ResourceRequest CreateResourceRequest(const char* method,
26 ResourceType type,
27 const GURL& url) {
28 ResourceRequest request;
29 request.method = std::string(method);
30 request.url = url;
31 request.first_party_for_cookies = url; // bypass third-party cookie blocking
32 request.request_initiator = url::Origin(url); // ensure initiator is set
33 request.referrer_policy = blink::kWebReferrerPolicyDefault;
34 request.load_flags = 0;
35 request.origin_pid = 0;
36 request.resource_type = type;
37 request.request_context = 0;
38 request.appcache_host_id = kAppCacheNoHostId;
39 request.download_to_file = false;
40 request.should_reset_appcache = false;
41 request.is_main_frame = true;
42 request.parent_is_main_frame = false;
43 request.transition_type = ui::PAGE_TRANSITION_LINK;
44 request.allow_download = true;
45 return request;
46 }
47
48 std::string ReadData(MojoHandle consumer, size_t size) {
49 CHECK_EQ(mojo::Wait(mojo::Handle(consumer), MOJO_HANDLE_SIGNAL_READABLE),
50 MOJO_RESULT_OK);
51 std::vector<char> buffer(size);
52 uint32_t num_bytes = static_cast<uint32_t>(size);
53 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
54 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE),
55 MOJO_RESULT_OK);
56 CHECK_EQ(num_bytes, static_cast<uint32_t>(size));
57
58 return std::string(buffer.data(), buffer.size());
59 }
60
61 } // namespace
62
63 class URLLoaderImplTest : public testing::Test {
64 public:
65 URLLoaderImplTest() : io_browser_thread_(BrowserThread::IO, &message_loop_) {}
66 ~URLLoaderImplTest() override {}
67
68 void SetUp() override {
69 test_server_.ServeFilesFromSourceDirectory(
70 base::FilePath(FILE_PATH_LITERAL("content/test/data")));
71 test_server_.Start();
72 }
73
74 void LoadAndCompareFile(const std::string& path) {
75 TestURLLoaderClient client;
76 mojom::URLLoaderPtr loader;
77
78 ResourceRequest request =
79 CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME,
80 test_server()->GetURL(std::string("/") + path));
81
82 URLLoaderImpl loader_impl(context(), mojo::MakeRequest(&loader), request,
83 client.CreateInterfacePtr());
84
85 client.RunUntilComplete();
86
87 base::FilePath file;
88 PathService::Get(content::DIR_TEST_DATA, &file);
89 file = file.AppendASCII(path);
90
91 std::string file_contents;
92 if (!base::ReadFileToString(file, &file_contents)) {
93 ADD_FAILURE() << "File not found: " << file.value();
94 return;
95 }
96
97 std::string data =
98 ReadData(client.response_body().value(), file_contents.size());
99 CHECK_EQ(data, file_contents);
100 }
101
102 net::EmbeddedTestServer* test_server() { return &test_server_; }
103 NetworkContext* context() { return &context_; }
104
105 private:
106 base::MessageLoop message_loop_;
107 TestBrowserThread io_browser_thread_;
108 net::EmbeddedTestServer test_server_;
109 NetworkContext context_;
110 };
111
112 TEST_F(URLLoaderImplTest, Basic) {
113 LoadAndCompareFile("simple_page.html");
114 }
115
116 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698