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 "base/memory/scoped_ptr.h" |
| 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/browser/resource_protocol_handler.h" |
| 10 #include "content/public/common/content_client.h" |
| 11 #include "content/public/common/url_constants.h" |
| 12 #include "net/url_request/url_request.h" |
| 13 #include "net/url_request/url_request_job_factory_impl.h" |
| 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace content { |
| 18 namespace { |
| 19 |
| 20 const char kHelloWorldHost[] = "hello-world"; |
| 21 const char kHelloWorldPath[] = "/hello.txt"; |
| 22 const char kInvalidPath[] = "/invalid.txt"; |
| 23 const int kHelloWorldResourceId = 42; |
| 24 const char kHelloWorld[] = "Hello, world!"; |
| 25 const char kTextPlain[] = "text/plain"; |
| 26 |
| 27 class HelloWorldContentClient : public ContentClient { |
| 28 public: |
| 29 base::StringPiece GetDataResource( |
| 30 int resource_id, |
| 31 ui::ScaleFactor scale_factor) const override { |
| 32 if (resource_id == kHelloWorldResourceId) |
| 33 return kHelloWorld; |
| 34 return ContentClient::GetDataResource(resource_id, scale_factor); |
| 35 } |
| 36 }; |
| 37 |
| 38 class ResourceProtocolHandlerTest : public ::testing::Test { |
| 39 protected: |
| 40 ResourceProtocolHandlerTest() : context_(true) { |
| 41 scoped_ptr<ResourceProtocolHandler> handler( |
| 42 new ResourceProtocolHandler(&content_client_)); |
| 43 handler->RegisterResource(kHelloWorldHost, kHelloWorldPath, |
| 44 kHelloWorldResourceId, ui::SCALE_FACTOR_NONE, |
| 45 kTextPlain); |
| 46 job_factory_.SetProtocolHandler(kResourceScheme, handler.release()); |
| 47 context_.set_job_factory(&job_factory_); |
| 48 context_.Init(); |
| 49 } |
| 50 |
| 51 void StartRequest(const char* path) { |
| 52 GURL url(base::StringPrintf("%s://%s%s", kResourceScheme, kHelloWorldHost, |
| 53 path)); |
| 54 request_ = |
| 55 context_.CreateRequest(url, net::DEFAULT_PRIORITY, &delegate_, nullptr); |
| 56 request_->Start(); |
| 57 EXPECT_TRUE(request_->is_pending()); |
| 58 base::RunLoop().Run(); |
| 59 EXPECT_FALSE(request_->is_pending()); |
| 60 } |
| 61 |
| 62 base::MessageLoop message_loop_; |
| 63 HelloWorldContentClient content_client_; |
| 64 net::URLRequestJobFactoryImpl job_factory_; |
| 65 net::TestURLRequestContext context_; |
| 66 net::TestDelegate delegate_; |
| 67 scoped_ptr<net::URLRequest> request_; |
| 68 }; |
| 69 |
| 70 TEST_F(ResourceProtocolHandlerTest, ValidResource) { |
| 71 StartRequest(kHelloWorldPath); |
| 72 ASSERT_TRUE(request_->status().is_success()); |
| 73 EXPECT_EQ(kHelloWorld, delegate_.data_received()); |
| 74 } |
| 75 |
| 76 TEST_F(ResourceProtocolHandlerTest, InvalidResource) { |
| 77 StartRequest(kInvalidPath); |
| 78 EXPECT_TRUE(delegate_.request_failed()); |
| 79 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 } // namespace content |
OLD | NEW |