#ifndef CHROME_UPDATER_TEST_SERVER_H_
#define CHROME_UPDATER_TEST_SERVER_H_
#include <list>
#include <memory>
#include <string>
#include <utility>
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "chrome/updater/test/request_matcher.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
class GURL;
namespace net {
namespace test_server {
struct HttpRequest;
class HttpResponse;
}
}
namespace updater::test {
class IntegrationTestCommands;
class ScopedServer {
public:
explicit ScopedServer(
scoped_refptr<IntegrationTestCommands> integration_test_commands);
ScopedServer();
~ScopedServer();
ScopedServer(const ScopedServer&) = delete;
ScopedServer& operator=(const ScopedServer&) = delete;
std::string host_port_pair() const {
return test_server_->host_port_pair().ToString();
}
void ConfigureTestMode(IntegrationTestCommands* commands);
void ExpectOnce(request::MatcherGroup request_matcher_group,
const std::string& response_body,
net::HttpStatusCode response_status_code = net::HTTP_OK);
void ExpectOnce(
request::MatcherGroup request_matcher_group,
base::RepeatingCallback<std::string(bool)> response_body_provider,
net::HttpStatusCode response_status_code = net::HTTP_OK);
GURL base_url() const { return test_server_->base_url(); }
std::string update_path() const { return "/update"; }
GURL update_url() const { return test_server_->GetURL(update_path()); }
std::string download_path() const { return "/download"; }
GURL download_url() const { return test_server_->GetURL(download_path()); }
void set_download_delay(base::TimeDelta delay) { download_delay_ = delay; }
std::string crash_report_path() const { return "/crash"; }
GURL crash_upload_url() const {
return test_server_->GetURL(crash_report_path());
}
std::string device_management_path() const { return "/dmapi"; }
GURL device_management_url() const {
return test_server_->GetURL(device_management_path());
}
std::string app_logo_path() const { return "/applogo/"; }
GURL app_logo_url() const { return test_server_->GetURL(app_logo_path()); }
std::string event_logging_path() const { return "/event_logging/"; }
GURL event_logging_url() const {
return test_server_->GetURL(event_logging_path());
}
std::string proxy_pac_path() const { return "/pac_script.pac"; }
GURL proxy_pac_url() const { return test_server_->GetURL(proxy_pac_path()); }
std::string proxy_url_no_path() const {
std::string proxy = test_server_->base_url().spec();
if (proxy.back() == '/') {
proxy.pop_back();
}
return proxy;
}
bool gzip_response() const { return gzip_response_; }
void set_gzip_response(bool gzip_response) { gzip_response_ = gzip_response; }
private:
std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
const net::test_server::HttpRequest& request);
std::unique_ptr<net::test_server::EmbeddedTestServer> test_server_ =
std::make_unique<net::test_server::EmbeddedTestServer>();
net::test_server::EmbeddedTestServerHandle test_server_handle_;
std::list<request::MatcherGroup> request_matcher_groups_;
std::list<std::pair<net::HttpStatusCode,
base::RepeatingCallback<std::string(bool)>>>
responses_;
base::TimeDelta download_delay_;
bool gzip_response_ = false;
};
}
#endif