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

Side by Side Diff: content/browser/loader/buffered_resource_handler_unittest.cc

Issue 694773003: Allow URL requests for object/embed tags to be intercepted as streams. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-guest-view-container-3
Patch Set: Created 6 years, 1 month 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 2014 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 "content/browser/loader/buffered_resource_handler.h"
6 #include "content/browser/loader/resource_dispatcher_host_impl.h"
7 #include "content/public/browser/resource_dispatcher_host_delegate.h"
8 #include "content/public/browser/resource_request_info.h"
9 #include "content/public/common/resource_response.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "content/public/test/test_utils.h"
12 #include "net/url_request/url_request_context.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace content {
16
17 class BufferedResourceHandlerTest : public testing::Test {
18 public:
19 BufferedResourceHandlerTest() : intercepted_as_stream_(false) {}
20 void set_intercepted_as_stream() { intercepted_as_stream_ = true; }
21
22 bool TestStreamIsIntercepted(bool allow_download,
23 bool must_download,
24 ResourceType request_resource_type);
25
26 private:
27 // Whether the URL request has been intercepted as a stream.
28 bool intercepted_as_stream_;
29
30 TestBrowserThreadBundle thread_bundle_;
31 };
32
33 class TestResourceHandler : public ResourceHandler {
34 public:
35 TestResourceHandler() : ResourceHandler(nullptr) {}
36 void SetController(ResourceController* controller) override {}
37 bool OnUploadProgress(uint64 position, uint64 size) override {
38 return false;
39 }
40 bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
41 ResourceResponse* response,
42 bool* defer) override {
43 return false;
44 }
45 bool OnResponseStarted(ResourceResponse* response, bool* defer) override {
46 return false;
47 }
48 bool OnWillStart(const GURL& url, bool* defer) override {
49 return false;
50 }
51 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override {
52 return false;
53 }
54 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
55 int* buf_size,
56 int min_size) override {
57 return false;
58 }
59 bool OnReadCompleted(int bytes_read, bool* defer) override {
60 return false;
61 }
62 void OnResponseCompleted(const net::URLRequestStatus& status,
63 const std::string& security_info,
64 bool* defer) override {}
65 void OnDataDownloaded(int bytes_downloaded) override {}
66 };
67
68 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
69 public:
70 TestResourceDispatcherHost(BufferedResourceHandlerTest* test) : test_(test) {}
71 scoped_ptr<ResourceHandler> MaybeInterceptAsStream(
72 net::URLRequest* request,
73 ResourceResponse* response,
74 std::string* payload) override {
75 test_->set_intercepted_as_stream();
76 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
77 }
78
79 private:
80 BufferedResourceHandlerTest* test_;
81 };
82
83 class TestResourceDispatcherHostDelegate
84 : public ResourceDispatcherHostDelegate {
85 public:
86 TestResourceDispatcherHostDelegate(bool must_download)
87 : must_download_(must_download) {
88 }
89
90 bool ShouldForceDownloadResource(const GURL& url,
91 const std::string& mime_type) override {
92 return must_download_;
93 }
94
95 private:
96 bool must_download_;
97 };
98
99 bool BufferedResourceHandlerTest::TestStreamIsIntercepted(
100 bool allow_download,
101 bool must_download,
102 ResourceType request_resource_type) {
103 // In the implementation |allow_download| == true implies
104 // |request_resource_type| == RESOURCE_TYPE_MAIN_FRAME or
105 // |request_resource_type| == RESOURCE_TYPE_SUB_FRAME.
106 if (allow_download) {
107 CHECK(request_resource_type == RESOURCE_TYPE_MAIN_FRAME ||
108 request_resource_type == RESOURCE_TYPE_SUB_FRAME);
109 }
110 intercepted_as_stream_ = false;
111
112 net::URLRequestContext context;
113 scoped_ptr<net::URLRequest> request(context.CreateRequest(
114 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr));
115 ResourceRequestInfo::AllocateForTesting(request.get(),
116 request_resource_type, nullptr, 0, 0, 0, allow_download, true);
117
118 TestResourceDispatcherHost host(this);
119 TestResourceDispatcherHostDelegate host_delegate(must_download);
120 host.SetDelegate(&host_delegate);
121
122 scoped_ptr<ResourceHandler> buffered_handler(
123 new BufferedResourceHandler(
124 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(),
125 &host, request.get()));
126
127 scoped_refptr<ResourceResponse> response(new ResourceResponse);
128 // The MIME type isn't important but it shouldn't be empty.
129 response->head.mime_type = "application/pdf";
130
131 bool defer = true;
132 buffered_handler->OnResponseStarted(response.get(), &defer);
133
134 // Run the message loop so that tasks don't get run after destruction.
135 content::RunAllPendingInMessageLoop();
136
137 return intercepted_as_stream_;
138 }
139
140 // Test that stream requests are correctly intercepted under the right
141 // circumstances.
142 TEST_F(BufferedResourceHandlerTest, StreamHandling) {
143 bool allow_download;
144 bool must_download;
145 ResourceType resource_type;
146
147 // Main frame request with no download allowed. Stream shouldn't be
148 // intercepted.
149 allow_download = false;
150 must_download = false;
151 resource_type = RESOURCE_TYPE_MAIN_FRAME;
152 ASSERT_FALSE(
153 TestStreamIsIntercepted(allow_download, must_download, resource_type));
154
155 // Main frame request with download allowed. Stream should be intercepted.
156 allow_download = true;
157 must_download = false;
158 resource_type = RESOURCE_TYPE_MAIN_FRAME;
159 ASSERT_TRUE(
160 TestStreamIsIntercepted(allow_download, must_download, resource_type));
161
162 // Main frame request with download forced. Stream shouldn't be intercepted.
163 allow_download = true;
164 must_download = true;
165 resource_type = RESOURCE_TYPE_MAIN_FRAME;
166 ASSERT_FALSE(
167 TestStreamIsIntercepted(allow_download, must_download, resource_type));
168
169 // Sub-resource request with download not allowed. Stream shouldn't be
170 // intercepted.
171 allow_download = false;
172 must_download = false;
173 resource_type = RESOURCE_TYPE_SUB_RESOURCE;
174 ASSERT_FALSE(
175 TestStreamIsIntercepted(allow_download, must_download, resource_type));
176
177 // Object request with download not allowed. Stream should be intercepted.
178 allow_download = false;
179 must_download = false;
180 resource_type = RESOURCE_TYPE_OBJECT;
181 ASSERT_TRUE(
182 TestStreamIsIntercepted(allow_download, must_download, resource_type));
183 }
184
185 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698