OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ | 5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ |
6 #define NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ | 6 #define NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "net/http/http_auth.h" |
12 #include "net/http/http_auth_handler.h" | 14 #include "net/http/http_auth_handler.h" |
13 #include "net/http/http_auth_handler_factory.h" | 15 #include "net/http/http_auth_handler_factory.h" |
14 #include "url/gurl.h" | 16 #include "url/gurl.h" |
15 | 17 |
16 namespace net { | 18 namespace net { |
17 | 19 |
18 class HostResolver; | 20 class HostResolver; |
19 | 21 |
20 // MockAuthHandler is used in tests to reliably trigger edge cases. | 22 // MockAuthHandler is used in tests to reliably trigger edge cases. |
21 class HttpAuthHandlerMock : public HttpAuthHandler { | 23 class HttpAuthHandlerMock : public HttpAuthHandler { |
22 public: | 24 public: |
23 enum Resolve { | |
24 RESOLVE_INIT, | |
25 RESOLVE_SKIP, | |
26 RESOLVE_SYNC, | |
27 RESOLVE_ASYNC, | |
28 RESOLVE_TESTED, | |
29 }; | |
30 | |
31 // The Factory class returns handlers in the order they were added via | 25 // The Factory class returns handlers in the order they were added via |
32 // AddMockHandler. | 26 // AddMockHandler. |
33 class Factory : public HttpAuthHandlerFactory { | 27 class Factory : public HttpAuthHandlerFactory { |
34 public: | 28 public: |
35 Factory(); | 29 Factory(); |
36 ~Factory() override; | 30 ~Factory() override; |
37 | 31 |
38 void AddMockHandler(HttpAuthHandler* handler, HttpAuth::Target target); | 32 void AddMockHandler(scoped_ptr<HttpAuthHandler> handler, |
| 33 CreateReason reason, |
| 34 HttpAuth::Target target); |
39 | 35 |
40 bool HaveAuthHandlers(HttpAuth::Target) const; | 36 bool HaveAuthHandlers(HttpAuth::Target) const; |
41 | 37 |
42 void set_do_init_from_challenge(bool do_init_from_challenge) { | |
43 do_init_from_challenge_ = do_init_from_challenge; | |
44 } | |
45 | |
46 // HttpAuthHandlerFactory: | 38 // HttpAuthHandlerFactory: |
47 int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge, | 39 int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge, |
48 HttpAuth::Target target, | 40 HttpAuth::Target target, |
49 const GURL& origin, | 41 const GURL& origin, |
50 CreateReason reason, | 42 CreateReason reason, |
51 int nonce_count, | 43 int nonce_count, |
52 const BoundNetLog& net_log, | 44 const BoundNetLog& net_log, |
53 scoped_ptr<HttpAuthHandler>* handler) override; | 45 scoped_ptr<HttpAuthHandler>* handler) override; |
54 | 46 |
55 private: | 47 private: |
56 ScopedVector<HttpAuthHandler> handlers_[HttpAuth::AUTH_NUM_TARGETS]; | 48 ScopedVector<HttpAuthHandler> |
57 bool do_init_from_challenge_; | 49 challenge_handlers_[HttpAuth::AUTH_NUM_TARGETS]; |
| 50 ScopedVector<HttpAuthHandler> |
| 51 preemptive_handlers_[HttpAuth::AUTH_NUM_TARGETS]; |
58 }; | 52 }; |
59 | 53 |
60 HttpAuthHandlerMock(); | 54 HttpAuthHandlerMock(); |
61 | 55 |
62 ~HttpAuthHandlerMock() override; | 56 ~HttpAuthHandlerMock() override; |
63 | 57 |
64 void SetResolveExpectation(Resolve resolve); | 58 void SetGenerateExpectation(bool async, int rv); |
65 | 59 |
66 virtual bool NeedsCanonicalName(); | 60 void set_expected_auth_scheme(const std::string& scheme) { |
67 | 61 expected_auth_scheme_ = scheme; |
68 virtual int ResolveCanonicalName(HostResolver* host_resolver, | 62 } |
69 const CompletionCallback& callback); | |
70 | |
71 | |
72 void SetGenerateExpectation(bool async, int rv); | |
73 | 63 |
74 void set_expect_multiple_challenges(bool expect_multiple_challenges) { | 64 void set_expect_multiple_challenges(bool expect_multiple_challenges) { |
75 expect_multiple_challenges_ = expect_multiple_challenges; | 65 expect_multiple_challenges_ = expect_multiple_challenges; |
76 } | 66 } |
77 | 67 |
78 void set_allows_default_credentials(bool allows_default_credentials) { | 68 void set_allows_default_credentials(bool allows_default_credentials) { |
79 allows_default_credentials_ = allows_default_credentials; | 69 allows_default_credentials_ = allows_default_credentials; |
80 } | 70 } |
81 | 71 |
82 bool expect_multiple_challenges() const { | |
83 return expect_multiple_challenges_; | |
84 } | |
85 | |
86 void set_allows_explicit_credentials(bool allows_explicit_credentials) { | 72 void set_allows_explicit_credentials(bool allows_explicit_credentials) { |
87 allows_explicit_credentials_ = allows_explicit_credentials; | 73 allows_explicit_credentials_ = allows_explicit_credentials; |
88 } | 74 } |
89 | 75 |
| 76 void set_auth_token(const std::string& auth_token) { |
| 77 auth_token_ = auth_token; |
| 78 } |
| 79 |
90 const GURL& request_url() const { | 80 const GURL& request_url() const { |
91 return request_url_; | 81 return request_url_; |
92 } | 82 } |
93 | 83 |
94 // HttpAuthHandler: | 84 // HttpAuthHandler: |
95 HttpAuth::AuthorizationResult HandleAnotherChallenge( | 85 HttpAuth::AuthorizationResult HandleAnotherChallenge( |
96 HttpAuthChallengeTokenizer* challenge) override; | 86 HttpAuthChallengeTokenizer* challenge) override; |
97 bool NeedsIdentity() override; | 87 bool NeedsIdentity() override; |
98 bool AllowsDefaultCredentials() override; | 88 bool AllowsDefaultCredentials() override; |
99 bool AllowsExplicitCredentials() override; | 89 bool AllowsExplicitCredentials() override; |
100 | 90 |
101 protected: | 91 protected: |
102 bool Init(HttpAuthChallengeTokenizer* challenge) override; | 92 bool Init(HttpAuthChallengeTokenizer* challenge) override; |
103 | 93 |
104 int GenerateAuthTokenImpl(const AuthCredentials* credentials, | 94 int GenerateAuthTokenImpl(const AuthCredentials* credentials, |
105 const HttpRequestInfo* request, | 95 const HttpRequestInfo* request, |
106 const CompletionCallback& callback, | 96 const CompletionCallback& callback, |
107 std::string* auth_token) override; | 97 std::string* auth_token) override; |
108 | 98 |
109 private: | 99 private: |
110 void OnResolveCanonicalName(); | |
111 | |
112 void OnGenerateAuthToken(); | 100 void OnGenerateAuthToken(); |
113 | 101 |
114 Resolve resolve_ = RESOLVE_INIT; | |
115 CompletionCallback callback_; | 102 CompletionCallback callback_; |
| 103 std::string expected_auth_scheme_; |
116 bool generate_async_ = false; | 104 bool generate_async_ = false; |
117 int generate_rv_; | 105 int generate_rv_ = 0; |
118 std::string* auth_token_ = nullptr; | 106 std::string auth_token_; |
| 107 std::string* generate_auth_token_buffer_ = nullptr; |
119 bool first_round_ = true; | 108 bool first_round_ = true; |
120 bool allows_default_credentials_ = false; | 109 bool allows_default_credentials_ = false; |
121 bool allows_explicit_credentials_ = true; | 110 bool allows_explicit_credentials_ = true; |
122 bool expect_multiple_challenges_ = false; | 111 bool expect_multiple_challenges_ = false; |
123 GURL request_url_; | 112 GURL request_url_; |
124 base::WeakPtrFactory<HttpAuthHandlerMock> weak_factory_; | 113 base::WeakPtrFactory<HttpAuthHandlerMock> weak_factory_; |
125 }; | 114 }; |
126 | 115 |
127 } // namespace net | 116 } // namespace net |
128 | 117 |
129 #endif // NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ | 118 #endif // NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ |
OLD | NEW |