#![allow(dead_code)]
use base64::{prelude::BASE64_STANDARD, Engine};
use rocket::http::{hyper::header, ContentType, Header};
use std::{fs, path::PathBuf};
use xapi_rs::{TEST_USER_PLAIN_TOKEN, V200, VERSION_HDR};
pub(crate) const BOUNDARY: &str = "MP_/xq.2QWbNf.dRrz_w=FAz9Dd";
pub(crate) const CR_LF: &[u8] = b"\r\n";
pub(crate) fn read_to_string(fixture: &str, json: bool) -> String {
let path = if json {
path_to(&format!("{}.json", fixture))
} else {
path_to(fixture)
};
fs::read_to_string(&path).expect(&format!("Failed reading string from '{}'", fixture))
}
pub(crate) fn read_to_bytes(fixture: &str) -> Vec<u8> {
let path = path_to(fixture);
fs::read(&path).expect(&format!("Failed reading bytes from '{}'", fixture))
}
pub(crate) fn to_b64_der(pem: &str) -> String {
let path = path_to(pem);
let lines: Vec<_> = fs::read_to_string(&path)
.expect(&format!("Failed reading PEM data from '{}'", pem))
.lines()
.filter(|x| !x.starts_with("-----"))
.map(String::from)
.collect();
lines.join("")
}
fn path_to(fixture: &str) -> PathBuf {
let mut result = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
result.push(format!("tests/samples/{}", fixture));
result
}
pub(crate) struct MyTestContext {
pub client: rocket::local::blocking::Client,
}
impl test_context::TestContext for MyTestContext {
fn setup() -> MyTestContext {
let __rocket = xapi_rs::build(true);
let client = rocket::local::blocking::Client::tracked(__rocket)
.expect("Failed creating Local Rocket client");
MyTestContext { client }
}
fn teardown(self) {
self.client.terminate();
}
}
pub(crate) fn accept_json() -> Header<'static> {
Header::new(header::ACCEPT.as_str(), "application/json")
}
pub(crate) fn v2() -> Header<'static> {
Header::new(VERSION_HDR, V200.to_string())
}
pub(crate) fn if_none_match(etag: &str) -> Header<'static> {
Header::new(header::IF_NONE_MATCH.as_str(), etag.to_string())
}
pub(crate) fn if_match(etag: &str) -> Header<'static> {
Header::new(header::IF_MATCH.as_str(), etag.to_string())
}
pub(crate) fn content_type(mime: &ContentType) -> Header<'static> {
Header::new(header::CONTENT_TYPE.as_str(), mime.to_string())
}
pub(crate) fn authorization() -> Header<'static> {
let b64_encoded = BASE64_STANDARD.encode(TEST_USER_PLAIN_TOKEN);
Header::new(
header::AUTHORIZATION.as_str(),
format!("Basic {}", b64_encoded),
)
}
pub(crate) fn act_as(email: &str, password: &str) -> Header<'static> {
let name_password = format!("{}:{}", email, password);
let b64_encoded = BASE64_STANDARD.encode(name_password);
Header::new(
header::AUTHORIZATION.as_str(),
format!("Basic {}", b64_encoded),
)
}
pub(crate) fn boundary_delimiter_line(boundary: &str) -> (ContentType, Vec<u8>) {
(
ContentType::new("multipart", "mixed")
.with_params(("boundary", format!("\"{}\"", BOUNDARY))),
[b"--", boundary.as_bytes(), b"\r\n"].concat(),
)
}
pub(crate) fn multipart(
delimiter: &[u8],
statement: &str,
att1: Option<Vec<u8>>,
att2: Option<Vec<u8>>,
) -> Vec<u8> {
let mut result = vec![];
result.extend_from_slice(delimiter);
result.extend_from_slice(b"Content-Type: application/json\r\n");
result.extend_from_slice(CR_LF);
result.extend_from_slice(statement.as_bytes());
result.extend_from_slice(CR_LF);
if att1.is_some() {
result.extend_from_slice(delimiter);
result.extend_from_slice(att1.unwrap().as_slice());
result.extend_from_slice(CR_LF);
if att2.is_some() {
result.extend_from_slice(delimiter);
result.extend_from_slice(att2.unwrap().as_slice());
result.extend_from_slice(CR_LF);
}
}
result.extend_from_slice(delimiter);
result
}
#[macro_export]
macro_rules! skip_if_legacy {
() => {
if xapi_rs::config().is_legacy() {
tracing::info!("*** Skip test b/c we're in Legacy mode.");
return Ok(());
}
};
}