File: expectation_handler.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 (111 lines) | stat: -rw-r--r-- 3,980 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_TEST_EMBEDDED_TEST_SERVER_EXPECTATION_HANDLER_H_
#define NET_TEST_EMBEDDED_TEST_SERVER_EXPECTATION_HANDLER_H_

#include <map>
#include <memory>
#include <string>
#include <string_view>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/synchronization/lock.h"
#include "base/test/test_future.h"
#include "base/thread_annotations.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

namespace net::test_server {

// ExpectationHandler provides a simplified way to handle HTTP requests in
// tests. It allows pre-registering responses for specific URLs and
// automatically sends them when matching requests are received.
//
// Basic usage:
//   EmbeddedTestServer server;
//   ExpectationHandler handler(&server);
//
//   // Start the server
//   ASSERT_TRUE(server.Start());
//
//   // Register a response for a specific URL Path
//   handler.OnRequest("/test.html")
//       .RespondWith("text/html", "<html>Test</html>");
//
//   shell()->web_contents()->GetController().LoadURLWithParams(
//     NavigationController::LoadURLParams(server.GetURL("/test.html")));
//
//   WaitForLoadStop(shell()->web_contents());
//
class ExpectationHandler {
 public:
  // Forward declaration of the ResponseBuilder class
  class ResponseBuilder;

  explicit ExpectationHandler(EmbeddedTestServer* embedded_test_server);
  ~ExpectationHandler();

  // Registers a response for a URL Path and returns a ResponseBuilder
  // for configuring the response.
  ResponseBuilder OnRequest(std::string_view path, bool is_prefix = false);

  // Handles an HTTP request and returns a response.
  // This is called by the EmbeddedTestServer when a request is received.
  std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request);

 private:
  // Stores the configuration for a URL response
  struct UrlResponseConfig;
  // Helper method to apply a configuration and create a response
  std::unique_ptr<HttpResponse> ApplyConfig(const HttpRequest& request,
                                            raw_ptr<UrlResponseConfig> config);

  std::map<std::string_view, std::unique_ptr<UrlResponseConfig>> url_responses_
      GUARDED_BY(lock_);
  raw_ref<EmbeddedTestServer> embedded_test_server_;
  base::Lock lock_;
};

// ResponseBuilder provides a fluent interface for configuring responses.
// It is created by ExpectationHandler::OnRequest() and allows chaining
// method calls to configure the response.
class ExpectationHandler::ResponseBuilder {
 public:
  ResponseBuilder(ExpectationHandler& handler,
                  std::string_view path,
                  bool is_prefix);

  ~ResponseBuilder();

  // Sets the content type and content for the HTTP response. The content_type
  // parameter specifies the MIME type (like "text/html") and content contains
  // the response body. Returns a reference to this ResponseBuilder to allow
  // method chaining.
  ResponseBuilder& RespondWith(
      std::string_view content_type = std::string_view("text/html"),
      std::string_view content = "");

  ResponseBuilder& RespondWith(
      const HttpStatusCode status_code,
      std::string_view content_type = std::string_view("text/html"),
      std::string_view content = "");

  // Associates a TestFuture with this response.
  // When a request for this URL is received, the TestFuture will be
  // fulfilled with the HttpRequest object.
  ResponseBuilder& SetValue(base::test::TestFuture<HttpRequest>& future);

 private:
  raw_ref<ExpectationHandler> handler_;
  std::string_view path_;
  raw_ptr<UrlResponseConfig> config_;
};

}  // namespace net::test_server

#endif  // NET_TEST_EMBEDDED_TEST_SERVER_EXPECTATION_HANDLER_H_