-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
common.rs
72 lines (57 loc) 路 2.01 KB
/
common.rs
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
// This is imported by different tests that use different functions.
#![allow(dead_code)]
use axum::body::{Body, BoxBody, HttpBody};
use axum::http::header::CONTENT_TYPE;
use axum::http::{request, Request};
use axum::response::Response;
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
use uuid::Uuid;
pub trait RequestBuilderExt {
fn json(self, json: serde_json::Value) -> Request<Body>;
fn empty_body(self) -> Request<Body>;
}
impl RequestBuilderExt for request::Builder {
fn json(self, json: serde_json::Value) -> Request<Body> {
self.header("Content-Type", "application/json")
.body(Body::from(json.to_string()))
.expect("failed to build request")
}
fn empty_body(self) -> Request<Body> {
self.body(Body::empty()).expect("failed to build request")
}
}
#[track_caller]
pub async fn response_json(resp: &mut Response<BoxBody>) -> serde_json::Value {
assert_eq!(
resp.headers()
.get(CONTENT_TYPE)
.expect("expected Content-Type"),
"application/json"
);
let body = resp.body_mut();
let mut bytes = Vec::new();
while let Some(res) = body.data().await {
let chunk = res.expect("error reading response body");
bytes.extend_from_slice(&chunk[..]);
}
serde_json::from_slice(&bytes).expect("failed to read response body as json")
}
#[track_caller]
pub fn expect_string(value: &serde_json::Value) -> &str {
value
.as_str()
.unwrap_or_else(|| panic!("expected string, got {value:?}"))
}
#[track_caller]
pub fn expect_uuid(value: &serde_json::Value) -> Uuid {
expect_string(value)
.parse::<Uuid>()
.unwrap_or_else(|e| panic!("failed to parse UUID from {value:?}: {e}"))
}
#[track_caller]
pub fn expect_rfc3339_timestamp(value: &serde_json::Value) -> OffsetDateTime {
let s = expect_string(value);
OffsetDateTime::parse(s, &Rfc3339)
.unwrap_or_else(|e| panic!("failed to parse RFC-3339 timestamp from {value:?}: {e}"))
}