Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Content-Type header to requests with body #627

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions kube-core/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Request builder type for arbitrary api types
use super::params::{DeleteParams, ListParams, Patch, PatchParams, PostParams};
use crate::{Error, Result};

pub(crate) const JSON_MIME: &str = "application/json";

/// A Kubernetes request builder
///
/// Takes a base_path and supplies constructors for common operations
Expand Down Expand Up @@ -101,7 +104,7 @@ impl Request {
qp.append_pair("dryRun", "All");
}
let urlstr = qp.finish();
let req = http::Request::post(urlstr);
let req = http::Request::post(urlstr).header(http::header::CONTENT_TYPE, JSON_MIME);
req.body(data).map_err(Error::HttpError)
}

Expand All @@ -111,7 +114,7 @@ impl Request {
let mut qp = form_urlencoded::Serializer::new(target);
let urlstr = qp.finish();
let body = serde_json::to_vec(&dp)?;
let req = http::Request::delete(urlstr);
let req = http::Request::delete(urlstr).header(http::header::CONTENT_TYPE, JSON_MIME);
req.body(body).map_err(Error::HttpError)
}

Expand All @@ -127,7 +130,7 @@ impl Request {
}
let urlstr = qp.finish();
let body = serde_json::to_vec(&dp)?;
let req = http::Request::delete(urlstr);
let req = http::Request::delete(urlstr).header(http::header::CONTENT_TYPE, JSON_MIME);
req.body(body).map_err(Error::HttpError)
}

Expand All @@ -147,8 +150,8 @@ impl Request {
let urlstr = qp.finish();

http::Request::patch(urlstr)
.header("Accept", "application/json")
.header("Content-Type", patch.content_type())
.header(http::header::ACCEPT, JSON_MIME)
.header(http::header::CONTENT_TYPE, patch.content_type())
.body(patch.serialize()?)
.map_err(Error::HttpError)
}
Expand All @@ -163,7 +166,7 @@ impl Request {
qp.append_pair("dryRun", "All");
}
let urlstr = qp.finish();
let req = http::Request::put(urlstr);
let req = http::Request::put(urlstr).header(http::header::CONTENT_TYPE, JSON_MIME);
req.body(data).map_err(Error::HttpError)
}
}
Expand Down Expand Up @@ -194,8 +197,8 @@ impl Request {
let urlstr = qp.finish();

http::Request::patch(urlstr)
.header("Accept", "application/json")
.header("Content-Type", patch.content_type())
.header(http::header::ACCEPT, JSON_MIME)
.header(http::header::CONTENT_TYPE, patch.content_type())
.body(patch.serialize()?)
.map_err(Error::HttpError)
}
Expand All @@ -214,7 +217,7 @@ impl Request {
qp.append_pair("dryRun", "All");
}
let urlstr = qp.finish();
let req = http::Request::put(urlstr);
let req = http::Request::put(urlstr).header(http::header::CONTENT_TYPE, JSON_MIME);
req.body(data).map_err(Error::HttpError)
}
}
Expand Down Expand Up @@ -242,6 +245,10 @@ mod test {
let url = corev1::Secret::url_path(&(), Some("ns"));
let req = Request::new(url).create(&PostParams::default(), vec![]).unwrap();
assert_eq!(req.uri(), "/api/v1/namespaces/ns/secrets?");
assert_eq!(
req.headers().get(http::header::CONTENT_TYPE).unwrap(),
super::JSON_MIME
);
}

#[test]
Expand Down Expand Up @@ -361,6 +368,10 @@ mod test {
};
let req = Request::new(url).replace("myds", &pp, vec![]).unwrap();
assert_eq!(req.uri(), "/apis/apps/v1/daemonsets/myds?&dryRun=All");
assert_eq!(
req.headers().get(http::header::CONTENT_TYPE).unwrap(),
super::JSON_MIME
);
}

#[test]
Expand All @@ -369,7 +380,11 @@ mod test {
let dp = DeleteParams::default();
let req = Request::new(url).delete("myrs", &dp).unwrap();
assert_eq!(req.uri(), "/apis/apps/v1/namespaces/ns/replicasets/myrs");
assert_eq!(req.method(), "DELETE")
assert_eq!(req.method(), "DELETE");
assert_eq!(
req.headers().get(http::header::CONTENT_TYPE).unwrap(),
super::JSON_MIME
);
}

#[test]
Expand All @@ -382,7 +397,11 @@ mod test {
req.uri(),
"/apis/apps/v1/namespaces/ns/replicasets?&labelSelector=app%3Dmyapp"
);
assert_eq!(req.method(), "DELETE")
assert_eq!(req.method(), "DELETE");
assert_eq!(
req.headers().get(http::header::CONTENT_TYPE).unwrap(),
super::JSON_MIME
);
}

#[test]
Expand Down Expand Up @@ -417,6 +436,10 @@ mod test {
.unwrap();
assert_eq!(req.uri(), "/api/v1/nodes/mynode/status?");
assert_eq!(req.method(), "PUT");
assert_eq!(
req.headers().get(http::header::CONTENT_TYPE).unwrap(),
super::JSON_MIME
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions kube-core/src/subresource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Debug;

use crate::{
params::{DeleteParams, PostParams},
request::Request,
request::{Request, JSON_MIME},
Error, Result,
};

Expand Down Expand Up @@ -112,7 +112,7 @@ impl Request {
"delete_options": ep.delete_options,
"metadata": { "name": name }
}))?;
let req = http::Request::post(urlstr);
let req = http::Request::post(urlstr).header(http::header::CONTENT_TYPE, JSON_MIME);
req.body(data).map_err(Error::HttpError)
}
}
Expand Down