File: test_response_payload.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 (103 lines) | stat: -rw-r--r-- 4,173 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
// 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.

// Test support library for response payloads.

#ifndef CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_TEST_RESPONSE_PAYLOAD_H_
#define CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_TEST_RESPONSE_PAYLOAD_H_

#include <optional>

#include "base/values.h"
#include "chrome/browser/policy/messaging_layer/util/reporting_server_connector.h"
#include "components/reporting/util/statusor.h"

namespace reporting {

// Class to build response based on an input request.
// Example:
//
//   auto simple_successful_response = ResponseBuilder(std::move(request))
//                                       .Build();
//   auto failed_response = ResponseBuilder(std::move(request))
//                                       .SetSuccess(false)
//                                       .SetForceConfirm(true)
//                                       .Build();
//
// For the document of what response payload should look like, search for
// "{{{Note}}} ERP Response Payload Overview" in the codebase.
class ResponseBuilder {
 public:
  ResponseBuilder() = default;
  // We need a copy constructor here because base::Value::Dict has deleted its
  // copy constructor and gtest will implicitly copy a ResponseBuilder object
  // when it uses |MakeUploadEncryptedReportAction|, which we cannot work
  // around.
  ResponseBuilder(const ResponseBuilder& other);
  ResponseBuilder(ResponseBuilder&& other) = default;
  explicit ResponseBuilder(const base::Value::Dict& request);
  explicit ResponseBuilder(base::Value::Dict&& request);
  ResponseBuilder& SetForceConfirm(bool force_confirm);
  ResponseBuilder& SetNull(bool null);
  ResponseBuilder& SetRequest(const base::Value::Dict& request);
  ResponseBuilder& SetRequest(base::Value::Dict&& request);
  ResponseBuilder& SetSuccess(bool success);

  // Build the response to be fed to a
  // |::policy::CloudPolicyClient::ResponseCallback| object.
  //
  // Because we are focusing on testing here, the build here won't change
  // members of the |ResponseBuilder| instance so that it can be called repeated
  // for convenience. Additionally, this allows us to add more const qualifiers
  // throughout the tests so that we reduce chances of incorrect test code
  // (which itself isn't tested!).
  StatusOr<base::Value::Dict> Build() const;

 private:
  // The request that was sent to the server.
  base::Value::Dict request_;
  // Parameters that can be tuned.
  struct {
    // Whether response should be a success or not
    bool success = true;
    // Whether forceConfirm is set to true or not
    bool force_confirm = false;
    // Whether response should be null
    bool null = false;
  } params_;
};

// Helper functor to be used with EXPECT_CALL and |UploadEncryptedReport|. It
// takes a |ResponseBuilder| object, and builds the response dict based on it
// and the input request from |UploadEncryptedReport|. Example:
//
//
//   EXPECT_CALL(*client_,
//               UploadEncryptedReport(IsDataUploadRequestValid(), _, _))
//     .WillOnce(MakeUploadEncryptedReportAction(
//        std::move(ResponseBuilder()
//                    .SetForceConfirm(force_confirm_by_server))));
//
// This class should be able to handle the majority of cases for defining mocked
// |UploadEncryptedReport| actions. In other circumstances, consider feeding a
// lambda function that uses |ResponseBuilder| to .WillOnce()/.WillRepeatedly().
//
// We don't define |MakeUploadEncryptedReportAction| a lambda function here
// because the mutable nature and the size of the argument list makes it far
// less readable.
class MakeUploadEncryptedReportAction {
 public:
  explicit MakeUploadEncryptedReportAction(
      ResponseBuilder&& response_builder = ResponseBuilder());
  void operator()(base::Value::Dict request,
                  std::optional<base::Value::Dict> context,
                  ReportingServerConnector::ResponseCallback callback);

 private:
  ResponseBuilder response_builder_;
};

}  // namespace reporting

#endif  // CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_TEST_RESPONSE_PAYLOAD_H_