xref: /aosp_15_r20/external/cronet/components/cronet/testing/test_server/test_server.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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 COMPONENTS_CRONET_TESTING_TEST_SERVER_TEST_SERVER_H_
6 #define COMPONENTS_CRONET_TESTING_TEST_SERVER_TEST_SERVER_H_
7 
8 #include <string>
9 
10 #include "net/test/embedded_test_server/embedded_test_server.h"
11 
12 namespace base {
13 class FilePath;
14 }  // namespace base
15 
16 namespace cronet {
17 
18 class TestServer {
19  public:
20   // Starts the server serving files from default test data directory.
21   // Returns true if started, false if server is already running.
22   // This will run the server in default mode (HTTP/1 with no SSL)
23   static bool Start();
24 
25   // Starts the server serving files from |test_files_root| directory.
26   // Returns true if started, false if server is already running.
27   // The provided server will support either HTTP/1 or HTTPS/1 depending
28   // on the |type| provided.
29   static bool StartServeFilesFromDirectory(
30       const base::FilePath& test_files_root,
31       net::EmbeddedTestServer::Type type,
32       net::EmbeddedTestServer::ServerCertificate cert);
33 
34   // Shuts down the server.
35   static void Shutdown();
36 
37   // Returns port number of the server.
38   static int GetPort();
39   // Returns host:port string of the server.
40   static std::string GetHostPort();
41 
42   // Returns URL which responds with the body "The quick brown fox jumps over
43   // the lazy dog".
44   static std::string GetSimpleURL();
45   // Returns URL which respond with echo of the method in response body.
46   static std::string GetEchoMethodURL();
47   // Returns URL which respond with echo of header with |header_name| in
48   // response body.
49   static std::string GetEchoHeaderURL(const std::string& header_name);
50   // Returns URL which responds with "The quick brown fox jumps over the lazy
51   // dog" in specified encoding.
52   static std::string GetUseEncodingURL(const std::string& encoding_name);
53   // Returns URL which respond with setting cookie to |cookie_line| and echo it
54   // in response body.
55   static std::string GetSetCookieURL(const std::string& cookie_line);
56   // Returns URL which echoes all request headers.
57   static std::string GetEchoAllHeadersURL();
58   // Returns URL which echoes data in a request body.
59   static std::string GetEchoRequestBodyURL();
60   // Returns URL which redirects to URL that echoes data in a request body.
61   static std::string GetRedirectToEchoBodyURL();
62   // Returns a URL that the server will return an Exabyte of data.
63   static std::string GetExabyteResponseURL();
64   // Prepares response and returns URL which respond with |data_size| of bytes
65   // in response body.
66   static std::string PrepareBigDataURL(size_t data_size);
67   // Releases response created by PrepareBigDataURL().
68   static void ReleaseBigDataURL();
69 
70   // The following URLs will make TestServer serve a response based on
71   // the contents of the corresponding file and its mock-http-headers file.
72 
73   // Returns URL which responds with content of file at |file_path|.
74   static std::string GetFileURL(const std::string& file_path);
75 
76   // Returns URL which responds with plain/text success.
GetSuccessURL()77   static std::string GetSuccessURL() { return GetFileURL("/success.txt"); }
78 
79   // Returns URL which redirects to plain/text success.
GetRedirectURL()80   static std::string GetRedirectURL() { return GetFileURL("/redirect.html"); }
81 
82   // Returns URL which redirects to redirect to plain/text success.
GetMultiRedirectURL()83   static std::string GetMultiRedirectURL() {
84     return GetFileURL("/multiredirect.html");
85   }
86 
87   // Returns URL which responds with status code 404 - page not found..
GetNotFoundURL()88   static std::string GetNotFoundURL() { return GetFileURL("/notfound.html"); }
89 };
90 
91 }  // namespace cronet
92 
93 #endif  // COMPONENTS_CRONET_TESTING_TEST_SERVER_TEST_SERVER_H_
94