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

Unified Diff: content/browser/resource_protocol_handler_unittest.cc

Issue 647853002: Create a proprietary scheme for loading web-accessible resources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update include guards too 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/resource_protocol_handler.cc ('k') | content/browser/storage_partition_impl_map.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/resource_protocol_handler_unittest.cc
diff --git a/content/browser/resource_protocol_handler_unittest.cc b/content/browser/resource_protocol_handler_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8694c3e3e2662e8836a84f6d5a8d4ba7fcff6513
--- /dev/null
+++ b/content/browser/resource_protocol_handler_unittest.cc
@@ -0,0 +1,83 @@
+// 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 "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "content/browser/resource_protocol_handler.h"
+#include "content/public/common/content_client.h"
+#include "content/public/common/url_constants.h"
+#include "net/url_request/url_request.h"
+#include "net/url_request/url_request_job_factory_impl.h"
+#include "net/url_request/url_request_test_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+namespace {
+
+const char kHelloWorldHost[] = "hello-world";
+const char kHelloWorldPath[] = "/hello.txt";
+const char kInvalidPath[] = "/invalid.txt";
+const int kHelloWorldResourceId = 42;
+const char kHelloWorld[] = "Hello, world!";
+const char kTextPlain[] = "text/plain";
+
+class HelloWorldContentClient : public ContentClient {
+ public:
+ base::StringPiece GetDataResource(
+ int resource_id,
+ ui::ScaleFactor scale_factor) const override {
+ if (resource_id == kHelloWorldResourceId)
+ return kHelloWorld;
+ return ContentClient::GetDataResource(resource_id, scale_factor);
+ }
+};
+
+class ResourceProtocolHandlerTest : public ::testing::Test {
+ protected:
+ ResourceProtocolHandlerTest() : context_(true) {
+ scoped_ptr<ResourceProtocolHandler> handler(
+ new ResourceProtocolHandler(&content_client_));
+ handler->RegisterResource(kHelloWorldHost, kHelloWorldPath,
+ kHelloWorldResourceId, ui::SCALE_FACTOR_NONE,
+ kTextPlain);
+ job_factory_.SetProtocolHandler(kResourceScheme, handler.release());
+ context_.set_job_factory(&job_factory_);
+ context_.Init();
+ }
+
+ void StartRequest(const char* path) {
+ GURL url(base::StringPrintf("%s://%s%s", kResourceScheme, kHelloWorldHost,
+ path));
+ request_ =
+ context_.CreateRequest(url, net::DEFAULT_PRIORITY, &delegate_, nullptr);
+ request_->Start();
+ EXPECT_TRUE(request_->is_pending());
+ base::RunLoop().Run();
+ EXPECT_FALSE(request_->is_pending());
+ }
+
+ base::MessageLoop message_loop_;
+ HelloWorldContentClient content_client_;
+ net::URLRequestJobFactoryImpl job_factory_;
+ net::TestURLRequestContext context_;
+ net::TestDelegate delegate_;
+ scoped_ptr<net::URLRequest> request_;
+};
+
+TEST_F(ResourceProtocolHandlerTest, ValidResource) {
+ StartRequest(kHelloWorldPath);
+ ASSERT_TRUE(request_->status().is_success());
+ EXPECT_EQ(kHelloWorld, delegate_.data_received());
+}
+
+TEST_F(ResourceProtocolHandlerTest, InvalidResource) {
+ StartRequest(kInvalidPath);
+ EXPECT_TRUE(delegate_.request_failed());
+ EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
+}
+
+} // namespace
+} // namespace content
« no previous file with comments | « content/browser/resource_protocol_handler.cc ('k') | content/browser/storage_partition_impl_map.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698