1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
//! Facilities for testing hyperbole apps.
use super::App;
use hyper::{
    body::{to_bytes, Bytes},
    Body, Method, Request, Response,
};
use serde::{de::DeserializeOwned, Serialize};

const LOCAL: ([u8; 4], u16) = ([127, 0, 0, 1], 4321);

#[inline]
fn request(m: Method, path: &str) -> Request<Body> {
    assert!(path.starts_with('/'));

    Request::builder()
        .method(m)
        .uri(format!("http://testclient{}", path))
        .body(Body::empty())
        .unwrap()
}

/// A test client.
pub struct Client {
    pub(super) app: App,
}

impl Client {
    /// Prepare a call with the provided `method` and `path`.
    pub fn call<'a>(&'a self, method: Method, path: &str) -> Call<'a> {
        Call {
            app: &self.app,
            req: request(method, path),
        }
    }

    /// Prepare a GET call with the provided `path`.
    pub fn get<'a>(&'a self, path: &str) -> Call<'a> {
        self.call(Method::GET, path)
    }

    /// Prepare a POST call with the provided `path`.
    pub fn post<'a>(&'a self, path: &str) -> Call<'a> {
        self.call(Method::POST, path)
    }

    /// Prepare a PUT call with the provided `path`.
    pub fn put<'a>(&'a self, path: &str) -> Call<'a> {
        self.call(Method::PUT, path)
    }

    /// Prepare a PATCH call with the provided `path`.
    pub fn patch<'a>(&'a self, path: &str) -> Call<'a> {
        self.call(Method::PATCH, path)
    }

    /// Prepare a DELETE call with the provided `path`.
    pub fn delete<'a>(&'a self, path: &str) -> Call<'a> {
        self.call(Method::DELETE, path)
    }
}

/// A test call.
pub struct Call<'a> {
    app: &'a App,
    req: Request<Body>,
}

impl<'a> Call<'a> {
    /// Set a request body for this call.
    pub fn body<B: Into<Body>>(mut self, body: B) -> Self {
        *self.req.body_mut() = body.into();
        self
    }

    /// Set a json request body for this call.
    pub fn body_json<T: Serialize>(mut self, body: &T) -> Self {
        *self.req.body_mut() = serde_json::to_vec(body).unwrap().into();
        self
    }

    /// Dispatch the request defined by this call.
    pub async fn dispatch_body(self) -> Response<Body> {
        self.app.dispatch(self.req, LOCAL.into()).await
    }

    /// Dispatch the request defined by this call, retrieving the response body as a [Bytes].
    pub async fn dispatch_bytes(self) -> Response<Bytes> {
        let (parts, body) = self.dispatch_body().await.into_parts();
        let bytes = to_bytes(body).await.unwrap();

        Response::from_parts(parts, bytes)
    }

    /// Dispatch the request defined by this call, retrieving the response body as a [String].
    pub async fn dispatch(self) -> Response<String> {
        self.dispatch_bytes()
            .await
            .map(|b| String::from_utf8_lossy(&*b).into_owned())
    }

    /// Dispatch the request defined by this call, retrieving the response body as a decoded
    /// json object.
    pub async fn dispatch_json<T: DeserializeOwned>(self) -> Response<T> {
        self.dispatch_bytes()
            .await
            .map(|b| serde_json::from_slice(&*b).unwrap())
    }
}