- add sources.
[platform/framework/web/crosswalk.git] / src / google_apis / gaia / fake_gaia.h
1 // Copyright (c) 2013 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 GOOGLE_APIS_GAIA_FAKE_GAIA_H_
6 #define GOOGLE_APIS_GAIA_FAKE_GAIA_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14
15 namespace base {
16 class DictionaryValue;
17 }
18
19 namespace net {
20 namespace test_server {
21 class BasicHttpResponse;
22 struct HttpRequest;
23 class HttpResponse;
24 }
25 }
26
27 // This is a test helper that implements a fake GAIA service for use in browser
28 // tests. It's mainly intended for use with EmbeddedTestServer, for which it can
29 // be registered as an additional request handler.
30 class FakeGaia {
31  public:
32   typedef std::set<std::string> ScopeSet;
33
34   // Access token details used for token minting and the token info endpoint.
35   struct AccessTokenInfo {
36     AccessTokenInfo();
37     ~AccessTokenInfo();
38
39     std::string token;
40     std::string issued_to;
41     std::string audience;
42     std::string user_id;
43     ScopeSet scopes;
44     int expires_in;
45     std::string email;
46   };
47
48   FakeGaia();
49   ~FakeGaia();
50
51   // Handles a request and returns a response if the request was recognized as a
52   // GAIA request. Note that this respects the switches::kGaiaUrl and friends so
53   // that this can used with EmbeddedTestServer::RegisterRequestHandler().
54   scoped_ptr<net::test_server::HttpResponse> HandleRequest(
55       const net::test_server::HttpRequest& request);
56
57   // Configures an OAuth2 token that'll be returned when a client requests an
58   // access token for the given refresh token.
59   void IssueOAuthToken(const std::string& refresh_token,
60                        const AccessTokenInfo& token_info);
61
62  private:
63   typedef std::map<std::string, AccessTokenInfo> AccessTokenInfoMap;
64
65   // Formats a JSON response with the data in |response_dict|.
66   void FormatJSONResponse(const base::DictionaryValue& response_dict,
67                           net::test_server::BasicHttpResponse* http_response);
68
69   // Returns the access token associated with |refresh_token| or NULL if not
70   // found.
71   const AccessTokenInfo* GetAccessTokenInfo(
72       const std::string& refresh_token) const;
73
74   // Extracts the parameter named |key| from |query| and places it in |value|.
75   // Returns false if no parameter is found.
76   static bool GetQueryParameter(const std::string& query,
77                                 const std::string& key,
78                                 std::string* value);
79
80   AccessTokenInfoMap access_token_info_map_;
81   std::string service_login_response_;
82
83   DISALLOW_COPY_AND_ASSIGN(FakeGaia);
84 };
85
86 #endif  // GOOGLE_APIS_GAIA_FAKE_GAIA_H_