Chromium Code Reviews| 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.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 else | |
|
Tom Sepez
2014/11/17 18:09:11
nit: else after return.
jbroman
2014/11/17 18:35:16
Done.
| |
| 35 return ContentClient::GetDataResource(resource_id, scale_factor); | |
| 36 } | |
| 37 }; | |
| 38 | |
| 39 class ResourceProtocolTest : public ::testing::Test { | |
| 40 protected: | |
| 41 ResourceProtocolTest() : context_(true) { | |
| 42 scoped_ptr<ResourceProtocolHandler> handler( | |
| 43 new ResourceProtocolHandler(&content_client_)); | |
| 44 handler->RegisterResource(kHelloWorldHost, kHelloWorldPath, | |
| 45 kHelloWorldResourceId, ui::SCALE_FACTOR_NONE, | |
| 46 kTextPlain); | |
| 47 job_factory_.SetProtocolHandler(kResourceScheme, handler.release()); | |
| 48 context_.set_job_factory(&job_factory_); | |
| 49 context_.Init(); | |
| 50 } | |
| 51 | |
| 52 void StartRequest(const char* path) { | |
| 53 GURL url(base::StringPrintf("%s://%s%s", kResourceScheme, kHelloWorldHost, | |
| 54 path)); | |
| 55 request_ = | |
| 56 context_.CreateRequest(url, net::DEFAULT_PRIORITY, &delegate_, nullptr); | |
| 57 request_->Start(); | |
| 58 EXPECT_TRUE(request_->is_pending()); | |
| 59 base::RunLoop().Run(); | |
| 60 EXPECT_FALSE(request_->is_pending()); | |
| 61 } | |
| 62 | |
| 63 base::MessageLoop message_loop_; | |
| 64 HelloWorldContentClient content_client_; | |
| 65 net::URLRequestJobFactoryImpl job_factory_; | |
| 66 net::TestURLRequestContext context_; | |
| 67 net::TestDelegate delegate_; | |
| 68 scoped_ptr<net::URLRequest> request_; | |
| 69 }; | |
| 70 | |
| 71 TEST_F(ResourceProtocolTest, ValidResource) { | |
| 72 StartRequest(kHelloWorldPath); | |
| 73 ASSERT_TRUE(request_->status().is_success()); | |
| 74 EXPECT_EQ(kHelloWorld, delegate_.data_received()); | |
| 75 } | |
| 76 | |
| 77 TEST_F(ResourceProtocolTest, InvalidResource) { | |
| 78 StartRequest(kInvalidPath); | |
| 79 EXPECT_TRUE(delegate_.request_failed()); | |
| 80 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | |
| 81 } | |
| 82 | |
| 83 } // namespace | |
| 84 } // namespace content | |
| OLD | NEW |