2023-07-13 23:36:42 +00:00
|
|
|
// Copyright 2023 Google LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#ifndef PACKAGER_MEDIA_TEST_TEST_WEB_SERVER_H_
|
|
|
|
#define PACKAGER_MEDIA_TEST_TEST_WEB_SERVER_H_
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/synchronization/mutex.h>
|
|
|
|
#include <absl/time/time.h>
|
2023-07-13 23:36:42 +00:00
|
|
|
|
|
|
|
// Forward declare mongoose struct types, used as pointers below.
|
|
|
|
struct mg_connection;
|
|
|
|
struct mg_http_message;
|
2023-07-18 23:19:52 +00:00
|
|
|
struct mg_mgr;
|
2023-07-13 23:36:42 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class TestWebServer {
|
|
|
|
public:
|
|
|
|
TestWebServer();
|
|
|
|
~TestWebServer();
|
|
|
|
|
2023-07-18 23:19:52 +00:00
|
|
|
bool Start();
|
|
|
|
|
|
|
|
// Reflects back the request characteristics as a JSON response.
|
|
|
|
std::string ReflectUrl() { return base_url_ + "/reflect"; }
|
|
|
|
|
|
|
|
// Responds with a specific HTTP status code.
|
|
|
|
std::string StatusCodeUrl(int code) {
|
|
|
|
return base_url_ + "/status?code=" + std::to_string(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Responds with HTTP 200 after a delay.
|
|
|
|
std::string DelayUrl(int seconds) {
|
|
|
|
return base_url_ + "/delay?seconds=" + std::to_string(seconds);
|
|
|
|
}
|
2023-07-13 23:36:42 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum TestWebServerStatus {
|
|
|
|
kNew,
|
|
|
|
kFailed,
|
|
|
|
kStarted,
|
|
|
|
kStopped,
|
|
|
|
};
|
|
|
|
|
|
|
|
absl::Mutex mutex_;
|
2023-07-14 19:40:12 +00:00
|
|
|
TestWebServerStatus status_ ABSL_GUARDED_BY(mutex_);
|
|
|
|
absl::CondVar started_ ABSL_GUARDED_BY(mutex_);
|
|
|
|
absl::CondVar stop_ ABSL_GUARDED_BY(mutex_);
|
|
|
|
bool stopped_ ABSL_GUARDED_BY(mutex_);
|
2023-07-13 23:36:42 +00:00
|
|
|
|
|
|
|
// Connections to be handled again later, mapped to the time at which we
|
|
|
|
// should handle them again. We can't block the server thread directly to
|
|
|
|
// simulate delays. Only ever accessed from |thread_|.
|
|
|
|
std::map<struct mg_connection*, absl::Time> delayed_connections_;
|
|
|
|
|
|
|
|
std::unique_ptr<std::thread> thread_;
|
|
|
|
|
2023-07-18 23:19:52 +00:00
|
|
|
std::string base_url_;
|
|
|
|
|
|
|
|
// Sets base_url_.
|
|
|
|
bool TryListenOnPort(struct mg_mgr* manager, int port);
|
|
|
|
|
|
|
|
void ThreadCallback();
|
2023-07-13 23:36:42 +00:00
|
|
|
|
|
|
|
static void HandleEvent(struct mg_connection* connection,
|
|
|
|
int event,
|
|
|
|
void* event_data,
|
|
|
|
void* callback_data);
|
|
|
|
|
|
|
|
bool HandleStatus(struct mg_http_message* message,
|
|
|
|
struct mg_connection* connection);
|
|
|
|
bool HandleDelay(struct mg_http_message* message,
|
|
|
|
struct mg_connection* connection);
|
|
|
|
bool HandleReflect(struct mg_http_message* message,
|
|
|
|
struct mg_connection* connection);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|
|
|
|
|
|
|
|
#endif // PACKAGER_MEDIA_TEST_TEST_WEB_SERVER_H_
|