Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / http / http_auth_handler_mock.h
blob32b416d2fba7424440accfdef5c9fb44eb33120f
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
3 // found in the LICENSE file.
5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_
6 #define NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_
8 #include <string>
10 #include "base/memory/scoped_vector.h"
11 #include "base/memory/weak_ptr.h"
12 #include "net/http/http_auth_handler.h"
13 #include "net/http/http_auth_handler_factory.h"
14 #include "url/gurl.h"
16 namespace net {
18 class HostResolver;
20 // MockAuthHandler is used in tests to reliably trigger edge cases.
21 class HttpAuthHandlerMock : public HttpAuthHandler {
22 public:
23 enum Resolve {
24 RESOLVE_INIT,
25 RESOLVE_SKIP,
26 RESOLVE_SYNC,
27 RESOLVE_ASYNC,
28 RESOLVE_TESTED,
31 // The Factory class returns handlers in the order they were added via
32 // AddMockHandler.
33 class Factory : public HttpAuthHandlerFactory {
34 public:
35 Factory();
36 ~Factory() override;
38 void AddMockHandler(HttpAuthHandler* handler, HttpAuth::Target target);
40 void set_do_init_from_challenge(bool do_init_from_challenge) {
41 do_init_from_challenge_ = do_init_from_challenge;
44 // HttpAuthHandlerFactory:
45 int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge,
46 HttpAuth::Target target,
47 const GURL& origin,
48 CreateReason reason,
49 int nonce_count,
50 const BoundNetLog& net_log,
51 scoped_ptr<HttpAuthHandler>* handler) override;
53 private:
54 ScopedVector<HttpAuthHandler> handlers_[HttpAuth::AUTH_NUM_TARGETS];
55 bool do_init_from_challenge_;
58 HttpAuthHandlerMock();
60 ~HttpAuthHandlerMock() override;
62 void SetResolveExpectation(Resolve resolve);
64 virtual bool NeedsCanonicalName();
66 virtual int ResolveCanonicalName(HostResolver* host_resolver,
67 const CompletionCallback& callback);
70 void SetGenerateExpectation(bool async, int rv);
72 void set_connection_based(bool connection_based) {
73 connection_based_ = connection_based;
76 void set_allows_default_credentials(bool allows_default_credentials) {
77 allows_default_credentials_ = allows_default_credentials;
80 void set_allows_explicit_credentials(bool allows_explicit_credentials) {
81 allows_explicit_credentials_ = allows_explicit_credentials;
84 const GURL& request_url() const {
85 return request_url_;
88 // HttpAuthHandler:
89 HttpAuth::AuthorizationResult HandleAnotherChallenge(
90 HttpAuthChallengeTokenizer* challenge) override;
91 bool NeedsIdentity() override;
92 bool AllowsDefaultCredentials() override;
93 bool AllowsExplicitCredentials() override;
95 protected:
96 bool Init(HttpAuthChallengeTokenizer* challenge) override;
98 int GenerateAuthTokenImpl(const AuthCredentials* credentials,
99 const HttpRequestInfo* request,
100 const CompletionCallback& callback,
101 std::string* auth_token) override;
103 private:
104 void OnResolveCanonicalName();
106 void OnGenerateAuthToken();
108 Resolve resolve_;
109 CompletionCallback callback_;
110 bool generate_async_;
111 int generate_rv_;
112 std::string* auth_token_;
113 bool first_round_;
114 bool connection_based_;
115 bool allows_default_credentials_;
116 bool allows_explicit_credentials_;
117 GURL request_url_;
118 base::WeakPtrFactory<HttpAuthHandlerMock> weak_factory_;
121 } // namespace net
123 #endif // NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_