Home | History | Annotate | Download | only in http
      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.
      4 
      5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_
      6 #define NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_
      7 
      8 #include <string>
      9 
     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"
     15 
     16 namespace net {
     17 
     18 class HostResolver;
     19 
     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,
     29   };
     30 
     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     virtual ~Factory();
     37 
     38     void AddMockHandler(HttpAuthHandler* handler, HttpAuth::Target target);
     39 
     40     void set_do_init_from_challenge(bool do_init_from_challenge) {
     41       do_init_from_challenge_ = do_init_from_challenge;
     42     }
     43 
     44     // HttpAuthHandlerFactory:
     45     virtual int CreateAuthHandler(
     46         HttpAuthChallengeTokenizer* challenge,
     47         HttpAuth::Target target,
     48         const GURL& origin,
     49         CreateReason reason,
     50         int nonce_count,
     51         const BoundNetLog& net_log,
     52         scoped_ptr<HttpAuthHandler>* handler) OVERRIDE;
     53 
     54    private:
     55     ScopedVector<HttpAuthHandler> handlers_[HttpAuth::AUTH_NUM_TARGETS];
     56     bool do_init_from_challenge_;
     57   };
     58 
     59   HttpAuthHandlerMock();
     60 
     61   virtual ~HttpAuthHandlerMock();
     62 
     63   void SetResolveExpectation(Resolve resolve);
     64 
     65   virtual bool NeedsCanonicalName();
     66 
     67   virtual int ResolveCanonicalName(HostResolver* host_resolver,
     68                                    const CompletionCallback& callback);
     69 
     70 
     71   void SetGenerateExpectation(bool async, int rv);
     72 
     73   void set_connection_based(bool connection_based) {
     74     connection_based_ = connection_based;
     75   }
     76 
     77   void set_allows_default_credentials(bool allows_default_credentials) {
     78     allows_default_credentials_ = allows_default_credentials;
     79   }
     80 
     81   void set_allows_explicit_credentials(bool allows_explicit_credentials) {
     82     allows_explicit_credentials_ = allows_explicit_credentials;
     83   }
     84 
     85   const GURL& request_url() const {
     86     return request_url_;
     87   }
     88 
     89   // HttpAuthHandler:
     90   virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
     91       HttpAuthChallengeTokenizer* challenge) OVERRIDE;
     92   virtual bool NeedsIdentity() OVERRIDE;
     93   virtual bool AllowsDefaultCredentials() OVERRIDE;
     94   virtual bool AllowsExplicitCredentials() OVERRIDE;
     95 
     96  protected:
     97   virtual bool Init(HttpAuthChallengeTokenizer* challenge) OVERRIDE;
     98 
     99   virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials,
    100                                     const HttpRequestInfo* request,
    101                                     const CompletionCallback& callback,
    102                                     std::string* auth_token) OVERRIDE;
    103 
    104  private:
    105   void OnResolveCanonicalName();
    106 
    107   void OnGenerateAuthToken();
    108 
    109   Resolve resolve_;
    110   CompletionCallback callback_;
    111   bool generate_async_;
    112   int generate_rv_;
    113   std::string* auth_token_;
    114   bool first_round_;
    115   bool connection_based_;
    116   bool allows_default_credentials_;
    117   bool allows_explicit_credentials_;
    118   GURL request_url_;
    119   base::WeakPtrFactory<HttpAuthHandlerMock> weak_factory_;
    120 };
    121 
    122 }  // namespace net
    123 
    124 #endif  // NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_
    125