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.
5 #include "net/proxy/network_delegate_error_observer.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/network_delegate_impl.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 class TestNetworkDelegate
: public NetworkDelegateImpl
{
23 TestNetworkDelegate() : got_pac_error_(false) {}
24 ~TestNetworkDelegate() override
{}
26 bool got_pac_error() const { return got_pac_error_
; }
29 // NetworkDelegate implementation.
30 int OnBeforeURLRequest(URLRequest
* request
,
31 const CompletionCallback
& callback
,
32 GURL
* new_url
) override
{
35 int OnBeforeSendHeaders(URLRequest
* request
,
36 const CompletionCallback
& callback
,
37 HttpRequestHeaders
* headers
) override
{
40 void OnSendHeaders(URLRequest
* request
,
41 const HttpRequestHeaders
& headers
) override
{}
42 int OnHeadersReceived(
44 const CompletionCallback
& callback
,
45 const HttpResponseHeaders
* original_response_headers
,
46 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
47 GURL
* allowed_unsafe_redirect_url
) override
{
50 void OnBeforeRedirect(URLRequest
* request
,
51 const GURL
& new_location
) override
{}
52 void OnResponseStarted(URLRequest
* request
) override
{}
53 void OnRawBytesRead(const URLRequest
& request
, int bytes_read
) override
{}
54 void OnCompleted(URLRequest
* request
, bool started
) override
{}
55 void OnURLRequestDestroyed(URLRequest
* request
) override
{}
57 void OnPACScriptError(int line_number
, const base::string16
& error
) override
{
58 got_pac_error_
= true;
60 AuthRequiredResponse
OnAuthRequired(URLRequest
* request
,
61 const AuthChallengeInfo
& auth_info
,
62 const AuthCallback
& callback
,
63 AuthCredentials
* credentials
) override
{
64 return AUTH_REQUIRED_RESPONSE_NO_ACTION
;
66 bool OnCanGetCookies(const URLRequest
& request
,
67 const CookieList
& cookie_list
) override
{
70 bool OnCanSetCookie(const URLRequest
& request
,
71 const std::string
& cookie_line
,
72 CookieOptions
* options
) override
{
75 bool OnCanAccessFile(const URLRequest
& request
,
76 const base::FilePath
& path
) const override
{
83 // Check that the OnPACScriptError method can be called from an arbitrary
85 TEST(NetworkDelegateErrorObserverTest
, CallOnThread
) {
86 base::Thread
thread("test_thread");
88 TestNetworkDelegate network_delegate
;
89 NetworkDelegateErrorObserver
observer(
90 &network_delegate
, base::ThreadTaskRunnerHandle::Get().get());
91 thread
.task_runner()->PostTask(
92 FROM_HERE
, base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError
,
93 base::Unretained(&observer
), 42, base::string16()));
95 base::MessageLoop::current()->RunUntilIdle();
96 ASSERT_TRUE(network_delegate
.got_pac_error());
99 // Check that passing a NULL network delegate works.
100 TEST(NetworkDelegateErrorObserverTest
, NoDelegate
) {
101 base::Thread
thread("test_thread");
103 NetworkDelegateErrorObserver
observer(
104 NULL
, base::ThreadTaskRunnerHandle::Get().get());
105 thread
.task_runner()->PostTask(
106 FROM_HERE
, base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError
,
107 base::Unretained(&observer
), 42, base::string16()));
109 base::MessageLoop::current()->RunUntilIdle();
110 // Shouldn't have crashed until here...