ruma-api-macros: Avoid empty POST/PUT request bodys
This commit is contained in:
parent
57e84f862b
commit
6b78988d39
@ -176,8 +176,10 @@ impl Request {
|
|||||||
quote! {
|
quote! {
|
||||||
#ruma_serde::json_to_buf(&RequestBody { #initializers })?
|
#ruma_serde::json_to_buf(&RequestBody { #initializers })?
|
||||||
}
|
}
|
||||||
} else {
|
} else if method == "GET" {
|
||||||
quote! { <T as ::std::default::Default>::default() }
|
quote! { <T as ::std::default::Default>::default() }
|
||||||
|
} else {
|
||||||
|
quote! { #ruma_serde::slice_to_buf(b"{}") }
|
||||||
};
|
};
|
||||||
|
|
||||||
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use ruma_api::{ruma_api, OutgoingRequest as _, OutgoingResponse as _, SendAccessToken};
|
use ruma_api::{OutgoingRequest as _, OutgoingResponse as _, SendAccessToken};
|
||||||
|
|
||||||
ruma_api! {
|
mod get {
|
||||||
|
ruma_api::ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
description: "Does something.",
|
description: "Does something.",
|
||||||
method: GET,
|
method: GET,
|
||||||
@ -13,21 +14,59 @@ ruma_api! {
|
|||||||
request: {}
|
request: {}
|
||||||
response: {}
|
response: {}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod post {
|
||||||
|
ruma_api::ruma_api! {
|
||||||
|
metadata: {
|
||||||
|
description: "Does something.",
|
||||||
|
method: POST,
|
||||||
|
name: "no_fields",
|
||||||
|
path: "/_matrix/my/endpoint",
|
||||||
|
rate_limited: false,
|
||||||
|
authentication: None,
|
||||||
|
}
|
||||||
|
|
||||||
|
request: {}
|
||||||
|
response: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_request_http_repr() {
|
fn empty_post_request_http_repr() {
|
||||||
let req = Request {};
|
let req = post::Request {};
|
||||||
let http_req = req
|
let http_req = req
|
||||||
.try_into_http_request::<Vec<u8>>("https://homeserver.tld", SendAccessToken::None)
|
.try_into_http_request::<Vec<u8>>("https://homeserver.tld", SendAccessToken::None)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// Empty POST requests should contain an empty dictionary as a body...
|
||||||
|
assert_eq!(http_req.body(), b"{}");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn empty_get_request_http_repr() {
|
||||||
|
let req = get::Request {};
|
||||||
|
let http_req = req
|
||||||
|
.try_into_http_request::<Vec<u8>>("https://homeserver.tld", SendAccessToken::None)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// ... but GET requests' bodies should be empty.
|
||||||
assert!(http_req.body().is_empty());
|
assert!(http_req.body().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_response_http_repr() {
|
fn empty_post_response_http_repr() {
|
||||||
let res = Response {};
|
let res = post::Response {};
|
||||||
let http_res = res.try_into_http_response::<Vec<u8>>().unwrap();
|
let http_res = res.try_into_http_response::<Vec<u8>>().unwrap();
|
||||||
|
|
||||||
|
// For the reponse, the body should be an empty dict again...
|
||||||
|
assert_eq!(http_res.body(), b"{}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_get_response_http_repr() {
|
||||||
|
let res = get::Response {};
|
||||||
|
let http_res = res.try_into_http_response::<Vec<u8>>().unwrap();
|
||||||
|
|
||||||
|
// ... even for GET requests.
|
||||||
assert_eq!(http_res.body(), b"{}");
|
assert_eq!(http_res.body(), b"{}");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user