OLD | NEW |
---|---|
(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" | |
mmenke
2014/11/17 18:57:08
optional nit: I believe the corresponding header
raymes
2014/11/17 23:44:25
Yes, I've seen lots of inconsistency in that. I ad
| |
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) {} | |
mmenke
2014/11/17 18:57:08
nit: Blank line after constructor.
raymes
2014/11/17 23:44:25
Done.
| |
20 void set_intercepted_as_stream() { intercepted_as_stream_ = true; } | |
21 | |
22 void set_stream_has_handler(bool stream_has_handler) { | |
23 stream_has_handler_ = stream_has_handler; | |
24 } | |
25 | |
26 bool TestStreamIsIntercepted(bool allow_download, | |
27 bool must_download, | |
28 ResourceType request_resource_type); | |
29 | |
30 private: | |
31 // Whether the URL request has been intercepted as a stream. | |
32 bool intercepted_as_stream_; | |
33 | |
34 TestBrowserThreadBundle thread_bundle_; | |
35 }; | |
36 | |
37 class TestResourceHandler : public ResourceHandler { | |
38 public: | |
39 TestResourceHandler() : ResourceHandler(nullptr) {} | |
mmenke
2014/11/17 18:57:08
nit: Blank lines between methods.
raymes
2014/11/17 23:44:25
Done.
| |
40 void SetController(ResourceController* controller) override {} | |
41 bool OnUploadProgress(uint64 position, uint64 size) override { | |
42 return false; | |
43 } | |
44 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
45 ResourceResponse* response, | |
46 bool* defer) override { | |
47 return false; | |
48 } | |
49 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | |
50 return false; | |
51 } | |
52 bool OnWillStart(const GURL& url, bool* defer) override { | |
53 return false; | |
54 } | |
55 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { | |
56 return false; | |
57 } | |
58 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
59 int* buf_size, | |
60 int min_size) override { | |
61 return false; | |
62 } | |
63 bool OnReadCompleted(int bytes_read, bool* defer) override { | |
64 return false; | |
65 } | |
66 void OnResponseCompleted(const net::URLRequestStatus& status, | |
67 const std::string& security_info, | |
68 bool* defer) override {} | |
69 void OnDataDownloaded(int bytes_downloaded) override {} | |
70 }; | |
71 | |
72 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { | |
73 public: | |
74 TestResourceDispatcherHost(BufferedResourceHandlerTest* test, | |
75 bool stream_has_handler) | |
76 : stream_has_handler_(stream_has_handler), | |
77 test_(test) {} | |
78 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( | |
mmenke
2014/11/17 18:57:08
nit: blank line between methods.
raymes
2014/11/17 23:44:25
Done.
| |
79 net::URLRequest* request, | |
80 ResourceResponse* response, | |
81 std::string* payload) override { | |
82 if (stream_has_handler_) { | |
83 test_->set_intercepted_as_stream(); | |
84 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | |
85 } else { | |
86 return scoped_ptr<ResourceHandler>(); | |
87 } | |
88 } | |
89 | |
90 private: | |
91 BufferedResourceHandlerTest* test_; | |
92 bool stream_has_handler_; | |
93 }; | |
94 | |
95 class TestResourceDispatcherHostDelegate | |
96 : public ResourceDispatcherHostDelegate { | |
97 public: | |
98 TestResourceDispatcherHostDelegate(bool must_download) | |
99 : must_download_(must_download) { | |
100 } | |
101 | |
102 bool ShouldForceDownloadResource(const GURL& url, | |
103 const std::string& mime_type) override { | |
104 return must_download_; | |
105 } | |
106 | |
107 private: | |
108 bool must_download_; | |
mmenke
2014/11/17 18:57:08
nit: const?
raymes
2014/11/17 23:44:25
Done.
| |
109 }; | |
110 | |
111 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( | |
112 bool allow_download, | |
113 bool must_download, | |
114 ResourceType request_resource_type) { | |
115 // In the implementation |allow_download| == true implies | |
116 // |request_resource_type| == RESOURCE_TYPE_MAIN_FRAME or | |
117 // |request_resource_type| == RESOURCE_TYPE_SUB_FRAME. | |
118 if (allow_download) { | |
119 CHECK(request_resource_type == RESOURCE_TYPE_MAIN_FRAME || | |
120 request_resource_type == RESOURCE_TYPE_SUB_FRAME); | |
mmenke
2014/11/17 18:57:08
Include base/logging.h, for CHECK...Actually, shou
raymes
2014/11/17 23:44:25
Done, I moved it into buffered_resource_handler.cc
| |
121 } | |
122 intercepted_as_stream_ = false; | |
123 | |
124 net::URLRequestContext context; | |
125 scoped_ptr<net::URLRequest> request(context.CreateRequest( | |
126 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); | |
mmenke
2014/11/17 18:57:08
include base/scoped_ptr.h and url/gurl.h
raymes
2014/11/17 23:44:25
Done.
| |
127 ResourceRequestInfo::AllocateForTesting(request.get(), | |
mmenke
2014/11/17 18:57:08
request.get(), should be moved onto next line, ali
raymes
2014/11/17 23:44:25
Done.
| |
128 request_resource_type, nullptr, 0, 0, 0, allow_download, true); | |
129 | |
130 TestResourceDispatcherHost host(this, stream_has_handler_); | |
131 TestResourceDispatcherHostDelegate host_delegate(must_download); | |
132 host.SetDelegate(&host_delegate); | |
133 | |
134 scoped_ptr<ResourceHandler> buffered_handler( | |
135 new BufferedResourceHandler( | |
136 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), | |
137 &host, request.get())); | |
138 | |
139 scoped_refptr<ResourceResponse> response(new ResourceResponse); | |
140 // The MIME type isn't important but it shouldn't be empty. | |
141 response->head.mime_type = "application/pdf"; | |
142 | |
143 bool defer = true; | |
mmenke
2014/11/17 18:57:08
defer should always be initialized to false, to mi
raymes
2014/11/17 23:44:25
Done.
| |
144 buffered_handler->OnResponseStarted(response.get(), &defer); | |
145 | |
146 // Run the message loop so that tasks don't get run after destruction. | |
147 content::RunAllPendingInMessageLoop(); | |
148 | |
149 return intercepted_as_stream_; | |
150 } | |
151 | |
152 // Test that stream requests are correctly intercepted under the right | |
153 // circumstances. | |
154 TEST_F(BufferedResourceHandlerTest, StreamHandling) { | |
155 bool allow_download; | |
156 bool must_download; | |
157 ResourceType resource_type; | |
158 | |
159 // Ensure the stream is handled by MaybeInterceptAsStream in the | |
160 // ResourceDispatcherHost. | |
161 set_stream_has_handler(true); | |
162 | |
163 // Main frame request with no download allowed. Stream shouldn't be | |
164 // intercepted. | |
165 allow_download = false; | |
166 must_download = false; | |
167 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
168 ASSERT_FALSE( | |
169 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
170 | |
171 // Main frame request with download allowed. Stream should be intercepted. | |
172 allow_download = true; | |
173 must_download = false; | |
174 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
175 ASSERT_TRUE( | |
176 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
177 | |
178 // Main frame request with download forced. Stream shouldn't be intercepted. | |
179 allow_download = true; | |
180 must_download = true; | |
181 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
182 ASSERT_FALSE( | |
183 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
184 | |
185 // Sub-resource request with download not allowed. Stream shouldn't be | |
186 // intercepted. | |
187 allow_download = false; | |
188 must_download = false; | |
189 resource_type = RESOURCE_TYPE_SUB_RESOURCE; | |
190 ASSERT_FALSE( | |
191 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
192 | |
193 // Object request with download not allowed. Stream should be intercepted. | |
194 allow_download = false; | |
195 must_download = false; | |
196 resource_type = RESOURCE_TYPE_OBJECT; | |
197 ASSERT_TRUE( | |
198 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
199 | |
200 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | |
201 // in the ResourceDispatcherHost. | |
202 set_stream_has_handler(false); | |
203 allow_download = false; | |
204 must_download = false; | |
205 resource_type = RESOURCE_TYPE_OBJECT; | |
206 ASSERT_FALSE( | |
207 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
208 | |
209 allow_download = true; | |
210 must_download = false; | |
211 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
212 ASSERT_FALSE( | |
213 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
214 } | |
215 | |
216 } // namespace content | |
OLD | NEW |