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_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
) OVERRIDE
{
47 virtual void OnBeforeRedirect(URLRequest
* request
,
48 const GURL
& new_location
) OVERRIDE
{}
49 virtual void OnResponseStarted(URLRequest
* request
) OVERRIDE
{}
50 virtual void OnRawBytesRead(const URLRequest
& request
,
51 int bytes_read
) OVERRIDE
{}
52 virtual void OnCompleted(URLRequest
* request
, bool started
) OVERRIDE
{}
53 virtual void OnURLRequestDestroyed(URLRequest
* request
) OVERRIDE
{}
55 virtual void OnPACScriptError(int line_number
,
56 const string16
& error
) OVERRIDE
{
57 got_pac_error_
= true;
59 virtual AuthRequiredResponse
OnAuthRequired(
61 const AuthChallengeInfo
& auth_info
,
62 const AuthCallback
& callback
,
63 AuthCredentials
* credentials
) OVERRIDE
{
64 return AUTH_REQUIRED_RESPONSE_NO_ACTION
;
66 virtual bool OnCanGetCookies(const URLRequest
& request
,
67 const CookieList
& cookie_list
) OVERRIDE
{
70 virtual bool OnCanSetCookie(const URLRequest
& request
,
71 const std::string
& cookie_line
,
72 CookieOptions
* options
) OVERRIDE
{
75 virtual bool OnCanAccessFile(const net::URLRequest
& request
,
76 const FilePath
& path
) const OVERRIDE
{
79 virtual bool OnCanThrottleRequest(const URLRequest
& request
) const OVERRIDE
{
82 virtual int OnBeforeSocketStreamConnect(
84 const CompletionCallback
& callback
) OVERRIDE
{
87 virtual void OnRequestWaitStateChange(const net::URLRequest
& request
,
88 RequestWaitState state
) OVERRIDE
{
96 // Check that the OnPACScriptError method can be called from an arbitrary
98 TEST(NetworkDelegateErrorObserverTest
, CallOnThread
) {
99 base::Thread
thread("test_thread");
101 TestNetworkDelegate network_delegate
;
102 NetworkDelegateErrorObserver
103 observer(&network_delegate
,
104 base::MessageLoopProxy::current());
105 thread
.message_loop()->PostTask(
107 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError
,
108 base::Unretained(&observer
), 42, string16()));
110 MessageLoop::current()->RunUntilIdle();
111 ASSERT_TRUE(network_delegate
.got_pac_error());
114 // Check that passing a NULL network delegate works.
115 TEST(NetworkDelegateErrorObserverTest
, NoDelegate
) {
116 base::Thread
thread("test_thread");
118 NetworkDelegateErrorObserver
119 observer(NULL
, base::MessageLoopProxy::current());
120 thread
.message_loop()->PostTask(
122 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError
,
123 base::Unretained(&observer
), 42, string16()));
125 MessageLoop::current()->RunUntilIdle();
126 // Shouldn't have crashed until here...