Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / proxy / network_delegate_error_observer_unittest.cc
blob1f6330f0dd379dc96f3223daaccffb8554f6eb84
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"
7 #include "base/bind.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"
15 namespace net {
17 namespace {
19 class TestNetworkDelegate : public net::NetworkDelegate {
20 public:
21 TestNetworkDelegate() : got_pac_error_(false) {}
22 virtual ~TestNetworkDelegate() {}
24 bool got_pac_error() const { return got_pac_error_; }
26 private:
27 // net::NetworkDelegate implementation.
28 virtual int OnBeforeURLRequest(URLRequest* request,
29 const CompletionCallback& callback,
30 GURL* new_url) OVERRIDE {
31 return OK;
33 virtual int OnBeforeSendHeaders(URLRequest* request,
34 const CompletionCallback& callback,
35 HttpRequestHeaders* headers) OVERRIDE {
36 return OK;
38 virtual void OnSendHeaders(URLRequest* request,
39 const HttpRequestHeaders& headers) OVERRIDE {}
40 virtual int OnHeadersReceived(
41 URLRequest* request,
42 const CompletionCallback& callback,
43 const HttpResponseHeaders* original_response_headers,
44 scoped_refptr<HttpResponseHeaders>* override_response_headers,
45 GURL* allowed_unsafe_redirect_url) OVERRIDE {
46 return net::OK;
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(
61 URLRequest* request,
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 {
69 return true;
71 virtual bool OnCanSetCookie(const URLRequest& request,
72 const std::string& cookie_line,
73 CookieOptions* options) OVERRIDE {
74 return true;
76 virtual bool OnCanAccessFile(const net::URLRequest& request,
77 const base::FilePath& path) const OVERRIDE {
78 return true;
80 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE {
81 return false;
83 virtual int OnBeforeSocketStreamConnect(
84 SocketStream* stream,
85 const CompletionCallback& callback) OVERRIDE {
86 return OK;
89 bool got_pac_error_;
92 } // namespace
94 // Check that the OnPACScriptError method can be called from an arbitrary
95 // thread.
96 TEST(NetworkDelegateErrorObserverTest, CallOnThread) {
97 base::Thread thread("test_thread");
98 thread.Start();
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),
107 base::string16()));
108 thread.Stop();
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");
116 thread.Start();
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),
124 base::string16()));
125 thread.Stop();
126 base::MessageLoop::current()->RunUntilIdle();
127 // Shouldn't have crashed until here...
130 } // namespace net