fake_gaia.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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
15namespace base {
16class DictionaryValue;
17}
18
19namespace net {
20namespace test_server {
21class BasicHttpResponse;
22struct HttpRequest;
23class 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.
30class 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 auth token, which can be a refresh token or an
59  // login-scoped access token for the token minting endpoint. Note that the
60  // scope and audience requested by the client need to match the token_info.
61  void IssueOAuthToken(const std::string& auth_token,
62                       const AccessTokenInfo& token_info);
63
64 private:
65  typedef std::multimap<std::string, AccessTokenInfo> AccessTokenInfoMap;
66
67  // Formats a JSON response with the data in |response_dict|.
68  void FormatJSONResponse(const base::DictionaryValue& response_dict,
69                          net::test_server::BasicHttpResponse* http_response);
70
71  // Returns the access token associated with |auth_token| that matches the
72  // given |client_id| and |scope_string|. If |scope_string| is empty, the first
73  // token satisfying the other criteria is returned. Returns NULL if no token
74  // matches.
75  const AccessTokenInfo* GetAccessTokenInfo(const std::string& auth_token,
76                                            const std::string& client_id,
77                                            const std::string& scope_string)
78      const;
79
80  // Extracts the parameter named |key| from |query| and places it in |value|.
81  // Returns false if no parameter is found.
82  static bool GetQueryParameter(const std::string& query,
83                                const std::string& key,
84                                std::string* value);
85
86  AccessTokenInfoMap access_token_info_map_;
87  std::string service_login_response_;
88
89  DISALLOW_COPY_AND_ASSIGN(FakeGaia);
90};
91
92#endif  // GOOGLE_APIS_GAIA_FAKE_GAIA_H_
93