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

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
7 #include "base/memory/scoped_ptr.h"
8 #include "content/browser/loader/resource_dispatcher_host_impl.h"
9 #include "content/public/browser/resource_dispatcher_host_delegate.h"
10 #include "content/public/browser/resource_request_info.h"
11 #include "content/public/common/resource_response.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
mmenke 2014/11/18 16:32:17 Header no longer used
raymes 2014/11/19 04:08:37 Done.
13 #include "content/public/test/test_utils.h"
mmenke 2014/11/18 16:32:17 Is this used?
raymes 2014/11/19 04:08:37 Done.
14 #include "net/url_request/url_request_context.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h"
17
18 namespace content {
19
20 class BufferedResourceHandlerTest : public testing::Test {
21 public:
22 BufferedResourceHandlerTest()
23 : stream_has_handler_(false),
24 intercepted_as_stream_(false) {}
25
26 void set_intercepted_as_stream() { intercepted_as_stream_ = true; }
27
28 void set_stream_has_handler(bool stream_has_handler) {
29 stream_has_handler_ = stream_has_handler;
30 }
31
32 bool TestStreamIsIntercepted(bool allow_download,
33 bool must_download,
34 ResourceType request_resource_type);
35
36 private:
37 // Whether the URL request should be intercepted has a stream.
38 bool stream_has_handler_;
39
40 // Whether the URL request has been intercepted as a stream.
41 bool intercepted_as_stream_;
42
43 base::MessageLoop message_loop_;
mmenke 2014/11/18 16:32:17 Should include message loop header.
raymes 2014/11/19 04:08:37 Done.
44 };
45
46 class TestResourceHandler : public ResourceHandler {
47 public:
48 TestResourceHandler() : ResourceHandler(nullptr) {}
mmenke 2014/11/18 16:32:17 nit: Blank lines between methods.
raymes 2014/11/19 04:08:37 Done.
49 void SetController(ResourceController* controller) override {}
50 bool OnUploadProgress(uint64 position, uint64 size) override {
51 return false;
52 }
53
54 bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
55 ResourceResponse* response,
56 bool* defer) override {
57 return false;
58 }
59
60 bool OnResponseStarted(ResourceResponse* response, bool* defer) override {
61 return false;
62 }
63
64 bool OnWillStart(const GURL& url, bool* defer) override {
65 return false;
66 }
67
68 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override {
69 return false;
70 }
71
72 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
73 int* buf_size,
74 int min_size) override {
75 return false;
76 }
77
78 bool OnReadCompleted(int bytes_read, bool* defer) override {
mmenke 2014/11/18 16:32:17 Methods currently not being tested should probably
raymes 2014/11/19 04:08:37 Done.
79 return false;
80 }
81
82 void OnResponseCompleted(const net::URLRequestStatus& status,
83 const std::string& security_info,
84 bool* defer) override {}
85
86 void OnDataDownloaded(int bytes_downloaded) override {}
mmenke 2014/11/18 16:32:17 private: DISALLOW_COPY_AND_ASSIGN(TestResourceHa
raymes 2014/11/19 04:08:37 Done.
87 };
88
89 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
90 public:
91 TestResourceDispatcherHost(BufferedResourceHandlerTest* test,
92 bool stream_has_handler)
93 : stream_has_handler_(stream_has_handler),
94 test_(test) {}
95
96 scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload(
97 net::URLRequest* request,
98 bool is_content_initiated,
99 bool must_download,
100 uint32 id,
101 scoped_ptr<DownloadSaveInfo> save_info,
102 const DownloadUrlParameters::OnStartedCallback& started_cb) override {
103 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
104 }
105
106 scoped_ptr<ResourceHandler> MaybeInterceptAsStream(
107 net::URLRequest* request,
108 ResourceResponse* response,
109 std::string* payload) override {
110 if (stream_has_handler_) {
111 test_->set_intercepted_as_stream();
mmenke 2014/11/18 16:32:17 nit: Think this makes more sense as an accessor,
raymes 2014/11/19 04:08:37 Well spotted, thanks! Done.
112 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
113 } else {
114 return scoped_ptr<ResourceHandler>();
115 }
116 }
117
118 private:
119 bool stream_has_handler_;
120 BufferedResourceHandlerTest* test_;
121 };
122
123 class TestResourceDispatcherHostDelegate
124 : public ResourceDispatcherHostDelegate {
125 public:
126 TestResourceDispatcherHostDelegate(bool must_download)
127 : must_download_(must_download) {
128 }
129
130 bool ShouldForceDownloadResource(const GURL& url,
131 const std::string& mime_type) override {
132 return must_download_;
133 }
134
135 private:
136 const bool must_download_;
137 };
138
139 bool BufferedResourceHandlerTest::TestStreamIsIntercepted(
140 bool allow_download,
141 bool must_download,
142 ResourceType request_resource_type) {
143 intercepted_as_stream_ = false;
144
145 net::URLRequestContext context;
146 scoped_ptr<net::URLRequest> request(context.CreateRequest(
147 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr));
148 ResourceRequestInfo::AllocateForTesting(
149 request.get(), request_resource_type, nullptr, 0, 0, 0, false, false,
150 allow_download, true);
151
152 TestResourceDispatcherHost host(this, stream_has_handler_);
153 TestResourceDispatcherHostDelegate host_delegate(must_download);
154 host.SetDelegate(&host_delegate);
155
156 scoped_ptr<ResourceHandler> buffered_handler(
157 new BufferedResourceHandler(
158 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(),
159 &host, request.get()));
160
161 scoped_refptr<ResourceResponse> response(new ResourceResponse);
162 // The MIME type isn't important but it shouldn't be empty.
163 response->head.mime_type = "application/pdf";
164
165 bool defer = false;
166 buffered_handler->OnResponseStarted(response.get(), &defer);
167
168 return intercepted_as_stream_;
169 }
170
171 // Test that stream requests are correctly intercepted under the right
172 // circumstances.
173 TEST_F(BufferedResourceHandlerTest, StreamHandling) {
174 bool allow_download;
175 bool must_download;
176 ResourceType resource_type;
177
178 // Ensure the stream is handled by MaybeInterceptAsStream in the
179 // ResourceDispatcherHost.
180 set_stream_has_handler(true);
181
182 // Main frame request with no download allowed. Stream shouldn't be
183 // intercepted.
184 allow_download = false;
185 must_download = false;
186 resource_type = RESOURCE_TYPE_MAIN_FRAME;
187 ASSERT_FALSE(
188 TestStreamIsIntercepted(allow_download, must_download, resource_type));
189
190 // Main frame request with download allowed. Stream should be intercepted.
191 allow_download = true;
192 must_download = false;
193 resource_type = RESOURCE_TYPE_MAIN_FRAME;
194 ASSERT_TRUE(
195 TestStreamIsIntercepted(allow_download, must_download, resource_type));
196
197 // Main frame request with download forced. Stream shouldn't be intercepted.
198 allow_download = true;
199 must_download = true;
200 resource_type = RESOURCE_TYPE_MAIN_FRAME;
201 ASSERT_FALSE(
202 TestStreamIsIntercepted(allow_download, must_download, resource_type));
203
204 // Sub-resource request with download not allowed. Stream shouldn't be
205 // intercepted.
206 allow_download = false;
207 must_download = false;
208 resource_type = RESOURCE_TYPE_SUB_RESOURCE;
209 ASSERT_FALSE(
210 TestStreamIsIntercepted(allow_download, must_download, resource_type));
211
212 // Object request with download not allowed. Stream should be intercepted.
213 allow_download = false;
214 must_download = false;
215 resource_type = RESOURCE_TYPE_OBJECT;
216 ASSERT_TRUE(
217 TestStreamIsIntercepted(allow_download, must_download, resource_type));
218
219 // Test the cases where the stream isn't handled by MaybeInterceptAsStream
220 // in the ResourceDispatcherHost.
221 set_stream_has_handler(false);
222
223 allow_download = false;
224 must_download = false;
225 resource_type = RESOURCE_TYPE_OBJECT;
226 ASSERT_FALSE(
mmenke 2014/11/18 16:32:17 I think these can all be EXPECTs.
raymes 2014/11/19 04:08:37 Done.
227 TestStreamIsIntercepted(allow_download, must_download, resource_type));
228
229 allow_download = true;
230 must_download = false;
231 resource_type = RESOURCE_TYPE_MAIN_FRAME;
232 ASSERT_FALSE(
233 TestStreamIsIntercepted(allow_download, must_download, resource_type));
234 }
235
236 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698