File: mock_crash_endpoint.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 2,955 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CRASH_CONTENT_BROWSER_ERROR_REPORTING_MOCK_CRASH_ENDPOINT_H_
#define COMPONENTS_CRASH_CONTENT_BROWSER_ERROR_REPORTING_MOCK_CRASH_ENDPOINT_H_

#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "net/http/http_status_code.h"

namespace net {
namespace test_server {
class EmbeddedTestServer;
class HttpResponse;
struct HttpRequest;
}  // namespace test_server
}  // namespace net

// Installs a mock crash server endpoint.
class MockCrashEndpoint {
 public:
  struct Report {
    Report(std::string query_value, std::string content_value);
    std::string query;
    std::string content;
  };

  explicit MockCrashEndpoint(net::test_server::EmbeddedTestServer* test_server);
  MockCrashEndpoint(const MockCrashEndpoint&) = delete;
  MockCrashEndpoint& operator=(const MockCrashEndpoint&) = delete;
  ~MockCrashEndpoint();

  // Returns the last received report, or waits for a query and returns it.
  Report WaitForReport();

  // Returns the last report received, if any.
  const std::optional<Report>& last_report() const { return last_report_; }

  // Clears last report so that WaitForReport will wait for another report.
  // Does not clear report_count() or all_reports().
  void clear_last_report() { last_report_.reset(); }

  // Get the number of reports received since this object was created.
  int report_count() const { return report_count_; }

  // Retrieves all the reports received by this mock crash endpoint.
  const std::vector<Report>& all_reports() const { return all_reports_; }

  // Configures whether the mock crash reporter client has user-consent for
  // submitting crash reports.
  void set_consented(bool consented) { consented_ = consented; }

  // Set the response that the server will return.
  void set_response(net::HttpStatusCode code, const std::string& content) {
    response_code_ = code;
    response_content_ = content;
  }

  // Returns the URL that tests should send crash reports to.
  std::string GetCrashEndpointURL() const;

 private:
  class Client;

  std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
      const net::test_server::HttpRequest& request);

  raw_ptr<net::test_server::EmbeddedTestServer> test_server_;
  std::unique_ptr<Client> client_;
  std::optional<Report> last_report_;
  std::vector<Report> all_reports_;
  int report_count_ = 0;
  bool consented_ = true;
  base::RepeatingClosure on_report_;
  net::HttpStatusCode response_code_ = net::HTTP_OK;
  std::string response_content_ = "123";
};

std::ostream& operator<<(std::ostream& out,
                         const MockCrashEndpoint::Report& report);

#endif  // COMPONENTS_CRASH_CONTENT_BROWSER_ERROR_REPORTING_MOCK_CRASH_ENDPOINT_H_