Set the default Content-Type for requests to application/json
This commit is contained in:
parent
adeb545062
commit
b087cf75b7
@ -120,7 +120,7 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
||||
let mut header_kvs = api.request.append_header_kvs();
|
||||
if authentication == "AccessToken" {
|
||||
header_kvs.extend(quote! {
|
||||
req_builder = req_builder.header(
|
||||
req_headers.insert(
|
||||
#http::header::AUTHORIZATION,
|
||||
#http::header::HeaderValue::from_str(
|
||||
&::std::format!(
|
||||
@ -332,7 +332,12 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
||||
},
|
||||
#request_path_string,
|
||||
#request_query_string,
|
||||
));
|
||||
))
|
||||
.header(#ruma_api::exports::http::header::CONTENT_TYPE, "application/json");
|
||||
|
||||
let mut req_headers = req_builder
|
||||
.headers_mut()
|
||||
.expect("`http::RequestBuilder` is in unusable state");
|
||||
|
||||
#header_kvs
|
||||
|
||||
|
@ -64,7 +64,7 @@ impl Request {
|
||||
{
|
||||
quote! {
|
||||
if let Some(header_val) = self.#field_name.as_ref() {
|
||||
req_builder = req_builder.header(
|
||||
req_headers.insert(
|
||||
#http::header::#header_name,
|
||||
#http::header::HeaderValue::from_str(header_val)?,
|
||||
);
|
||||
@ -72,7 +72,7 @@ impl Request {
|
||||
}
|
||||
}
|
||||
_ => quote! {
|
||||
req_builder = req_builder.header(
|
||||
req_headers.insert(
|
||||
#http::header::#header_name,
|
||||
#http::header::HeaderValue::from_str(self.#field_name.as_ref())?,
|
||||
);
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use http::header::{Entry, CONTENT_TYPE};
|
||||
use ruma_api::ruma_api;
|
||||
use ruma_api::{ruma_api, OutgoingRequest as _};
|
||||
|
||||
ruma_api! {
|
||||
metadata: {
|
||||
@ -16,6 +16,9 @@ ruma_api! {
|
||||
request: {
|
||||
#[ruma_api(header = LOCATION)]
|
||||
pub location: Option<String>,
|
||||
|
||||
#[ruma_api(header = CONTENT_TYPE)]
|
||||
pub stuff: String,
|
||||
}
|
||||
|
||||
response: {
|
||||
@ -25,7 +28,7 @@ ruma_api! {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn content_type_override() {
|
||||
fn response_content_type_override() {
|
||||
let res = Response { stuff: "magic".into() };
|
||||
let mut http_res = http::Response::<Vec<u8>>::try_from(res).unwrap();
|
||||
|
||||
@ -40,3 +43,18 @@ fn content_type_override() {
|
||||
);
|
||||
assert_eq!(http_res.headers().get("content-type").unwrap(), "magic");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_content_type_override() {
|
||||
let req = Request { location: None, stuff: "magic".into() };
|
||||
let mut http_req = req.try_into_http_request("https://homeserver.tld", None).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
match http_req.headers_mut().entry(CONTENT_TYPE) {
|
||||
Entry::Occupied(occ) => occ.iter().count(),
|
||||
_ => 0,
|
||||
},
|
||||
1
|
||||
);
|
||||
assert_eq!(http_req.headers().get("content-type").unwrap(), "magic");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user