| 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/url_request/test_url_fetcher_factory.h" | 10 #include "net/url_request/test_url_fetcher_factory.h" |
| 10 #include "net/url_request/url_fetcher.h" | 11 #include "net/url_request/url_fetcher.h" |
| 11 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 14 | 15 |
| 15 const char kTestPageA[] = "http://www.a.com/"; | 16 const char kTestPageA[] = "http://www.a.com/"; |
| 16 const char kTestPageAResponse[] = { 1, 2, 3, 4, 5, 6, 7 }; | 17 const char kTestPageAResponse[] = { 1, 2, 3, 4, 5, 6, 7 }; |
| 17 const char kTestPageB[] = "http://www.b.com/"; | 18 const char kTestPageB[] = "http://www.b.com/"; |
| 18 const char kTestPageBResponse[] = { 'a', 'b', 'c' }; | 19 const char kTestPageBResponse[] = { 'a', 'b', 'c' }; |
| 19 | 20 |
| 20 | 21 |
| 21 class DistillerURLFetcherTest : public testing::Test { | 22 class DistillerURLFetcherTest : public testing::Test { |
| 22 public: | 23 public: |
| 23 void FetcherCallback(const std::string& response) { | 24 void FetcherCallback(const std::string& response) { |
| 24 response_ = response; | 25 response_ = response; |
| 25 } | 26 } |
| 26 | 27 |
| 27 protected: | 28 protected: |
| 28 // testing::Test implementation: | 29 // testing::Test implementation: |
| 29 virtual void SetUp() OVERRIDE { | 30 virtual void SetUp() OVERRIDE { |
| 30 url_fetcher_.reset(new dom_distiller::DistillerURLFetcher()); | 31 url_fetcher_.reset(new dom_distiller::DistillerURLFetcher()); |
| 31 factory_.reset(new net::FakeURLFetcherFactory(NULL)); | 32 factory_.reset(new net::FakeURLFetcherFactory(NULL)); |
| 32 factory_->SetFakeResponse( | 33 factory_->SetFakeResponse( |
| 33 GURL(kTestPageA), | 34 GURL(kTestPageA), |
| 34 std::string(kTestPageAResponse, sizeof(kTestPageAResponse)), true); | 35 std::string(kTestPageAResponse, sizeof(kTestPageAResponse)), |
| 36 net::HTTP_OK); |
| 35 factory_->SetFakeResponse( | 37 factory_->SetFakeResponse( |
| 36 GURL(kTestPageB), | 38 GURL(kTestPageB), |
| 37 std::string(kTestPageBResponse, sizeof(kTestPageBResponse)), false); | 39 std::string(kTestPageBResponse, sizeof(kTestPageBResponse)), |
| 40 net::HTTP_INTERNAL_SERVER_ERROR); |
| 38 } | 41 } |
| 39 | 42 |
| 40 void Fetch(const std::string& url, | 43 void Fetch(const std::string& url, |
| 41 const std::string& expected_response) { | 44 const std::string& expected_response) { |
| 42 base::MessageLoop loop(base::MessageLoop::TYPE_UI); | 45 base::MessageLoop loop(base::MessageLoop::TYPE_UI); |
| 43 url_fetcher_->FetchURL( | 46 url_fetcher_->FetchURL( |
| 44 NULL, url, | 47 NULL, url, |
| 45 base::Bind(&DistillerURLFetcherTest::FetcherCallback, | 48 base::Bind(&DistillerURLFetcherTest::FetcherCallback, |
| 46 base::Unretained(this))); | 49 base::Unretained(this))); |
| 47 loop.RunUntilIdle(); | 50 loop.RunUntilIdle(); |
| 48 CHECK_EQ(expected_response, response_); | 51 CHECK_EQ(expected_response, response_); |
| 49 } | 52 } |
| 50 | 53 |
| 51 scoped_ptr<dom_distiller::DistillerURLFetcher> url_fetcher_; | 54 scoped_ptr<dom_distiller::DistillerURLFetcher> url_fetcher_; |
| 52 scoped_ptr<net::FakeURLFetcherFactory> factory_; | 55 scoped_ptr<net::FakeURLFetcherFactory> factory_; |
| 53 std::string response_; | 56 std::string response_; |
| 54 }; | 57 }; |
| 55 | 58 |
| 56 TEST_F(DistillerURLFetcherTest, PopulateProto) { | 59 TEST_F(DistillerURLFetcherTest, PopulateProto) { |
| 57 Fetch(kTestPageA, | 60 Fetch(kTestPageA, |
| 58 std::string(kTestPageAResponse, sizeof(kTestPageAResponse))); | 61 std::string(kTestPageAResponse, sizeof(kTestPageAResponse))); |
| 59 } | 62 } |
| 60 | 63 |
| 61 TEST_F(DistillerURLFetcherTest, PopulateProtoFailedFetch) { | 64 TEST_F(DistillerURLFetcherTest, PopulateProtoFailedFetch) { |
| 62 // Expect the callback to contain an empty string for this URL. | 65 // Expect the callback to contain an empty string for this URL. |
| 63 Fetch(kTestPageB, std::string(std::string(), 0)); | 66 Fetch(kTestPageB, std::string(std::string(), 0)); |
| 64 } | 67 } |
| OLD | NEW |