Clean up cache storage implementation DCHECKs.
[chromium-blink-merge.git] / net / proxy / network_delegate_error_observer_unittest.cc
blob6b9d6b0f1ce1c028c6893ad5fa6d1e5368085d3e
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/thread_task_runner_handle.h"
10 #include "base/threading/thread.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/network_delegate_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace net {
17 namespace {
19 class TestNetworkDelegate : public NetworkDelegateImpl {
20 public:
21 TestNetworkDelegate() : got_pac_error_(false) {}
22 ~TestNetworkDelegate() override {}
24 bool got_pac_error() const { return got_pac_error_; }
26 private:
27 // NetworkDelegate implementation.
28 int OnBeforeURLRequest(URLRequest* request,
29 const CompletionCallback& callback,
30 GURL* new_url) override {
31 return OK;
33 int OnBeforeSendHeaders(URLRequest* request,
34 const CompletionCallback& callback,
35 HttpRequestHeaders* headers) override {
36 return OK;
38 void OnSendHeaders(URLRequest* request,
39 const HttpRequestHeaders& headers) override {}
40 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 OK;
48 void OnBeforeRedirect(URLRequest* request,
49 const GURL& new_location) override {}
50 void OnResponseStarted(URLRequest* request) override {}
51 void OnRawBytesRead(const URLRequest& request, int bytes_read) override {}
52 void OnCompleted(URLRequest* request, bool started) override {}
53 void OnURLRequestDestroyed(URLRequest* request) override {}
55 void OnPACScriptError(int line_number, const base::string16& error) override {
56 got_pac_error_ = true;
58 AuthRequiredResponse OnAuthRequired(URLRequest* request,
59 const AuthChallengeInfo& auth_info,
60 const AuthCallback& callback,
61 AuthCredentials* credentials) override {
62 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
64 bool OnCanGetCookies(const URLRequest& request,
65 const CookieList& cookie_list) override {
66 return true;
68 bool OnCanSetCookie(const URLRequest& request,
69 const std::string& cookie_line,
70 CookieOptions* options) override {
71 return true;
73 bool OnCanAccessFile(const URLRequest& request,
74 const base::FilePath& path) const override {
75 return true;
77 bool OnCanThrottleRequest(const URLRequest& request) const override {
78 return false;
81 bool got_pac_error_;
84 // Check that the OnPACScriptError method can be called from an arbitrary
85 // thread.
86 TEST(NetworkDelegateErrorObserverTest, CallOnThread) {
87 base::Thread thread("test_thread");
88 thread.Start();
89 TestNetworkDelegate network_delegate;
90 NetworkDelegateErrorObserver observer(
91 &network_delegate, base::ThreadTaskRunnerHandle::Get().get());
92 thread.message_loop()
93 ->PostTask(FROM_HERE,
94 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError,
95 base::Unretained(&observer),
96 42,
97 base::string16()));
98 thread.Stop();
99 base::MessageLoop::current()->RunUntilIdle();
100 ASSERT_TRUE(network_delegate.got_pac_error());
103 // Check that passing a NULL network delegate works.
104 TEST(NetworkDelegateErrorObserverTest, NoDelegate) {
105 base::Thread thread("test_thread");
106 thread.Start();
107 NetworkDelegateErrorObserver observer(
108 NULL, base::ThreadTaskRunnerHandle::Get().get());
109 thread.message_loop()
110 ->PostTask(FROM_HERE,
111 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError,
112 base::Unretained(&observer),
114 base::string16()));
115 thread.Stop();
116 base::MessageLoop::current()->RunUntilIdle();
117 // Shouldn't have crashed until here...
120 } // namespace
122 } // namespace net