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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use bytes::{BufMut, BytesMut};
use std::collections::HashMap;

use crate::app::App;

use crate::core::context::Context;
use crate::core::request::decode;
use crate::core::request::Request;
use crate::core::response::{Response, StatusMessage};

pub async fn request<T: Context<Response = Response> + Send, S: Send>(
    app: &App<Request, T, S>,
    method: &str,
    route: &str,
    headers: &[(&str, &str)],
    body: &str,
) -> TestResponse {
    let headers_mapped: Vec<String> = headers
        .iter()
        .map(|val| format!("{}: {}", val.0, val.1))
        .collect();
    let headers = headers_mapped.join("\n");
    let body = format!(
        "{} {} HTTP/1.1\nHost: localhost:8080\n{}\n\n{}",
        method, route, headers, body
    );

    let mut bytes = BytesMut::with_capacity(body.len());
    bytes.put(body.as_bytes());

    let request = decode(&mut bytes).unwrap().unwrap();
    let matched_route = app.resolve_from_method_and_path("GET", route);
    let response = app.resolve(request, matched_route).await.unwrap();

    TestResponse::new(response)
}

pub async fn get<T: Context<Response = Response> + Send, S: Send>(
    app: &App<Request, T, S>,
    route: &str,
) -> TestResponse {
    let body = format!("GET {} HTTP/1.1\nHost: localhost:8080\n\n", route);

    let mut bytes = BytesMut::with_capacity(body.len());
    bytes.put(body.as_bytes());

    let request = decode(&mut bytes).unwrap().unwrap();
    let matched_route = app.resolve_from_method_and_path("GET", route);
    let response = app.resolve(request, matched_route).await.unwrap();

    TestResponse::new(response)
}

pub async fn delete<T: Context<Response = Response> + Send, S: Send>(
    app: &App<Request, T, S>,
    route: &str,
) -> TestResponse {
    let body = format!("DELETE {} HTTP/1.1\nHost: localhost:8080\n\n", route);

    let mut bytes = BytesMut::with_capacity(body.len());
    bytes.put(body.as_bytes());

    let request = decode(&mut bytes).unwrap().unwrap();
    let matched_route = app.resolve_from_method_and_path("DELETE", route);
    let response = app.resolve(request, matched_route).await.unwrap();

    TestResponse::new(response)
}

pub async fn post<T: Context<Response = Response> + Send, S: Send>(
    app: &App<Request, T, S>,
    route: &str,
    content: &str,
) -> TestResponse {
    let body = format!(
        "POST {} HTTP/1.1\nHost: localhost:8080\nContent-Length: {}\n\n{}",
        route,
        content.len(),
        content
    );

    let mut bytes = BytesMut::with_capacity(body.len());
    bytes.put(body.as_bytes());

    let request = decode(&mut bytes).unwrap().unwrap();
    let matched_route = app.resolve_from_method_and_path("POST", route);
    let response = app.resolve(request, matched_route).await.unwrap();

    TestResponse::new(response)
}

pub async fn put<T: Context<Response = Response> + Send, S: Send>(
    app: &App<Request, T, S>,
    route: &str,
    content: &str,
) -> TestResponse {
    let body = format!(
        "PUT {} HTTP/1.1\nHost: localhost:8080\nContent-Length: {}\n\n{}",
        route,
        content.len(),
        content
    );

    let mut bytes = BytesMut::with_capacity(body.len());
    bytes.put(body.as_bytes());

    let request = decode(&mut bytes).unwrap().unwrap();
    let matched_route = app.resolve_from_method_and_path("PUT", route);
    let response = app.resolve(request, matched_route).await.unwrap();

    TestResponse::new(response)
}

pub async fn patch<T: Context<Response = Response> + Send, S: Send>(
    app: &App<Request, T, S>,
    route: &str,
    content: &str,
) -> TestResponse {
    let body = format!(
        "PATCH {} HTTP/1.1\nHost: localhost:8080\nContent-Length: {}\n\n{}",
        route,
        content.len(),
        content
    );

    let mut bytes = BytesMut::with_capacity(body.len());
    bytes.put(body.as_bytes());

    let request = decode(&mut bytes).unwrap().unwrap();
    let matched_route = app.resolve_from_method_and_path("PATCH", route);
    let response = app.resolve(request, matched_route).await.unwrap();

    TestResponse::new(response)
}

#[derive(Debug)]
pub struct TestResponse {
    pub body: String,
    pub headers: HashMap<String, String>,
    pub status: (String, u32),
}

impl TestResponse {
    fn new(response: Response) -> TestResponse {
        let mut headers = HashMap::new();
        let header_string = String::from_utf8(response.header_raw.to_vec()).unwrap();

        for header_pair in header_string.split("\r\n") {
            if !header_pair.is_empty() {
                let mut split = header_pair.split(':');
                let key = split.next().unwrap().trim().to_owned();
                let value = split.next().unwrap().trim().to_owned();

                headers.insert(key, value);
            }
        }

        TestResponse {
            body: String::from_utf8(response.response).unwrap(),
            headers,
            status: match response.status_message {
                StatusMessage::Ok => ("Ok".to_owned(), 200),
                StatusMessage::Custom(code, message) => (message, code),
            },
        }
    }
}