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/url_request_test_util.h"
      6 
      7 #include "base/compiler_specific.h"
      8 #include "base/logging.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/threading/thread.h"
     11 #include "base/threading/worker_pool.h"
     12 #include "net/base/host_port_pair.h"
     13 #include "net/cert/cert_verifier.h"
     14 #include "net/dns/mock_host_resolver.h"
     15 #include "net/http/http_network_session.h"
     16 #include "net/http/http_response_headers.h"
     17 #include "net/http/http_server_properties_impl.h"
     18 #include "net/http/transport_security_state.h"
     19 #include "net/ssl/channel_id_service.h"
     20 #include "net/ssl/default_channel_id_store.h"
     21 #include "net/url_request/static_http_user_agent_settings.h"
     22 #include "net/url_request/url_request_job_factory_impl.h"
     23 #include "testing/gtest/include/gtest/gtest.h"
     24 
     25 namespace net {
     26 
     27 namespace {
     28 
     29 // These constants put the NetworkDelegate events of TestNetworkDelegate
     30 // into an order. They are used in conjunction with
     31 // |TestNetworkDelegate::next_states_| to check that we do not send
     32 // events in the wrong order.
     33 const int kStageBeforeURLRequest = 1 << 0;
     34 const int kStageBeforeSendHeaders = 1 << 1;
     35 const int kStageSendHeaders = 1 << 2;
     36 const int kStageHeadersReceived = 1 << 3;
     37 const int kStageAuthRequired = 1 << 4;
     38 const int kStageBeforeRedirect = 1 << 5;
     39 const int kStageResponseStarted = 1 << 6;
     40 const int kStageCompletedSuccess = 1 << 7;
     41 const int kStageCompletedError = 1 << 8;
     42 const int kStageURLRequestDestroyed = 1 << 9;
     43 const int kStageDestruction = 1 << 10;
     44 
     45 }  // namespace
     46 
     47 TestURLRequestContext::TestURLRequestContext()
     48     : initialized_(false),
     49       client_socket_factory_(NULL),
     50       context_storage_(this) {
     51   Init();
     52 }
     53 
     54 TestURLRequestContext::TestURLRequestContext(bool delay_initialization)
     55     : initialized_(false),
     56       client_socket_factory_(NULL),
     57       context_storage_(this) {
     58   if (!delay_initialization)
     59     Init();
     60 }
     61 
     62 TestURLRequestContext::~TestURLRequestContext() {
     63   DCHECK(initialized_);
     64   AssertNoURLRequests();
     65 }
     66 
     67 void TestURLRequestContext::Init() {
     68   DCHECK(!initialized_);
     69   initialized_ = true;
     70 
     71   if (!host_resolver())
     72     context_storage_.set_host_resolver(
     73         scoped_ptr<HostResolver>(new MockCachingHostResolver()));
     74   if (!proxy_service())
     75     context_storage_.set_proxy_service(ProxyService::CreateDirect());
     76   if (!cert_verifier())
     77     context_storage_.set_cert_verifier(CertVerifier::CreateDefault());
     78   if (!transport_security_state())
     79     context_storage_.set_transport_security_state(new TransportSecurityState);
     80   if (!ssl_config_service())
     81     context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
     82   if (!http_auth_handler_factory()) {
     83     context_storage_.set_http_auth_handler_factory(
     84         HttpAuthHandlerFactory::CreateDefault(host_resolver()));
     85   }
     86   if (!http_server_properties()) {
     87     context_storage_.set_http_server_properties(
     88         scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
     89   }
     90   if (!transport_security_state()) {
     91     context_storage_.set_transport_security_state(
     92         new TransportSecurityState());
     93   }
     94   if (http_transaction_factory()) {
     95     // Make sure we haven't been passed an object we're not going to use.
     96     EXPECT_FALSE(client_socket_factory_);
     97   } else {
     98     HttpNetworkSession::Params params;
     99     if (http_network_session_params_)
    100       params = *http_network_session_params_;
    101     params.client_socket_factory = client_socket_factory();
    102     params.host_resolver = host_resolver();
    103     params.cert_verifier = cert_verifier();
    104     params.transport_security_state = transport_security_state();
    105     params.proxy_service = proxy_service();
    106     params.ssl_config_service = ssl_config_service();
    107     params.http_auth_handler_factory = http_auth_handler_factory();
    108     params.network_delegate = network_delegate();
    109     params.http_server_properties = http_server_properties();
    110     params.net_log = net_log();
    111     context_storage_.set_http_transaction_factory(new HttpCache(
    112         new HttpNetworkSession(params),
    113         HttpCache::DefaultBackend::InMemory(0)));
    114   }
    115   // In-memory cookie store.
    116   if (!cookie_store())
    117     context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
    118   // In-memory Channel ID service.
    119   if (!channel_id_service()) {
    120     context_storage_.set_channel_id_service(
    121         new ChannelIDService(
    122             new DefaultChannelIDStore(NULL),
    123             base::WorkerPool::GetTaskRunner(true)));
    124   }
    125   if (!http_user_agent_settings()) {
    126     context_storage_.set_http_user_agent_settings(
    127         new StaticHttpUserAgentSettings("en-us,fr", std::string()));
    128   }
    129   if (!job_factory())
    130     context_storage_.set_job_factory(new URLRequestJobFactoryImpl);
    131 }
    132 
    133 TestURLRequestContextGetter::TestURLRequestContextGetter(
    134     const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
    135     : network_task_runner_(network_task_runner) {
    136   DCHECK(network_task_runner_.get());
    137 }
    138 
    139 TestURLRequestContextGetter::TestURLRequestContextGetter(
    140     const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
    141     scoped_ptr<TestURLRequestContext> context)
    142     : network_task_runner_(network_task_runner), context_(context.Pass()) {
    143   DCHECK(network_task_runner_.get());
    144 }
    145 
    146 TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
    147 
    148 TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
    149   if (!context_.get())
    150     context_.reset(new TestURLRequestContext);
    151   return context_.get();
    152 }
    153 
    154 scoped_refptr<base::SingleThreadTaskRunner>
    155 TestURLRequestContextGetter::GetNetworkTaskRunner() const {
    156   return network_task_runner_;
    157 }
    158 
    159 TestDelegate::TestDelegate()
    160     : cancel_in_rr_(false),
    161       cancel_in_rs_(false),
    162       cancel_in_rd_(false),
    163       cancel_in_rd_pending_(false),
    164       quit_on_complete_(true),
    165       quit_on_redirect_(false),
    166       quit_on_before_network_start_(false),
    167       allow_certificate_errors_(false),
    168       response_started_count_(0),
    169       received_bytes_count_(0),
    170       received_redirect_count_(0),
    171       received_before_network_start_count_(0),
    172       received_data_before_response_(false),
    173       request_failed_(false),
    174       have_certificate_errors_(false),
    175       certificate_errors_are_fatal_(false),
    176       auth_required_(false),
    177       have_full_request_headers_(false),
    178       buf_(new IOBuffer(kBufferSize)) {
    179 }
    180 
    181 TestDelegate::~TestDelegate() {}
    182 
    183 void TestDelegate::ClearFullRequestHeaders() {
    184   full_request_headers_.Clear();
    185   have_full_request_headers_ = false;
    186 }
    187 
    188 void TestDelegate::OnReceivedRedirect(URLRequest* request,
    189                                       const RedirectInfo& redirect_info,
    190                                       bool* defer_redirect) {
    191   EXPECT_TRUE(request->is_redirecting());
    192 
    193   have_full_request_headers_ =
    194       request->GetFullRequestHeaders(&full_request_headers_);
    195 
    196   received_redirect_count_++;
    197   if (quit_on_redirect_) {
    198     *defer_redirect = true;
    199     base::MessageLoop::current()->PostTask(FROM_HERE,
    200                                            base::MessageLoop::QuitClosure());
    201   } else if (cancel_in_rr_) {
    202     request->Cancel();
    203   }
    204 }
    205 
    206 void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
    207   received_before_network_start_count_++;
    208   if (quit_on_before_network_start_) {
    209     *defer = true;
    210     base::MessageLoop::current()->PostTask(FROM_HERE,
    211                                            base::MessageLoop::QuitClosure());
    212   }
    213 }
    214 
    215 void TestDelegate::OnAuthRequired(URLRequest* request,
    216                                   AuthChallengeInfo* auth_info) {
    217   auth_required_ = true;
    218   if (!credentials_.Empty()) {
    219     request->SetAuth(credentials_);
    220   } else {
    221     request->CancelAuth();
    222   }
    223 }
    224 
    225 void TestDelegate::OnSSLCertificateError(URLRequest* request,
    226                                          const SSLInfo& ssl_info,
    227                                          bool fatal) {
    228   // The caller can control whether it needs all SSL requests to go through,
    229   // independent of any possible errors, or whether it wants SSL errors to
    230   // cancel the request.
    231   have_certificate_errors_ = true;
    232   certificate_errors_are_fatal_ = fatal;
    233   if (allow_certificate_errors_)
    234     request->ContinueDespiteLastError();
    235   else
    236     request->Cancel();
    237 }
    238 
    239 void TestDelegate::OnResponseStarted(URLRequest* request) {
    240   // It doesn't make sense for the request to have IO pending at this point.
    241   DCHECK(!request->status().is_io_pending());
    242   EXPECT_FALSE(request->is_redirecting());
    243 
    244   have_full_request_headers_ =
    245       request->GetFullRequestHeaders(&full_request_headers_);
    246 
    247   response_started_count_++;
    248   if (cancel_in_rs_) {
    249     request->Cancel();
    250     OnResponseCompleted(request);
    251   } else if (!request->status().is_success()) {
    252     DCHECK(request->status().status() == URLRequestStatus::FAILED ||
    253            request->status().status() == URLRequestStatus::CANCELED);
    254     request_failed_ = true;
    255     OnResponseCompleted(request);
    256   } else {
    257     // Initiate the first read.
    258     int bytes_read = 0;
    259     if (request->Read(buf_.get(), kBufferSize, &bytes_read))
    260       OnReadCompleted(request, bytes_read);
    261     else if (!request->status().is_io_pending())
    262       OnResponseCompleted(request);
    263   }
    264 }
    265 
    266 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
    267   // It doesn't make sense for the request to have IO pending at this point.
    268   DCHECK(!request->status().is_io_pending());
    269 
    270   if (response_started_count_ == 0)
    271     received_data_before_response_ = true;
    272 
    273   if (cancel_in_rd_)
    274     request->Cancel();
    275 
    276   if (bytes_read >= 0) {
    277     // There is data to read.
    278     received_bytes_count_ += bytes_read;
    279 
    280     // consume the data
    281     data_received_.append(buf_->data(), bytes_read);
    282   }
    283 
    284   // If it was not end of stream, request to read more.
    285   if (request->status().is_success() && bytes_read > 0) {
    286     bytes_read = 0;
    287     while (request->Read(buf_.get(), kBufferSize, &bytes_read)) {
    288       if (bytes_read > 0) {
    289         data_received_.append(buf_->data(), bytes_read);
    290         received_bytes_count_ += bytes_read;
    291       } else {
    292         break;
    293       }
    294     }
    295   }
    296   if (!request->status().is_io_pending())
    297     OnResponseCompleted(request);
    298   else if (cancel_in_rd_pending_)
    299     request->Cancel();
    300 }
    301 
    302 void TestDelegate::OnResponseCompleted(URLRequest* request) {
    303   if (quit_on_complete_)
    304     base::MessageLoop::current()->PostTask(FROM_HERE,
    305                                            base::MessageLoop::QuitClosure());
    306 }
    307 
    308 TestNetworkDelegate::TestNetworkDelegate()
    309     : last_error_(0),
    310       error_count_(0),
    311       created_requests_(0),
    312       destroyed_requests_(0),
    313       completed_requests_(0),
    314       canceled_requests_(0),
    315       cookie_options_bit_mask_(0),
    316       blocked_get_cookies_count_(0),
    317       blocked_set_cookie_count_(0),
    318       set_cookie_count_(0),
    319       observed_before_proxy_headers_sent_callbacks_(0),
    320       has_load_timing_info_before_redirect_(false),
    321       has_load_timing_info_before_auth_(false),
    322       can_access_files_(true),
    323       can_throttle_requests_(true),
    324       cancel_request_with_policy_violating_referrer_(false) {
    325 }
    326 
    327 TestNetworkDelegate::~TestNetworkDelegate() {
    328   for (std::map<int, int>::iterator i = next_states_.begin();
    329        i != next_states_.end(); ++i) {
    330     event_order_[i->first] += "~TestNetworkDelegate\n";
    331     EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first];
    332   }
    333 }
    334 
    335 bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect(
    336     LoadTimingInfo* load_timing_info_before_redirect) const {
    337   *load_timing_info_before_redirect = load_timing_info_before_redirect_;
    338   return has_load_timing_info_before_redirect_;
    339 }
    340 
    341 bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth(
    342     LoadTimingInfo* load_timing_info_before_auth) const {
    343   *load_timing_info_before_auth = load_timing_info_before_auth_;
    344   return has_load_timing_info_before_auth_;
    345 }
    346 
    347 void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
    348   if (next_states_.find(request_id) == next_states_.end()) {
    349     // TODO(davidben): Although the URLRequest documentation does not allow
    350     // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's
    351     // destructor also calls Cancel. Either officially support this or fix the
    352     // ResourceLoader code.
    353     next_states_[request_id] = kStageBeforeURLRequest | kStageCompletedError;
    354     event_order_[request_id] = "";
    355   }
    356 }
    357 
    358 int TestNetworkDelegate::OnBeforeURLRequest(
    359     URLRequest* request,
    360     const CompletionCallback& callback,
    361     GURL* new_url) {
    362   int req_id = request->identifier();
    363   InitRequestStatesIfNew(req_id);
    364   event_order_[req_id] += "OnBeforeURLRequest\n";
    365   EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) <<
    366       event_order_[req_id];
    367   next_states_[req_id] =
    368       kStageBeforeSendHeaders |
    369       kStageResponseStarted |  // data: URLs do not trigger sending headers
    370       kStageBeforeRedirect |  // a delegate can trigger a redirection
    371       kStageCompletedError |  // request canceled by delegate
    372       kStageAuthRequired;  // Auth can come next for FTP requests
    373   created_requests_++;
    374   return OK;
    375 }
    376 
    377 int TestNetworkDelegate::OnBeforeSendHeaders(
    378     URLRequest* request,
    379     const CompletionCallback& callback,
    380     HttpRequestHeaders* headers) {
    381   int req_id = request->identifier();
    382   InitRequestStatesIfNew(req_id);
    383   event_order_[req_id] += "OnBeforeSendHeaders\n";
    384   EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
    385       event_order_[req_id];
    386   next_states_[req_id] =
    387       kStageSendHeaders |
    388       kStageCompletedError;  // request canceled by delegate
    389 
    390   return OK;
    391 }
    392 
    393 void TestNetworkDelegate::OnBeforeSendProxyHeaders(
    394     net::URLRequest* request,
    395     const net::ProxyInfo& proxy_info,
    396     net::HttpRequestHeaders* headers) {
    397   ++observed_before_proxy_headers_sent_callbacks_;
    398   last_observed_proxy_ = proxy_info.proxy_server().host_port_pair();
    399 }
    400 
    401 void TestNetworkDelegate::OnSendHeaders(
    402     URLRequest* request,
    403     const HttpRequestHeaders& headers) {
    404   int req_id = request->identifier();
    405   InitRequestStatesIfNew(req_id);
    406   event_order_[req_id] += "OnSendHeaders\n";
    407   EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
    408       event_order_[req_id];
    409   next_states_[req_id] =
    410       kStageHeadersReceived |
    411       kStageCompletedError;
    412 }
    413 
    414 int TestNetworkDelegate::OnHeadersReceived(
    415     URLRequest* request,
    416     const CompletionCallback& callback,
    417     const HttpResponseHeaders* original_response_headers,
    418     scoped_refptr<HttpResponseHeaders>* override_response_headers,
    419     GURL* allowed_unsafe_redirect_url) {
    420   int req_id = request->identifier();
    421   event_order_[req_id] += "OnHeadersReceived\n";
    422   InitRequestStatesIfNew(req_id);
    423   EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) <<
    424       event_order_[req_id];
    425   next_states_[req_id] =
    426       kStageBeforeRedirect |
    427       kStageResponseStarted |
    428       kStageAuthRequired |
    429       kStageCompletedError;  // e.g. proxy resolution problem
    430 
    431   // Basic authentication sends a second request from the URLRequestHttpJob
    432   // layer before the URLRequest reports that a response has started.
    433   next_states_[req_id] |= kStageBeforeSendHeaders;
    434 
    435   if (!redirect_on_headers_received_url_.is_empty()) {
    436     *override_response_headers =
    437         new net::HttpResponseHeaders(original_response_headers->raw_headers());
    438     (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found");
    439     (*override_response_headers)->RemoveHeader("Location");
    440     (*override_response_headers)->AddHeader(
    441         "Location: " + redirect_on_headers_received_url_.spec());
    442 
    443     redirect_on_headers_received_url_ = GURL();
    444 
    445     if (!allowed_unsafe_redirect_url_.is_empty())
    446       *allowed_unsafe_redirect_url = allowed_unsafe_redirect_url_;
    447   }
    448 
    449   return OK;
    450 }
    451 
    452 void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
    453                                            const GURL& new_location) {
    454   load_timing_info_before_redirect_ = LoadTimingInfo();
    455   request->GetLoadTimingInfo(&load_timing_info_before_redirect_);
    456   has_load_timing_info_before_redirect_ = true;
    457   EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null());
    458   EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null());
    459 
    460   int req_id = request->identifier();
    461   InitRequestStatesIfNew(req_id);
    462   event_order_[req_id] += "OnBeforeRedirect\n";
    463   EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
    464       event_order_[req_id];
    465   next_states_[req_id] =
    466       kStageBeforeURLRequest |  // HTTP redirects trigger this.
    467       kStageBeforeSendHeaders |  // Redirects from the network delegate do not
    468                                  // trigger onBeforeURLRequest.
    469       kStageCompletedError;
    470 
    471   // A redirect can lead to a file or a data URL. In this case, we do not send
    472   // headers.
    473   next_states_[req_id] |= kStageResponseStarted;
    474 }
    475 
    476 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
    477   LoadTimingInfo load_timing_info;
    478   request->GetLoadTimingInfo(&load_timing_info);
    479   EXPECT_FALSE(load_timing_info.request_start_time.is_null());
    480   EXPECT_FALSE(load_timing_info.request_start.is_null());
    481 
    482   int req_id = request->identifier();
    483   InitRequestStatesIfNew(req_id);
    484   event_order_[req_id] += "OnResponseStarted\n";
    485   EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
    486       event_order_[req_id];
    487   next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
    488   if (request->status().status() == URLRequestStatus::FAILED) {
    489     error_count_++;
    490     last_error_ = request->status().error();
    491   }
    492 }
    493 
    494 void TestNetworkDelegate::OnRawBytesRead(const URLRequest& request,
    495                                          int bytes_read) {
    496 }
    497 
    498 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
    499   int req_id = request->identifier();
    500   InitRequestStatesIfNew(req_id);
    501   event_order_[req_id] += "OnCompleted\n";
    502   // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
    503   // is logically identical to
    504   // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
    505   EXPECT_TRUE(!request->status().is_success() ||
    506               (next_states_[req_id] & kStageCompletedSuccess)) <<
    507       event_order_[req_id];
    508   EXPECT_TRUE(request->status().is_success() ||
    509               (next_states_[req_id] & kStageCompletedError)) <<
    510       event_order_[req_id];
    511   next_states_[req_id] = kStageURLRequestDestroyed;
    512   completed_requests_++;
    513   if (request->status().status() == URLRequestStatus::FAILED) {
    514     error_count_++;
    515     last_error_ = request->status().error();
    516   } else if (request->status().status() == URLRequestStatus::CANCELED) {
    517     canceled_requests_++;
    518   } else {
    519     DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
    520   }
    521 }
    522 
    523 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
    524   int req_id = request->identifier();
    525   InitRequestStatesIfNew(req_id);
    526   event_order_[req_id] += "OnURLRequestDestroyed\n";
    527   EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
    528       event_order_[req_id];
    529   next_states_[req_id] = kStageDestruction;
    530   destroyed_requests_++;
    531 }
    532 
    533 void TestNetworkDelegate::OnPACScriptError(int line_number,
    534                                            const base::string16& error) {
    535 }
    536 
    537 NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
    538     URLRequest* request,
    539     const AuthChallengeInfo& auth_info,
    540     const AuthCallback& callback,
    541     AuthCredentials* credentials) {
    542   load_timing_info_before_auth_ = LoadTimingInfo();
    543   request->GetLoadTimingInfo(&load_timing_info_before_auth_);
    544   has_load_timing_info_before_auth_ = true;
    545   EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null());
    546   EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null());
    547 
    548   int req_id = request->identifier();
    549   InitRequestStatesIfNew(req_id);
    550   event_order_[req_id] += "OnAuthRequired\n";
    551   EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) <<
    552       event_order_[req_id];
    553   next_states_[req_id] = kStageBeforeSendHeaders |
    554       kStageAuthRequired |  // For example, proxy auth followed by server auth.
    555       kStageHeadersReceived |  // Request canceled by delegate simulates empty
    556                                // response.
    557       kStageResponseStarted |  // data: URLs do not trigger sending headers
    558       kStageBeforeRedirect |   // a delegate can trigger a redirection
    559       kStageCompletedError;    // request cancelled before callback
    560   return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
    561 }
    562 
    563 bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
    564                                           const CookieList& cookie_list) {
    565   bool allow = true;
    566   if (cookie_options_bit_mask_ & NO_GET_COOKIES)
    567     allow = false;
    568 
    569   if (!allow) {
    570     blocked_get_cookies_count_++;
    571   }
    572 
    573   return allow;
    574 }
    575 
    576 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
    577                                          const std::string& cookie_line,
    578                                          CookieOptions* options) {
    579   bool allow = true;
    580   if (cookie_options_bit_mask_ & NO_SET_COOKIE)
    581     allow = false;
    582 
    583   if (!allow) {
    584     blocked_set_cookie_count_++;
    585   } else {
    586     set_cookie_count_++;
    587   }
    588 
    589   return allow;
    590 }
    591 
    592 bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
    593                                           const base::FilePath& path) const {
    594   return can_access_files_;
    595 }
    596 
    597 bool TestNetworkDelegate::OnCanThrottleRequest(
    598     const URLRequest& request) const {
    599   return can_throttle_requests_;
    600 }
    601 
    602 int TestNetworkDelegate::OnBeforeSocketStreamConnect(
    603     SocketStream* socket,
    604     const CompletionCallback& callback) {
    605   return OK;
    606 }
    607 
    608 bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
    609     const URLRequest& request,
    610     const GURL& target_url,
    611     const GURL& referrer_url) const {
    612   return cancel_request_with_policy_violating_referrer_;
    613 }
    614 
    615 // static
    616 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1");
    617 
    618 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost(
    619   const std::string& new_value)
    620     : old_value_(value_),
    621       new_value_(new_value) {
    622   value_ = new_value_;
    623 }
    624 
    625 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() {
    626   DCHECK_EQ(value_, new_value_);
    627   value_ = old_value_;
    628 }
    629 
    630 // static
    631 const std::string& ScopedCustomUrlRequestTestHttpHost::value() {
    632   return value_;
    633 }
    634 
    635 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
    636 }
    637 
    638 URLRequestJob* TestJobInterceptor::MaybeCreateJob(
    639     URLRequest* request,
    640     NetworkDelegate* network_delegate) const {
    641   URLRequestJob* job = main_intercept_job_;
    642   main_intercept_job_ = NULL;
    643   return job;
    644 }
    645 
    646 void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
    647   main_intercept_job_ = job;
    648 }
    649 
    650 }  // namespace net
    651