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
3 // found in the LICENSE file.
6 #include "base/bind_helpers.h"
7 #include "base/message_loop/message_loop.h"
8 #include "components/dom_distiller/core/distiller_url_fetcher.h"
9 #include "net/http/http_status_code.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_fetcher.h"
12 #include "net/url_request/url_request_context_getter.h"
13 #include "net/url_request/url_request_status.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 const char kTestPageA
[] = "http://www.a.com/";
18 const char kTestPageAResponse
[] = { 1, 2, 3, 4, 5, 6, 7 };
19 const char kTestPageB
[] = "http://www.b.com/";
20 const char kTestPageBResponse
[] = { 'a', 'b', 'c' };
23 class DistillerURLFetcherTest
: public testing::Test
{
25 void FetcherCallback(const std::string
& response
) {
30 // testing::Test implementation:
31 void SetUp() override
{
32 url_fetcher_
.reset(new dom_distiller::DistillerURLFetcher(NULL
));
33 factory_
.reset(new net::FakeURLFetcherFactory(NULL
));
34 factory_
->SetFakeResponse(
36 std::string(kTestPageAResponse
, sizeof(kTestPageAResponse
)),
38 net::URLRequestStatus::SUCCESS
);
39 factory_
->SetFakeResponse(
41 std::string(kTestPageBResponse
, sizeof(kTestPageBResponse
)),
42 net::HTTP_INTERNAL_SERVER_ERROR
,
43 net::URLRequestStatus::SUCCESS
);
46 void Fetch(const std::string
& url
,
47 const std::string
& expected_response
) {
48 base::MessageLoopForUI loop
;
49 url_fetcher_
->FetchURL(
51 base::Bind(&DistillerURLFetcherTest::FetcherCallback
,
52 base::Unretained(this)));
54 CHECK_EQ(expected_response
, response_
);
57 scoped_ptr
<dom_distiller::DistillerURLFetcher
> url_fetcher_
;
58 scoped_ptr
<net::FakeURLFetcherFactory
> factory_
;
59 std::string response_
;
62 TEST_F(DistillerURLFetcherTest
, PopulateProto
) {
64 std::string(kTestPageAResponse
, sizeof(kTestPageAResponse
)));
67 TEST_F(DistillerURLFetcherTest
, PopulateProtoFailedFetch
) {
68 // Expect the callback to contain an empty string for this URL.
69 Fetch(kTestPageB
, std::string(std::string(), 0));