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/message_loop/message_loop_proxy.h"
10 #include "base/threading/thread.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/network_delegate.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 class TestNetworkDelegate
: public net::NetworkDelegate
{
21 TestNetworkDelegate() : got_pac_error_(false) {}
22 virtual ~TestNetworkDelegate() {}
24 bool got_pac_error() const { return got_pac_error_
; }
27 // net::NetworkDelegate implementation.
28 virtual int OnBeforeURLRequest(URLRequest
* request
,
29 const CompletionCallback
& callback
,
30 GURL
* new_url
) OVERRIDE
{
33 virtual int OnBeforeSendHeaders(URLRequest
* request
,
34 const CompletionCallback
& callback
,
35 HttpRequestHeaders
* headers
) OVERRIDE
{
38 virtual void OnSendHeaders(URLRequest
* request
,
39 const HttpRequestHeaders
& headers
) OVERRIDE
{}
40 virtual int OnHeadersReceived(
42 const CompletionCallback
& callback
,
43 const HttpResponseHeaders
* original_response_headers
,
44 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
45 GURL
* allowed_unsafe_redirect_url
) OVERRIDE
{
48 virtual void OnBeforeRedirect(URLRequest
* request
,
49 const GURL
& new_location
) OVERRIDE
{}
50 virtual void OnResponseStarted(URLRequest
* request
) OVERRIDE
{}
51 virtual void OnRawBytesRead(const URLRequest
& request
,
52 int bytes_read
) OVERRIDE
{}
53 virtual void OnCompleted(URLRequest
* request
, bool started
) OVERRIDE
{}
54 virtual void OnURLRequestDestroyed(URLRequest
* request
) OVERRIDE
{}
56 virtual void OnPACScriptError(int line_number
,
57 const base::string16
& error
) OVERRIDE
{
58 got_pac_error_
= true;
60 virtual AuthRequiredResponse
OnAuthRequired(
62 const AuthChallengeInfo
& auth_info
,
63 const AuthCallback
& callback
,
64 AuthCredentials
* credentials
) OVERRIDE
{
65 return AUTH_REQUIRED_RESPONSE_NO_ACTION
;
67 virtual bool OnCanGetCookies(const URLRequest
& request
,
68 const CookieList
& cookie_list
) OVERRIDE
{
71 virtual bool OnCanSetCookie(const URLRequest
& request
,
72 const std::string
& cookie_line
,
73 CookieOptions
* options
) OVERRIDE
{
76 virtual bool OnCanAccessFile(const net::URLRequest
& request
,
77 const base::FilePath
& path
) const OVERRIDE
{
80 virtual bool OnCanThrottleRequest(const URLRequest
& request
) const OVERRIDE
{
83 virtual int OnBeforeSocketStreamConnect(
85 const CompletionCallback
& callback
) OVERRIDE
{
94 // Check that the OnPACScriptError method can be called from an arbitrary
96 TEST(NetworkDelegateErrorObserverTest
, CallOnThread
) {
97 base::Thread
thread("test_thread");
99 TestNetworkDelegate network_delegate
;
100 NetworkDelegateErrorObserver
observer(
101 &network_delegate
, base::MessageLoopProxy::current().get());
102 thread
.message_loop()
103 ->PostTask(FROM_HERE
,
104 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError
,
105 base::Unretained(&observer
),
109 base::MessageLoop::current()->RunUntilIdle();
110 ASSERT_TRUE(network_delegate
.got_pac_error());
113 // Check that passing a NULL network delegate works.
114 TEST(NetworkDelegateErrorObserverTest
, NoDelegate
) {
115 base::Thread
thread("test_thread");
117 NetworkDelegateErrorObserver
observer(
118 NULL
, base::MessageLoopProxy::current().get());
119 thread
.message_loop()
120 ->PostTask(FROM_HERE
,
121 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError
,
122 base::Unretained(&observer
),
126 base::MessageLoop::current()->RunUntilIdle();
127 // Shouldn't have crashed until here...