| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "components/dom_distiller/core/distiller_url_fetcher.h" | 8 #include "components/dom_distiller/core/distiller_url_fetcher.h" |
| 9 #include "net/http/http_status_code.h" | 9 #include "net/http/http_status_code.h" |
| 10 #include "net/url_request/test_url_fetcher_factory.h" | 10 #include "net/url_request/test_url_fetcher_factory.h" |
| 11 #include "net/url_request/url_fetcher.h" | 11 #include "net/url_request/url_fetcher.h" |
| 12 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
| 13 #include "net/url_request/url_request_status.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 const char kTestPageA[] = "http://www.a.com/"; | 17 const char kTestPageA[] = "http://www.a.com/"; |
| 17 const char kTestPageAResponse[] = { 1, 2, 3, 4, 5, 6, 7 }; | 18 const char kTestPageAResponse[] = { 1, 2, 3, 4, 5, 6, 7 }; |
| 18 const char kTestPageB[] = "http://www.b.com/"; | 19 const char kTestPageB[] = "http://www.b.com/"; |
| 19 const char kTestPageBResponse[] = { 'a', 'b', 'c' }; | 20 const char kTestPageBResponse[] = { 'a', 'b', 'c' }; |
| 20 | 21 |
| 21 | 22 |
| 22 class DistillerURLFetcherTest : public testing::Test { | 23 class DistillerURLFetcherTest : public testing::Test { |
| 23 public: | 24 public: |
| 24 void FetcherCallback(const std::string& response) { | 25 void FetcherCallback(const std::string& response) { |
| 25 response_ = response; | 26 response_ = response; |
| 26 } | 27 } |
| 27 | 28 |
| 28 protected: | 29 protected: |
| 29 // testing::Test implementation: | 30 // testing::Test implementation: |
| 30 virtual void SetUp() OVERRIDE { | 31 virtual void SetUp() OVERRIDE { |
| 31 url_fetcher_.reset(new dom_distiller::DistillerURLFetcher()); | 32 url_fetcher_.reset(new dom_distiller::DistillerURLFetcher()); |
| 32 factory_.reset(new net::FakeURLFetcherFactory(NULL)); | 33 factory_.reset(new net::FakeURLFetcherFactory(NULL)); |
| 33 factory_->SetFakeResponse( | 34 factory_->SetFakeResponse( |
| 34 GURL(kTestPageA), | 35 GURL(kTestPageA), |
| 35 std::string(kTestPageAResponse, sizeof(kTestPageAResponse)), | 36 std::string(kTestPageAResponse, sizeof(kTestPageAResponse)), |
| 36 net::HTTP_OK); | 37 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 37 factory_->SetFakeResponse( | 38 factory_->SetFakeResponse( |
| 38 GURL(kTestPageB), | 39 GURL(kTestPageB), |
| 39 std::string(kTestPageBResponse, sizeof(kTestPageBResponse)), | 40 std::string(kTestPageBResponse, sizeof(kTestPageBResponse)), |
| 40 net::HTTP_INTERNAL_SERVER_ERROR); | 41 net::HTTP_INTERNAL_SERVER_ERROR, net::URLRequestStatus::FAILED); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void Fetch(const std::string& url, | 44 void Fetch(const std::string& url, |
| 44 const std::string& expected_response) { | 45 const std::string& expected_response) { |
| 45 base::MessageLoop loop(base::MessageLoop::TYPE_UI); | 46 base::MessageLoop loop(base::MessageLoop::TYPE_UI); |
| 46 url_fetcher_->FetchURL( | 47 url_fetcher_->FetchURL( |
| 47 NULL, url, | 48 NULL, url, |
| 48 base::Bind(&DistillerURLFetcherTest::FetcherCallback, | 49 base::Bind(&DistillerURLFetcherTest::FetcherCallback, |
| 49 base::Unretained(this))); | 50 base::Unretained(this))); |
| 50 loop.RunUntilIdle(); | 51 loop.RunUntilIdle(); |
| 51 CHECK_EQ(expected_response, response_); | 52 CHECK_EQ(expected_response, response_); |
| 52 } | 53 } |
| 53 | 54 |
| 54 scoped_ptr<dom_distiller::DistillerURLFetcher> url_fetcher_; | 55 scoped_ptr<dom_distiller::DistillerURLFetcher> url_fetcher_; |
| 55 scoped_ptr<net::FakeURLFetcherFactory> factory_; | 56 scoped_ptr<net::FakeURLFetcherFactory> factory_; |
| 56 std::string response_; | 57 std::string response_; |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 TEST_F(DistillerURLFetcherTest, PopulateProto) { | 60 TEST_F(DistillerURLFetcherTest, PopulateProto) { |
| 60 Fetch(kTestPageA, | 61 Fetch(kTestPageA, |
| 61 std::string(kTestPageAResponse, sizeof(kTestPageAResponse))); | 62 std::string(kTestPageAResponse, sizeof(kTestPageAResponse))); |
| 62 } | 63 } |
| 63 | 64 |
| 64 TEST_F(DistillerURLFetcherTest, PopulateProtoFailedFetch) { | 65 TEST_F(DistillerURLFetcherTest, PopulateProtoFailedFetch) { |
| 65 // Expect the callback to contain an empty string for this URL. | 66 // Expect the callback to contain an empty string for this URL. |
| 66 Fetch(kTestPageB, std::string(std::string(), 0)); | 67 Fetch(kTestPageB, std::string(std::string(), 0)); |
| 67 } | 68 } |
| OLD | NEW |