use wiremock::matchers::{header_exists, method, path, path_regex};
use wiremock::{Mock, MockServer, ResponseTemplate};
use super::fixtures;
pub async fn setup_hls_server() -> MockServer {
let server = MockServer::start().await;
let master_content = fixtures::load_hls_fixture("master.m3u8", &server.uri());
Mock::given(method("GET"))
.and(path("/hls/master.m3u8"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(master_content)
.insert_header("Content-Type", "application/vnd.apple.mpegurl"),
)
.mount(&server)
.await;
let media_content = fixtures::load_hls_fixture("media.m3u8", &server.uri());
Mock::given(method("GET"))
.and(path_regex(r"^/hls/.*\.m3u8$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(media_content)
.insert_header("Content-Type", "application/vnd.apple.mpegurl"),
)
.mount(&server)
.await;
let segment_bytes = fixtures::load_media_bytes("small.ts");
Mock::given(method("GET"))
.and(path_regex(r"^/hls/segment_\d+\.ts$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_bytes(segment_bytes)
.insert_header("Content-Type", "video/mp2t"),
)
.mount(&server)
.await;
server
}
pub async fn setup_media_server() -> MockServer {
let server = MockServer::start().await;
let media_bytes = fixtures::load_media_bytes("small.bin");
Mock::given(method("GET"))
.and(path_regex(r"^/media/.*"))
.respond_with(
ResponseTemplate::new(200)
.set_body_bytes(media_bytes.clone())
.insert_header("Content-Type", "application/octet-stream")
.insert_header("Accept-Ranges", "bytes"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/media/.*"))
.and(header_exists("Range"))
.respond_with(
ResponseTemplate::new(206)
.set_body_bytes(media_bytes[..1024.min(media_bytes.len())].to_vec())
.insert_header("Content-Type", "application/octet-stream")
.insert_header(
"Content-Range",
format!("bytes 0-{}/{}", 1023.min(media_bytes.len() - 1), media_bytes.len()),
)
.insert_header("Accept-Ranges", "bytes"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/thumbnails/.*"))
.respond_with(
ResponseTemplate::new(200)
.set_body_bytes(vec![0xFF, 0xD8, 0xFF, 0xE0]) .insert_header("Content-Type", "image/jpeg"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/storyboard/.*"))
.respond_with(
ResponseTemplate::new(200)
.set_body_bytes(vec![0xFF, 0xD8, 0xFF, 0xE0])
.insert_header("Content-Type", "image/jpeg"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/hls/master.m3u8"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(fixtures::load_hls_fixture("master.m3u8", &server.uri()))
.insert_header("Content-Type", "application/vnd.apple.mpegurl"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/hls/.*\.m3u8$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(fixtures::load_hls_fixture("media.m3u8", &server.uri()))
.insert_header("Content-Type", "application/vnd.apple.mpegurl"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/hls/segment_\d+\.ts$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_bytes(media_bytes)
.insert_header("Content-Type", "video/mp2t"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/(captions|subtitles)/.*\.srt$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(fixtures::load_subtitle_string("sample.srt"))
.insert_header("Content-Type", "text/plain; charset=utf-8"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/(captions|subtitles)/.*\.vtt$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(fixtures::load_subtitle_string("sample.vtt"))
.insert_header("Content-Type", "text/vtt; charset=utf-8"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/(captions|subtitles)/.*\.json[3]?$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string("{\"events\":[]}")
.insert_header("Content-Type", "application/json"),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path_regex(r"^/(captions|subtitles)/.*\.(srv[123]|ttml|ass|ssa)$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string("")
.insert_header("Content-Type", "text/plain; charset=utf-8"),
)
.mount(&server)
.await;
server
}