volga 0.9.1

Easy & Fast Web Framework for Rust
Documentation
//! Utilities for unit tests

use crate::HttpResponse;
use http_body_util::BodyExt;
use std::path::Path;
use tokio::io::AsyncReadExt;

/// Read response body bytes
pub(crate) async fn read_file_bytes(response: &mut HttpResponse) -> Vec<u8> {
    let mut buffer = vec![];
    while let Some(next) = response.body_mut().frame().await {
        let frame = next.unwrap();
        if let Some(chunk) = frame.data_ref() {
            buffer.extend_from_slice(chunk);
        }
    }

    // If the file starts with a UTF-8 BOM (EF BB BF), remove it
    if buffer.starts_with(&[0xEF, 0xBB, 0xBF]) {
        buffer.drain(0..3); // This removes the first three bytes
    }

    buffer
}

/// Reads a file
pub(crate) async fn read_file(path: impl AsRef<Path>) -> Vec<u8> {
    let mut file = tokio::fs::File::open(path).await.unwrap();
    let mut bytes = vec![];

    file.read_to_end(&mut bytes).await.unwrap();

    // If the file starts with a UTF-8 BOM (EF BB BF), remove it
    if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
        bytes.drain(0..3); // This removes the first three bytes
    }

    bytes
}