File: request_matcher.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 (89 lines) | stat: -rw-r--r-- 3,342 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
// Copyright 2023 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_UPDATER_TEST_REQUEST_MATCHER_H_
#define CHROME_UPDATER_TEST_REQUEST_MATCHER_H_

#include <list>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/functional/callback_forward.h"
#include "base/version.h"
#include "chrome/updater/update_service.h"
#include "chrome/updater/updater_scope.h"
#include "chrome/updater/updater_version.h"

class GURL;

namespace updater::test {

struct HttpRequest;

namespace request {
// Defines a generic matcher to match expectations for a request.
using Matcher = base::RepeatingCallback<bool(const HttpRequest&)>;

// Defines a group of matchers which all must pass in order to match a request.
// This allows for combining several matchers when matching a single request.
using MatcherGroup = std::list<Matcher>;

// Returns a matcher which returns true if the `expected_path_regex` fully
// matches the request path.
[[nodiscard]] Matcher GetPathMatcher(const std::string& expected_path_regex);

// Returns a matcher which returns true if all the `expected_headers` are
// found. `expected_headers` is a map of (header_name, header_content_regex).
[[nodiscard]] Matcher GetHeaderMatcher(
    const base::flat_map<std::string, std::string> expected_headers);

// Returns a matcher which returns true if the request has updater's user-agent.
Matcher GetUpdaterUserAgentMatcher(
    const base::Version& updater_version = base::Version(kUpdaterVersion));

// Returns a matcher which returns true if the request is actually intended
// for the `target_url` (uses current server as a proxy).
Matcher GetTargetURLMatcher(GURL target_url);

// Returns a matcher which returns true if all regexex are found in the given
// order.
[[nodiscard]] Matcher GetContentMatcher(
    const std::vector<std::string>& expected_content_regex_sequence);

// Returns a matcher which returns true if request scope is same as the given
// value.
[[nodiscard]] Matcher GetScopeMatcher(UpdaterScope scope);

// Returns a matcher which returns true if the app priority in request matches
// the given value.
[[nodiscard]] Matcher GetAppPriorityMatcher(const std::string& app_id,
                                            UpdateService::Priority priority);

// Returns a matcher which checks that update is enabled for updater itself.
[[nodiscard]] Matcher GetUpdaterEnableUpdatesMatcher();

// Defines the expectations of a form in a multipart content.
struct FormExpectations {
  FormExpectations(const std::string& name, std::vector<std::string> regexes);
  FormExpectations(const FormExpectations&);
  FormExpectations& operator=(const FormExpectations& other);
  ~FormExpectations();

  std::string name;

  // A list of regexes to partially match the form data in the given order.
  std::vector<std::string> regex_sequence;
};

// Returns a matcher, which returns true if the forms are separated by the
// form-data boundary, each form has the expected name, and all form-regexex are
// found in the given order.
[[nodiscard]] Matcher GetMultipartContentMatcher(
    const std::vector<FormExpectations>& form_expections);

}  // namespace request
}  // namespace updater::test

#endif  // CHROME_UPDATER_TEST_REQUEST_MATCHER_H_