File: test_authorization_server.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 5,235 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_PRINTING_OAUTH2_TEST_AUTHORIZATION_SERVER_H_
#define CHROME_BROWSER_ASH_PRINTING_OAUTH2_TEST_AUTHORIZATION_SERVER_H_

#include <string>

#include "base/containers/flat_map.h"
#include "base/containers/queue.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chrome/browser/ash/printing/oauth2/status_code.h"
#include "chromeos/printing/uri.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"

namespace network {
class SharedURLLoaderFactory;
}  // namespace network

namespace ash {
namespace printing {
namespace oauth2 {

// Helper function that parses parameters formatted as URL query. Returns false
// <=> the parsing failed (at least one of the keys is empty or repeated). The
// results are saved to `results` that is cleared at the beginning.
bool ParseURLParameters(const std::string& params_str,
                        base::flat_map<std::string, std::string>& results);

// Represents results returned by callback void(StatusCode, const string&).
struct CallbackResult {
  StatusCode status = StatusCode::kUnexpectedError;
  std::string data;
};

// Helper function that returns callback saving its parameters to a given
// structure.
StatusCallback BindResult(CallbackResult& target);

// Builds metadata returned in the response for Metadata request.
base::Value::Dict BuildMetadata(const std::string& authorization_server_uri,
                                const std::string& authorization_uri,
                                const std::string& token_uri,
                                const std::string& registration_uri = "",
                                const std::string& revocation_uri = "");

// Simulates Authorization Server. It contains base::test::TaskEnvironment that
// can be accessed via TaskEnvironment() method.
class FakeAuthorizationServer {
 public:
  FakeAuthorizationServer();
  ~FakeAuthorizationServer();

  base::test::TaskEnvironment& TaskEnvironment() { return task_environment_; }

  // Returns the URLLoaderFactory that must be used to send HTTP requests and
  // receive responses.
  scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory() {
    return fake_server_.GetSafeWeakWrapper();
  }

  // Processes all pending tasks and get the next request from the fake server.
  // Checks if it is a GET `url` request without a payload. The method returns
  // an empty string <=> there is no errors. Otherwise it returns an error
  // message.
  std::string ReceiveGET(const std::string& url);

  // Processes all pending tasks and get the next request from the fake server.
  // Checks if it is a POST `url` request with a JSON payload. The payload is
  // parsed and saved to `out_params` that is always overwritten. The method
  // returns an empty string <=> there is no errors. Otherwise it returns
  // an error message.
  std::string ReceivePOSTWithJSON(const std::string& url,
                                  base::Value::Dict& out_params);

  // Processes all pending tasks and get the next request from the fake server.
  // Checks if it is a POST `url` request with a payload containing URL-encoded
  // parameters. The payload is parsed and saved to `out_params` that is always
  // overwritten. The method returns an empty string <=> there is no errors.
  // Otherwise it returns an error message.
  std::string ReceivePOSTWithURLParams(
      const std::string& url,
      base::flat_map<std::string, std::string>& out_params);

  // Sends a response to the current pending request with the JSON content given
  // in `params` and processes all pending tasks. The call to this method must
  // be preceded by a previous call to one of Receive* methods.
  void ResponseWithJSON(net::HttpStatusCode status,
                        const base::Value::Dict& params);

 private:
  // Processes all pending tasks and get the next request from the fake server.
  // Returns an error message if there is no requests or the request doesn't
  // match `method`, `url` and `content_type. The payload is saved to `content`.
  // The method returns an empty string <=> there is no errors.
  std::string GetNextRequest(const std::string& method,
                             const std::string& url,
                             const std::string& content_type,
                             std::string& content);

  // Payloads of the incoming requests are stored here (FIFO).
  base::queue<std::string> upload_data_;

  // The pending request that is currently being processed.
  raw_ptr<network::TestURLLoaderFactory::PendingRequest, DanglingUntriaged>
      current_request_ = nullptr;

  network::TestURLLoaderFactory fake_server_;
  base::test::TaskEnvironment task_environment_;
};

}  // namespace oauth2
}  // namespace printing
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_PRINTING_OAUTH2_TEST_AUTHORIZATION_SERVER_H_