Merge branch 'master' into release-2.0
This commit is contained in:
30
src/body.rs
30
src/body.rs
@@ -15,16 +15,16 @@ use super::SerdeValue;
|
|||||||
/// The different kinds of bodies to send.
|
/// The different kinds of bodies to send.
|
||||||
///
|
///
|
||||||
/// *Internal API*
|
/// *Internal API*
|
||||||
pub(crate) enum Payload {
|
pub(crate) enum Payload<'a> {
|
||||||
Empty,
|
Empty,
|
||||||
Text(String, String),
|
Text(&'a str, String),
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
JSON(SerdeValue),
|
JSON(SerdeValue),
|
||||||
Reader(Box<dyn Read + 'static>),
|
Reader(Box<dyn Read + 'a>),
|
||||||
Bytes(Vec<u8>),
|
Bytes(&'a [u8]),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Payload {
|
impl fmt::Debug for Payload<'_> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Payload::Empty => write!(f, "Empty"),
|
Payload::Empty => write!(f, "Empty"),
|
||||||
@@ -37,8 +37,8 @@ impl fmt::Debug for Payload {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Payload {
|
impl Default for Payload<'_> {
|
||||||
fn default() -> Payload {
|
fn default() -> Self {
|
||||||
Payload::Empty
|
Payload::Empty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,25 +56,25 @@ pub(crate) enum BodySize {
|
|||||||
/// Payloads are turned into this type where we can hold both a size and the reader.
|
/// Payloads are turned into this type where we can hold both a size and the reader.
|
||||||
///
|
///
|
||||||
/// *Internal API*
|
/// *Internal API*
|
||||||
pub(crate) struct SizedReader {
|
pub(crate) struct SizedReader<'a> {
|
||||||
pub size: BodySize,
|
pub size: BodySize,
|
||||||
pub reader: Box<dyn Read + 'static>,
|
pub reader: Box<dyn Read + 'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for SizedReader {
|
impl fmt::Debug for SizedReader<'_> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "SizedReader[size={:?},reader]", self.size)
|
write!(f, "SizedReader[size={:?},reader]", self.size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SizedReader {
|
impl<'a> SizedReader<'a> {
|
||||||
fn new(size: BodySize, reader: Box<dyn Read + 'static>) -> Self {
|
fn new(size: BodySize, reader: Box<dyn Read + 'a>) -> Self {
|
||||||
SizedReader { size, reader }
|
SizedReader { size, reader }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Payload {
|
impl<'a> Payload<'a> {
|
||||||
pub fn into_read(self) -> SizedReader {
|
pub fn into_read(self) -> SizedReader<'a> {
|
||||||
match self {
|
match self {
|
||||||
Payload::Empty => SizedReader::new(BodySize::Empty, Box::new(empty())),
|
Payload::Empty => SizedReader::new(BodySize::Empty, Box::new(empty())),
|
||||||
Payload::Text(text, _charset) => {
|
Payload::Text(text, _charset) => {
|
||||||
@@ -86,7 +86,7 @@ impl Payload {
|
|||||||
encoding.encode(&text, EncoderTrap::Replace).unwrap()
|
encoding.encode(&text, EncoderTrap::Replace).unwrap()
|
||||||
};
|
};
|
||||||
#[cfg(not(feature = "charset"))]
|
#[cfg(not(feature = "charset"))]
|
||||||
let bytes = text.into_bytes();
|
let bytes = text.as_bytes();
|
||||||
let len = bytes.len();
|
let len = bytes.len();
|
||||||
let cursor = Cursor::new(bytes);
|
let cursor = Cursor::new(bytes);
|
||||||
SizedReader::new(BodySize::Known(len as u64), Box::new(cursor))
|
SizedReader::new(BodySize::Known(len as u64), Box::new(cursor))
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ impl Request {
|
|||||||
/// println!("{:?}", r);
|
/// println!("{:?}", r);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn send_bytes(self, data: &[u8]) -> Result<Response> {
|
pub fn send_bytes(self, data: &[u8]) -> Result<Response> {
|
||||||
self.do_call(Payload::Bytes(data.to_owned()))
|
self.do_call(Payload::Bytes(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send data as a string.
|
/// Send data as a string.
|
||||||
@@ -162,10 +162,9 @@ impl Request {
|
|||||||
/// println!("{:?}", r);
|
/// println!("{:?}", r);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn send_string(self, data: &str) -> Result<Response> {
|
pub fn send_string(self, data: &str) -> Result<Response> {
|
||||||
let text = data.into();
|
|
||||||
let charset =
|
let charset =
|
||||||
crate::response::charset_from_content_type(self.header("content-type")).to_string();
|
crate::response::charset_from_content_type(self.header("content-type")).to_string();
|
||||||
self.do_call(Payload::Text(text, charset))
|
self.do_call(Payload::Text(data, charset))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a sequence of (key, value) pairs as form-urlencoded data.
|
/// Send a sequence of (key, value) pairs as form-urlencoded data.
|
||||||
@@ -191,7 +190,7 @@ impl Request {
|
|||||||
let encoded = form_urlencoded::Serializer::new(String::new())
|
let encoded = form_urlencoded::Serializer::new(String::new())
|
||||||
.extend_pairs(data)
|
.extend_pairs(data)
|
||||||
.finish();
|
.finish();
|
||||||
this.do_call(Payload::Bytes(encoded.into_bytes()))
|
this.do_call(Payload::Bytes(&encoded.into_bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send data from a reader.
|
/// Send data from a reader.
|
||||||
@@ -212,7 +211,7 @@ impl Request {
|
|||||||
/// .set("Content-Type", "text/plain")
|
/// .set("Content-Type", "text/plain")
|
||||||
/// .send(read);
|
/// .send(read);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn send(self, reader: impl Read + 'static) -> Result<Response> {
|
pub fn send(self, reader: impl Read) -> Result<Response> {
|
||||||
self.do_call(Payload::Reader(Box::new(reader)))
|
self.do_call(Payload::Reader(Box::new(reader)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,3 +476,12 @@ fn request_implements_send_and_sync() {
|
|||||||
"https://example.com/".to_string(),
|
"https://example.com/".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn send_byte_slice() {
|
||||||
|
let bytes = vec![1, 2, 3];
|
||||||
|
crate::agent()
|
||||||
|
.post("http://example.com")
|
||||||
|
.send(&bytes[1..2])
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user