// Copyright 2012 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_EMBEDDED_TEST_SERVER_H_ #define NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ #include <stdint.h> #include <map> #include <memory> #include <optional> #include <string> #include <string_view> #include <vector> #include "base/files/file_path.h" #include "base/functional/callback.h" #include "base/memory/raw_ptr.h" #include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "base/threading/thread.h" #include "base/threading/thread_checker.h" #include "base/types/expected.h" #include "net/base/address_list.h" #include "net/base/host_port_pair.h" #include "net/base/ip_endpoint.h" #include "net/cert/test_root_certs.h" #include "net/cert/x509_certificate.h" #include "net/socket/ssl_server_socket.h" #include "net/socket/stream_socket.h" #include "net/socket/tcp_server_socket.h" #include "net/ssl/ssl_server_config.h" #include "net/test/cert_builder.h" #include "net/test/embedded_test_server/http_connection.h" #include "third_party/boringssl/src/pki/ocsp_revocation_status.h" #include "third_party/boringssl/src/pki/parse_certificate.h" #include "url/gurl.h" #include "url/origin.h" namespace net { class StreamSocket; class TCPServerSocket; namespace test_server { class EmbeddedTestServerConnectionListener; class HttpConnection; class HttpResponse; class HttpResponseDelegate; struct HttpRequest; class EmbeddedTestServer; // Enum representing the possible outcomes of handling an upgrade request. // - kUpgraded: The request was successfully upgraded to a WebSocket connection. // - kNotHandled: The request was not handled as an upgrade and should be // processed as a normal HTTP request. enum class UpgradeResult { … }; // Returned by the Start[AcceptingConnections]WithHandle() APIs, to simplify // correct shutdown ordering of the EmbeddedTestServer. Shutdown() is invoked // on the associated test server when the handle goes out of scope. The handle // must therefore be destroyed before the test server. class EmbeddedTestServerHandle { … }; // Class providing an HTTP server for testing purpose. This is a basic server // providing only an essential subset of HTTP/1.1 protocol. Especially, // it assumes that the request syntax is correct. It *does not* support // a Chunked Transfer Encoding. // // The common use case for unit tests is below: // // void SetUp() { // test_server_ = std::make_unique<EmbeddedTestServer>(); // test_server_->RegisterRequestHandler( // base::BindRepeating(&FooTest::HandleRequest, base::Unretained(this))); // ASSERT_TRUE((test_server_handle_ = test_server_.StartAndReturnHandle())); // } // // std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { // GURL absolute_url = test_server_->GetURL(request.relative_url); // if (absolute_url.path() != "/test") // return nullptr; // // auto http_response = std::make_unique<BasicHttpResponse>(); // http_response->set_code(net::HTTP_OK); // http_response->set_content("hello"); // http_response->set_content_type("text/plain"); // return http_response; // } // // For a test that spawns another process such as browser_tests, it is // suggested to call Start in SetUpOnMainThread after the process is spawned. // If you have to do it before the process spawns, you need to first setup the // listen socket so that there is no no other threads running while spawning // the process. To do so, please follow the following example: // // void SetUp() { // ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); // ... // InProcessBrowserTest::SetUp(); // } // // void SetUpOnMainThread() { // // Starts the accept IO thread. // embedded_test_server()->StartAcceptingConnections(); // } // class EmbeddedTestServer { … }; } // namespace test_server // TODO(svaldez): Refactor EmbeddedTestServer to be in the net namespace. EmbeddedTestServer; } // namespace net #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_