Home | History | Annotate | Download | only in url_request
      1 // Copyright (c) 2012 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 "net/url_request/test_url_fetcher_factory.h"
      6 
      7 #include <string>
      8 
      9 #include "base/bind.h"
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "net/base/host_port_pair.h"
     14 #include "net/http/http_response_headers.h"
     15 #include "net/url_request/url_fetcher_delegate.h"
     16 #include "net/url_request/url_fetcher_impl.h"
     17 #include "net/url_request/url_request_status.h"
     18 
     19 namespace net {
     20 
     21 ScopedURLFetcherFactory::ScopedURLFetcherFactory(
     22     URLFetcherFactory* factory) {
     23   DCHECK(!URLFetcherImpl::factory());
     24   URLFetcherImpl::set_factory(factory);
     25 }
     26 
     27 ScopedURLFetcherFactory::~ScopedURLFetcherFactory() {
     28   DCHECK(URLFetcherImpl::factory());
     29   URLFetcherImpl::set_factory(NULL);
     30 }
     31 
     32 TestURLFetcher::TestURLFetcher(int id,
     33                                const GURL& url,
     34                                URLFetcherDelegate* d)
     35     : owner_(NULL),
     36       id_(id),
     37       original_url_(url),
     38       delegate_(d),
     39       delegate_for_tests_(NULL),
     40       did_receive_last_chunk_(false),
     41       fake_load_flags_(0),
     42       fake_response_code_(-1),
     43       fake_response_destination_(STRING),
     44       fake_was_fetched_via_proxy_(false),
     45       fake_max_retries_(0) {
     46 }
     47 
     48 TestURLFetcher::~TestURLFetcher() {
     49   if (delegate_for_tests_)
     50     delegate_for_tests_->OnRequestEnd(id_);
     51   if (owner_)
     52     owner_->RemoveFetcherFromMap(id_);
     53 }
     54 
     55 void TestURLFetcher::SetUploadData(const std::string& upload_content_type,
     56                                    const std::string& upload_content) {
     57   upload_data_ = upload_content;
     58 }
     59 
     60 void TestURLFetcher::SetUploadFilePath(
     61     const std::string& upload_content_type,
     62     const base::FilePath& file_path,
     63     uint64 range_offset,
     64     uint64 range_length,
     65     scoped_refptr<base::TaskRunner> file_task_runner) {
     66   upload_file_path_ = file_path;
     67 }
     68 
     69 void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) {
     70 }
     71 
     72 void TestURLFetcher::AppendChunkToUpload(const std::string& data,
     73                                          bool is_last_chunk) {
     74   DCHECK(!did_receive_last_chunk_);
     75   did_receive_last_chunk_ = is_last_chunk;
     76   chunks_.push_back(data);
     77   if (delegate_for_tests_)
     78     delegate_for_tests_->OnChunkUpload(id_);
     79 }
     80 
     81 void TestURLFetcher::SetLoadFlags(int load_flags) {
     82   fake_load_flags_= load_flags;
     83 }
     84 
     85 int TestURLFetcher::GetLoadFlags() const {
     86   return fake_load_flags_;
     87 }
     88 
     89 void TestURLFetcher::SetReferrer(const std::string& referrer) {
     90 }
     91 
     92 void TestURLFetcher::SetExtraRequestHeaders(
     93     const std::string& extra_request_headers) {
     94   fake_extra_request_headers_.Clear();
     95   fake_extra_request_headers_.AddHeadersFromString(extra_request_headers);
     96 }
     97 
     98 void TestURLFetcher::AddExtraRequestHeader(const std::string& header_line) {
     99   fake_extra_request_headers_.AddHeaderFromString(header_line);
    100 }
    101 
    102 void TestURLFetcher::GetExtraRequestHeaders(
    103     HttpRequestHeaders* headers) const {
    104   *headers = fake_extra_request_headers_;
    105 }
    106 
    107 void TestURLFetcher::SetRequestContext(
    108     URLRequestContextGetter* request_context_getter) {
    109 }
    110 
    111 void TestURLFetcher::SetFirstPartyForCookies(
    112     const GURL& first_party_for_cookies) {
    113 }
    114 
    115 void TestURLFetcher::SetURLRequestUserData(
    116     const void* key,
    117     const CreateDataCallback& create_data_callback) {
    118 }
    119 
    120 void TestURLFetcher::SetStopOnRedirect(bool stop_on_redirect) {
    121 }
    122 
    123 void TestURLFetcher::SetAutomaticallyRetryOn5xx(bool retry) {
    124 }
    125 
    126 void TestURLFetcher::SetMaxRetriesOn5xx(int max_retries) {
    127   fake_max_retries_ = max_retries;
    128 }
    129 
    130 int TestURLFetcher::GetMaxRetriesOn5xx() const {
    131   return fake_max_retries_;
    132 }
    133 
    134 base::TimeDelta TestURLFetcher::GetBackoffDelay() const {
    135   return fake_backoff_delay_;
    136 }
    137 
    138 void TestURLFetcher::SetAutomaticallyRetryOnNetworkChanges(int max_retries) {
    139 }
    140 
    141 void TestURLFetcher::SaveResponseToFileAtPath(
    142     const base::FilePath& file_path,
    143     scoped_refptr<base::TaskRunner> file_task_runner) {
    144 }
    145 
    146 void TestURLFetcher::SaveResponseToTemporaryFile(
    147     scoped_refptr<base::TaskRunner> file_task_runner) {
    148 }
    149 
    150 HttpResponseHeaders* TestURLFetcher::GetResponseHeaders() const {
    151   return fake_response_headers_.get();
    152 }
    153 
    154 HostPortPair TestURLFetcher::GetSocketAddress() const {
    155   NOTIMPLEMENTED();
    156   return HostPortPair();
    157 }
    158 
    159 bool TestURLFetcher::WasFetchedViaProxy() const {
    160   return fake_was_fetched_via_proxy_;
    161 }
    162 
    163 void TestURLFetcher::Start() {
    164   // Overriden to do nothing. It is assumed the caller will notify the delegate.
    165   if (delegate_for_tests_)
    166     delegate_for_tests_->OnRequestStart(id_);
    167 }
    168 
    169 const GURL& TestURLFetcher::GetOriginalURL() const {
    170   return original_url_;
    171 }
    172 
    173 const GURL& TestURLFetcher::GetURL() const {
    174   return fake_url_;
    175 }
    176 
    177 const URLRequestStatus& TestURLFetcher::GetStatus() const {
    178   return fake_status_;
    179 }
    180 
    181 int TestURLFetcher::GetResponseCode() const {
    182   return fake_response_code_;
    183 }
    184 
    185 const ResponseCookies& TestURLFetcher::GetCookies() const {
    186   return fake_cookies_;
    187 }
    188 
    189 bool TestURLFetcher::FileErrorOccurred(int* out_error_code) const {
    190   NOTIMPLEMENTED();
    191   return false;
    192 }
    193 
    194 void TestURLFetcher::ReceivedContentWasMalformed() {
    195 }
    196 
    197 bool TestURLFetcher::GetResponseAsString(
    198     std::string* out_response_string) const {
    199   if (fake_response_destination_ != STRING)
    200     return false;
    201 
    202   *out_response_string = fake_response_string_;
    203   return true;
    204 }
    205 
    206 bool TestURLFetcher::GetResponseAsFilePath(
    207     bool take_ownership, base::FilePath* out_response_path) const {
    208   if (fake_response_destination_ != TEMP_FILE)
    209     return false;
    210 
    211   *out_response_path = fake_response_file_path_;
    212   return true;
    213 }
    214 
    215 void TestURLFetcher::set_status(const URLRequestStatus& status) {
    216   fake_status_ = status;
    217 }
    218 
    219 void TestURLFetcher::set_was_fetched_via_proxy(bool flag) {
    220   fake_was_fetched_via_proxy_ = flag;
    221 }
    222 
    223 void TestURLFetcher::set_response_headers(
    224     scoped_refptr<HttpResponseHeaders> headers) {
    225   fake_response_headers_ = headers;
    226 }
    227 
    228 void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) {
    229   fake_backoff_delay_ = backoff_delay;
    230 }
    231 
    232 void TestURLFetcher::SetDelegateForTests(DelegateForTests* delegate_for_tests) {
    233   delegate_for_tests_ = delegate_for_tests;
    234 }
    235 
    236 void TestURLFetcher::SetResponseString(const std::string& response) {
    237   fake_response_destination_ = STRING;
    238   fake_response_string_ = response;
    239 }
    240 
    241 void TestURLFetcher::SetResponseFilePath(const base::FilePath& path) {
    242   fake_response_destination_ = TEMP_FILE;
    243   fake_response_file_path_ = path;
    244 }
    245 
    246 TestURLFetcherFactory::TestURLFetcherFactory()
    247     : ScopedURLFetcherFactory(this),
    248       delegate_for_tests_(NULL),
    249       remove_fetcher_on_delete_(false) {
    250 }
    251 
    252 TestURLFetcherFactory::~TestURLFetcherFactory() {}
    253 
    254 URLFetcher* TestURLFetcherFactory::CreateURLFetcher(
    255     int id,
    256     const GURL& url,
    257     URLFetcher::RequestType request_type,
    258     URLFetcherDelegate* d) {
    259   TestURLFetcher* fetcher = new TestURLFetcher(id, url, d);
    260   if (remove_fetcher_on_delete_)
    261     fetcher->set_owner(this);
    262   fetcher->SetDelegateForTests(delegate_for_tests_);
    263   fetchers_[id] = fetcher;
    264   return fetcher;
    265 }
    266 
    267 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const {
    268   Fetchers::const_iterator i = fetchers_.find(id);
    269   return i == fetchers_.end() ? NULL : i->second;
    270 }
    271 
    272 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) {
    273   Fetchers::iterator i = fetchers_.find(id);
    274   DCHECK(i != fetchers_.end());
    275   fetchers_.erase(i);
    276 }
    277 
    278 void TestURLFetcherFactory::SetDelegateForTests(
    279     TestURLFetcherDelegateForTests* delegate_for_tests) {
    280   delegate_for_tests_ = delegate_for_tests;
    281 }
    282 
    283 FakeURLFetcher::FakeURLFetcher(const GURL& url,
    284                                URLFetcherDelegate* d,
    285                                const std::string& response_data,
    286                                bool success)
    287     : TestURLFetcher(0, url, d),
    288       weak_factory_(this) {
    289   set_status(URLRequestStatus(
    290       success ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED,
    291       0));
    292   set_response_code(success ? 200 : 500);
    293   SetResponseString(response_data);
    294 }
    295 
    296 FakeURLFetcher::~FakeURLFetcher() {}
    297 
    298 void FakeURLFetcher::Start() {
    299   base::MessageLoop::current()->PostTask(
    300       FROM_HERE,
    301       base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr()));
    302 }
    303 
    304 void FakeURLFetcher::RunDelegate() {
    305   delegate()->OnURLFetchComplete(this);
    306 }
    307 
    308 const GURL& FakeURLFetcher::GetURL() const {
    309   return TestURLFetcher::GetOriginalURL();
    310 }
    311 
    312 FakeURLFetcherFactory::FakeURLFetcherFactory(
    313     URLFetcherFactory* default_factory)
    314     : ScopedURLFetcherFactory(this),
    315       creator_(base::Bind(&DefaultFakeURLFetcherCreator)),
    316       default_factory_(default_factory) {
    317 }
    318 
    319 FakeURLFetcherFactory::FakeURLFetcherFactory(
    320     URLFetcherFactory* default_factory,
    321     const FakeURLFetcherCreator& creator)
    322     : ScopedURLFetcherFactory(this),
    323       creator_(creator),
    324       default_factory_(default_factory) {
    325 }
    326 
    327 scoped_ptr<FakeURLFetcher> FakeURLFetcherFactory::DefaultFakeURLFetcherCreator(
    328       const GURL& url,
    329       URLFetcherDelegate* delegate,
    330       const std::string& response,
    331       bool success) {
    332   return scoped_ptr<FakeURLFetcher>(new FakeURLFetcher(url, delegate,
    333                                                        response, success));
    334 }
    335 
    336 FakeURLFetcherFactory::~FakeURLFetcherFactory() {}
    337 
    338 URLFetcher* FakeURLFetcherFactory::CreateURLFetcher(
    339     int id,
    340     const GURL& url,
    341     URLFetcher::RequestType request_type,
    342     URLFetcherDelegate* d) {
    343   FakeResponseMap::const_iterator it = fake_responses_.find(url);
    344   if (it == fake_responses_.end()) {
    345     if (default_factory_ == NULL) {
    346       // If we don't have a baked response for that URL we return NULL.
    347       DLOG(ERROR) << "No baked response for URL: " << url.spec();
    348       return NULL;
    349     } else {
    350       return default_factory_->CreateURLFetcher(id, url, request_type, d);
    351     }
    352   }
    353 
    354   scoped_ptr<FakeURLFetcher> fake_fetcher =
    355       creator_.Run(url, d, it->second.first, it->second.second);
    356   // TODO: Make URLFetcherFactory::CreateURLFetcher return a scoped_ptr
    357   return fake_fetcher.release();
    358 }
    359 
    360 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url,
    361                                             const std::string& response_data,
    362                                             bool success) {
    363   // Overwrite existing URL if it already exists.
    364   fake_responses_[GURL(url)] = std::make_pair(response_data, success);
    365 }
    366 
    367 void FakeURLFetcherFactory::ClearFakeResponses() {
    368   fake_responses_.clear();
    369 }
    370 
    371 URLFetcherImplFactory::URLFetcherImplFactory() {}
    372 
    373 URLFetcherImplFactory::~URLFetcherImplFactory() {}
    374 
    375 URLFetcher* URLFetcherImplFactory::CreateURLFetcher(
    376     int id,
    377     const GURL& url,
    378     URLFetcher::RequestType request_type,
    379     URLFetcherDelegate* d) {
    380   return new URLFetcherImpl(url, request_type, d);
    381 }
    382 
    383 }  // namespace net
    384