Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2010 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 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/string16.h"
     12 #include "base/task.h"
     13 #include "googleurl/src/gurl.h"
     14 #include "net/http/http_auth_handler.h"
     15 #include "net/http/http_auth_handler_factory.h"
     16 
     17 namespace net {
     18 
     19 class HostResolver;
     20 
     21 // MockAuthHandler is used in tests to reliably trigger edge cases.
     22 class HttpAuthHandlerMock : public HttpAuthHandler {
     23  public:
     24   enum Resolve {
     25     RESOLVE_INIT,
     26     RESOLVE_SKIP,
     27     RESOLVE_SYNC,
     28     RESOLVE_ASYNC,
     29     RESOLVE_TESTED,
     30   };
     31 
     32   // The Factory class simply returns the same handler each time
     33   // CreateAuthHandler is called.
     34   class Factory : public HttpAuthHandlerFactory {
     35    public:
     36     Factory();
     37     virtual ~Factory();
     38 
     39     void set_mock_handler(HttpAuthHandler* handler, HttpAuth::Target target);
     40 
     41     void set_do_init_from_challenge(bool do_init_from_challenge) {
     42       do_init_from_challenge_ = do_init_from_challenge;
     43     }
     44 
     45     // HttpAuthHandlerFactory:
     46     virtual int CreateAuthHandler(HttpAuth::ChallengeTokenizer* 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);
     53 
     54    private:
     55     scoped_ptr<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                                    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   const GURL& request_url() const {
     78     return request_url_;
     79   }
     80 
     81   // HttpAuthHandler:
     82   virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
     83       HttpAuth::ChallengeTokenizer* challenge);
     84   virtual bool NeedsIdentity();
     85 
     86  protected:
     87   virtual bool Init(HttpAuth::ChallengeTokenizer* challenge);
     88 
     89   virtual int GenerateAuthTokenImpl(const string16* username,
     90                                     const string16* password,
     91                                     const HttpRequestInfo* request,
     92                                     CompletionCallback* callback,
     93                                     std::string* auth_token);
     94 
     95  private:
     96   void OnResolveCanonicalName();
     97 
     98   void OnGenerateAuthToken();
     99 
    100   Resolve resolve_;
    101   CompletionCallback* user_callback_;
    102   ScopedRunnableMethodFactory<HttpAuthHandlerMock> method_factory_;
    103   bool generate_async_;
    104   int generate_rv_;
    105   std::string* auth_token_;
    106   bool first_round_;
    107   bool connection_based_;
    108   GURL request_url_;
    109 };
    110 
    111 }  // namespace net
    112 
    113 #endif  // NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_
    114