Index: content/browser/loader/buffered_resource_handler_unittest.cc |
diff --git a/content/browser/loader/buffered_resource_handler_unittest.cc b/content/browser/loader/buffered_resource_handler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a938fba3ccb1eb6e31e5730a511ff568c6548a16 |
--- /dev/null |
+++ b/content/browser/loader/buffered_resource_handler_unittest.cc |
@@ -0,0 +1,236 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/loader/buffered_resource_handler.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "content/browser/loader/resource_dispatcher_host_impl.h" |
+#include "content/public/browser/resource_dispatcher_host_delegate.h" |
+#include "content/public/browser/resource_request_info.h" |
+#include "content/public/common/resource_response.h" |
+#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.
|
+#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.
|
+#include "net/url_request/url_request_context.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+ |
+namespace content { |
+ |
+class BufferedResourceHandlerTest : public testing::Test { |
+ public: |
+ BufferedResourceHandlerTest() |
+ : stream_has_handler_(false), |
+ intercepted_as_stream_(false) {} |
+ |
+ void set_intercepted_as_stream() { intercepted_as_stream_ = true; } |
+ |
+ void set_stream_has_handler(bool stream_has_handler) { |
+ stream_has_handler_ = stream_has_handler; |
+ } |
+ |
+ bool TestStreamIsIntercepted(bool allow_download, |
+ bool must_download, |
+ ResourceType request_resource_type); |
+ |
+ private: |
+ // Whether the URL request should be intercepted has a stream. |
+ bool stream_has_handler_; |
+ |
+ // Whether the URL request has been intercepted as a stream. |
+ bool intercepted_as_stream_; |
+ |
+ base::MessageLoop message_loop_; |
mmenke
2014/11/18 16:32:17
Should include message loop header.
raymes
2014/11/19 04:08:37
Done.
|
+}; |
+ |
+class TestResourceHandler : public ResourceHandler { |
+ public: |
+ TestResourceHandler() : ResourceHandler(nullptr) {} |
mmenke
2014/11/18 16:32:17
nit: Blank lines between methods.
raymes
2014/11/19 04:08:37
Done.
|
+ void SetController(ResourceController* controller) override {} |
+ bool OnUploadProgress(uint64 position, uint64 size) override { |
+ return false; |
+ } |
+ |
+ bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
+ ResourceResponse* response, |
+ bool* defer) override { |
+ return false; |
+ } |
+ |
+ bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
+ return false; |
+ } |
+ |
+ bool OnWillStart(const GURL& url, bool* defer) override { |
+ return false; |
+ } |
+ |
+ bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { |
+ return false; |
+ } |
+ |
+ bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
+ int* buf_size, |
+ int min_size) override { |
+ return false; |
+ } |
+ |
+ 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.
|
+ return false; |
+ } |
+ |
+ void OnResponseCompleted(const net::URLRequestStatus& status, |
+ const std::string& security_info, |
+ bool* defer) override {} |
+ |
+ 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.
|
+}; |
+ |
+class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { |
+ public: |
+ TestResourceDispatcherHost(BufferedResourceHandlerTest* test, |
+ bool stream_has_handler) |
+ : stream_has_handler_(stream_has_handler), |
+ test_(test) {} |
+ |
+ scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload( |
+ net::URLRequest* request, |
+ bool is_content_initiated, |
+ bool must_download, |
+ uint32 id, |
+ scoped_ptr<DownloadSaveInfo> save_info, |
+ const DownloadUrlParameters::OnStartedCallback& started_cb) override { |
+ return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
+ } |
+ |
+ scoped_ptr<ResourceHandler> MaybeInterceptAsStream( |
+ net::URLRequest* request, |
+ ResourceResponse* response, |
+ std::string* payload) override { |
+ if (stream_has_handler_) { |
+ 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.
|
+ return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
+ } else { |
+ return scoped_ptr<ResourceHandler>(); |
+ } |
+ } |
+ |
+ private: |
+ bool stream_has_handler_; |
+ BufferedResourceHandlerTest* test_; |
+}; |
+ |
+class TestResourceDispatcherHostDelegate |
+ : public ResourceDispatcherHostDelegate { |
+ public: |
+ TestResourceDispatcherHostDelegate(bool must_download) |
+ : must_download_(must_download) { |
+ } |
+ |
+ bool ShouldForceDownloadResource(const GURL& url, |
+ const std::string& mime_type) override { |
+ return must_download_; |
+ } |
+ |
+ private: |
+ const bool must_download_; |
+}; |
+ |
+bool BufferedResourceHandlerTest::TestStreamIsIntercepted( |
+ bool allow_download, |
+ bool must_download, |
+ ResourceType request_resource_type) { |
+ intercepted_as_stream_ = false; |
+ |
+ net::URLRequestContext context; |
+ scoped_ptr<net::URLRequest> request(context.CreateRequest( |
+ GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); |
+ ResourceRequestInfo::AllocateForTesting( |
+ request.get(), request_resource_type, nullptr, 0, 0, 0, false, false, |
+ allow_download, true); |
+ |
+ TestResourceDispatcherHost host(this, stream_has_handler_); |
+ TestResourceDispatcherHostDelegate host_delegate(must_download); |
+ host.SetDelegate(&host_delegate); |
+ |
+ scoped_ptr<ResourceHandler> buffered_handler( |
+ new BufferedResourceHandler( |
+ scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), |
+ &host, request.get())); |
+ |
+ scoped_refptr<ResourceResponse> response(new ResourceResponse); |
+ // The MIME type isn't important but it shouldn't be empty. |
+ response->head.mime_type = "application/pdf"; |
+ |
+ bool defer = false; |
+ buffered_handler->OnResponseStarted(response.get(), &defer); |
+ |
+ return intercepted_as_stream_; |
+} |
+ |
+// Test that stream requests are correctly intercepted under the right |
+// circumstances. |
+TEST_F(BufferedResourceHandlerTest, StreamHandling) { |
+ bool allow_download; |
+ bool must_download; |
+ ResourceType resource_type; |
+ |
+ // Ensure the stream is handled by MaybeInterceptAsStream in the |
+ // ResourceDispatcherHost. |
+ set_stream_has_handler(true); |
+ |
+ // Main frame request with no download allowed. Stream shouldn't be |
+ // intercepted. |
+ allow_download = false; |
+ must_download = false; |
+ resource_type = RESOURCE_TYPE_MAIN_FRAME; |
+ ASSERT_FALSE( |
+ TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
+ |
+ // Main frame request with download allowed. Stream should be intercepted. |
+ allow_download = true; |
+ must_download = false; |
+ resource_type = RESOURCE_TYPE_MAIN_FRAME; |
+ ASSERT_TRUE( |
+ TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
+ |
+ // Main frame request with download forced. Stream shouldn't be intercepted. |
+ allow_download = true; |
+ must_download = true; |
+ resource_type = RESOURCE_TYPE_MAIN_FRAME; |
+ ASSERT_FALSE( |
+ TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
+ |
+ // Sub-resource request with download not allowed. Stream shouldn't be |
+ // intercepted. |
+ allow_download = false; |
+ must_download = false; |
+ resource_type = RESOURCE_TYPE_SUB_RESOURCE; |
+ ASSERT_FALSE( |
+ TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
+ |
+ // Object request with download not allowed. Stream should be intercepted. |
+ allow_download = false; |
+ must_download = false; |
+ resource_type = RESOURCE_TYPE_OBJECT; |
+ ASSERT_TRUE( |
+ TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
+ |
+ // Test the cases where the stream isn't handled by MaybeInterceptAsStream |
+ // in the ResourceDispatcherHost. |
+ set_stream_has_handler(false); |
+ |
+ allow_download = false; |
+ must_download = false; |
+ resource_type = RESOURCE_TYPE_OBJECT; |
+ ASSERT_FALSE( |
mmenke
2014/11/18 16:32:17
I think these can all be EXPECTs.
raymes
2014/11/19 04:08:37
Done.
|
+ TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
+ |
+ allow_download = true; |
+ must_download = false; |
+ resource_type = RESOURCE_TYPE_MAIN_FRAME; |
+ ASSERT_FALSE( |
+ TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
+} |
+ |
+} // namespace content |