xref: /aosp_15_r20/external/cronet/net/test/url_request/url_request_mock_data_job.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_DATA_JOB_H_
6 #define NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_DATA_JOB_H_
7 
8 #include <stddef.h>
9 
10 #include <optional>
11 #include <string>
12 
13 #include "base/memory/weak_ptr.h"
14 #include "net/url_request/url_request_job.h"
15 
16 namespace net {
17 
18 class URLRequest;
19 
20 // Mock data job, which synchronously returns data repeated multiple times. If
21 // |request_client_certificate| is true, then this job will request client
22 // certificate before proceeding.
23 class URLRequestMockDataJob : public URLRequestJob {
24  public:
25   URLRequestMockDataJob(URLRequest* request,
26                         const std::string& data,
27                         int data_repeat_count,
28                         bool request_client_certificate);
29   ~URLRequestMockDataJob() override;
30 
31   void Start() override;
32   int ReadRawData(IOBuffer* buf, int buf_size) override;
33   void GetResponseInfo(HttpResponseInfo* info) override;
34   void ContinueWithCertificate(
35       scoped_refptr<X509Certificate> client_cert,
36       scoped_refptr<SSLPrivateKey> client_private_key) override;
37 
38   // Adds the testing URLs to the URLRequestFilter.
39   static void AddUrlHandler();
40   static void AddUrlHandlerForHostname(const std::string& hostname);
41 
42   // Given data and repeat cound, constructs a mock URL that will return that
43   // data repeated |repeat_count| times when started.  |data| must be safe for
44   // URL.
45   static GURL GetMockHttpUrl(const std::string& data, int repeat_count);
46   static GURL GetMockHttpsUrl(const std::string& data, int repeat_count);
47 
48   // Constructs Mock URL which will request client certificate and return the
49   // word "data" as the response.
50   static GURL GetMockUrlForClientCertificateRequest();
51 
52   // URLRequestFailedJob must be added as a handler for |hostname| for
53   // the returned URL to return |net_error|.
54   static GURL GetMockHttpUrlForHostname(const std::string& hostname,
55                                         const std::string& data,
56                                         int repeat_count);
57   static GURL GetMockHttpsUrlForHostname(const std::string& hostname,
58                                          const std::string& data,
59                                          int repeat_count);
60 
61   // Overrides response headers in the mocked response.
62   void OverrideResponseHeaders(const std::string& headers);
63 
64  private:
65   void GetResponseInfoConst(HttpResponseInfo* info) const;
66 
67   void StartAsync();
68 
69   std::optional<std::string> headers_;
70   std::string data_;
71   size_t data_offset_ = 0;
72   bool request_client_certificate_;
73   base::WeakPtrFactory<URLRequestMockDataJob> weak_factory_{this};
74 };
75 
76 }  // namespace net
77 
78 #endif  // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_DATA_JOB_H_
79