mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
parent
2f68a2a641
commit
f47e830cfb
16 changed files with 183 additions and 25 deletions
2
.github/workflows/linux.yml
vendored
2
.github/workflows/linux.yml
vendored
|
@ -54,7 +54,7 @@ jobs:
|
|||
continue-on-error: true
|
||||
run: |
|
||||
cd ntex
|
||||
cargo test --no-default-features --no-fail-fast --features="async-std,cookie,url,compress,openssl,rustls" --lib -- --test-threads 1
|
||||
cargo test --no-default-features --no-fail-fast --features="async-std,cookie,url,compress,openssl,rustls"
|
||||
|
||||
- name: Install cargo-cache
|
||||
continue-on-error: true
|
||||
|
|
|
@ -990,6 +990,25 @@ mod tests {
|
|||
b.put_slice(b"123");
|
||||
assert_eq!(&buf[..], b"1231111111");
|
||||
|
||||
let mut b: &mut [u8] = buf.as_mut();
|
||||
let chunk = b.chunk_mut();
|
||||
chunk.write_byte(0, b'9');
|
||||
assert_eq!(&buf[..], b"9231111111");
|
||||
|
||||
let mut b = buf.as_mut();
|
||||
let chunk = b.chunk_mut();
|
||||
chunk.copy_from_slice(b"0000000000");
|
||||
assert_eq!(&buf[..], b"0000000000");
|
||||
|
||||
let mut b = buf.as_mut();
|
||||
assert_eq!(format!("{:?}", b.chunk_mut()), "UninitSlice[...]");
|
||||
|
||||
let b = buf.as_mut();
|
||||
let mut bb = Box::new(b);
|
||||
let chunk = bb.chunk_mut();
|
||||
chunk.copy_from_slice(b"1111111111");
|
||||
assert_eq!(&buf[..], b"1111111111");
|
||||
|
||||
let mut buf = BytesMut::new();
|
||||
buf.put_u8(0x01);
|
||||
assert_eq!(buf, b"\x01"[..]);
|
||||
|
|
|
@ -4078,12 +4078,39 @@ mod tests {
|
|||
assert!(b.is_empty());
|
||||
assert!(b.len() == 0);
|
||||
|
||||
let b = Bytes::from(&Bytes::from(LONG));
|
||||
assert_eq!(b, LONG);
|
||||
|
||||
let b = Bytes::from(BytesMut::from(LONG));
|
||||
assert_eq!(b, LONG);
|
||||
|
||||
let b = BytesMut::try_from(b).unwrap();
|
||||
let mut b: Bytes = BytesMut::try_from(b).unwrap().freeze();
|
||||
assert_eq!(b, LONG);
|
||||
assert_eq!(Buf::remaining(&b), LONG.len());
|
||||
assert_eq!(Buf::chunk(&b), LONG);
|
||||
Buf::advance(&mut b, 10);
|
||||
assert_eq!(Buf::chunk(&b), &LONG[10..]);
|
||||
|
||||
let mut b = BytesMut::try_from(LONG).unwrap();
|
||||
assert_eq!(b, LONG);
|
||||
assert_eq!(Buf::remaining(&b), LONG.len());
|
||||
assert_eq!(BufMut::remaining_mut(&b), 25);
|
||||
assert_eq!(Buf::chunk(&b), LONG);
|
||||
Buf::advance(&mut b, 10);
|
||||
assert_eq!(Buf::chunk(&b), &LONG[10..]);
|
||||
|
||||
let mut b = BytesMut::with_capacity(12);
|
||||
BufMut::put_i8(&mut b, 1);
|
||||
assert_eq!(b, b"\x01".as_ref());
|
||||
BufMut::put_u8(&mut b, 2);
|
||||
assert_eq!(b, b"\x01\x02".as_ref());
|
||||
BufMut::put_slice(&mut b, b"12345");
|
||||
assert_eq!(b, b"\x01\x0212345".as_ref());
|
||||
BufMut::chunk_mut(&mut b).write_byte(0, b'1');
|
||||
unsafe { BufMut::advance_mut(&mut b, 1) };
|
||||
assert_eq!(b, b"\x01\x02123451".as_ref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_vec() {
|
||||
let bv = BytesVec::copy_from_slice(&LONG[..]);
|
||||
|
|
|
@ -86,3 +86,24 @@ macro_rules! serde_impl {
|
|||
|
||||
serde_impl!(Bytes, BytesVisitor, copy_from_slice);
|
||||
serde_impl!(BytesMut, BytesMutVisitor, from);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_serialize() {
|
||||
let s: Bytes = serde_json::from_str(r#""nice bytes""#).unwrap();
|
||||
assert_eq!(s, "nice bytes");
|
||||
let s: BytesMut = serde_json::from_str(r#""nice bytes""#).unwrap();
|
||||
assert_eq!(s, "nice bytes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize() {
|
||||
let s = serde_json::to_string(&Bytes::from_static(b"nice bytes")).unwrap();
|
||||
assert_eq!(s, "[110,105,99,101,32,98,121,116,101,115]");
|
||||
let s = serde_json::to_string(&BytesMut::copy_from_slice(b"nice bytes")).unwrap();
|
||||
assert_eq!(s, "[110,105,99,101,32,98,121,116,101,115]");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -371,7 +371,7 @@ mod utf8 {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::borrow::ToOwned;
|
||||
use std::borrow::{Borrow, Cow, ToOwned};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
|
@ -384,12 +384,34 @@ mod test {
|
|||
assert_eq!(s, "test");
|
||||
assert_eq!(s, *"test");
|
||||
assert_eq!(s, "test".to_owned());
|
||||
assert_eq!(s.as_str(), "test");
|
||||
assert_eq!(s.as_slice(), b"test");
|
||||
assert_eq!(s.as_bytes(), &Bytes::copy_from_slice(b"test"));
|
||||
assert_eq!(Borrow::<str>::borrow(&s), "test");
|
||||
|
||||
assert_eq!(format!("{}", s), "test");
|
||||
assert_eq!(format!("{:?}", s), "\"test\"");
|
||||
assert_eq!(s.into_bytes(), Bytes::copy_from_slice(b"test"));
|
||||
|
||||
let b = s.into_bytes();
|
||||
assert_eq!(b, Bytes::copy_from_slice(b"test"));
|
||||
|
||||
let s = unsafe { ByteString::from_bytes_unchecked(b) };
|
||||
assert_eq!(s, "test");
|
||||
assert_eq!(s.slice(0..2), "te");
|
||||
|
||||
let s = ByteString::from(Cow::Borrowed("test"));
|
||||
assert_eq!(s, "test");
|
||||
let mut s = ByteString::from(Cow::Owned("test".to_string()));
|
||||
assert_eq!(s, "test");
|
||||
|
||||
s.clear();
|
||||
assert_eq!(s, "");
|
||||
|
||||
assert!(ByteString::try_from(b"\xc3\x28".as_ref()).is_err());
|
||||
assert!(ByteString::try_from(vec![b'\xc3']).is_err());
|
||||
assert!(ByteString::try_from(Bytes::from_static(b"\xc3\x28")).is_err());
|
||||
assert!(ByteString::try_from(&Bytes::from_static(b"\xc3\x28")).is_err());
|
||||
assert!(ByteString::try_from(BytesMut::copy_from_slice(b"\xc3\x28")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -145,6 +145,7 @@ impl From<std::convert::Infallible> for Error {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::error::Error as StdError;
|
||||
|
||||
#[test]
|
||||
fn inner_error_is_invalid_status_code() {
|
||||
|
@ -155,8 +156,12 @@ mod tests {
|
|||
assert!(ie.is::<status::InvalidStatusCode>());
|
||||
ie.downcast_ref::<status::InvalidStatusCode>().unwrap();
|
||||
|
||||
assert!(err.source().is_none());
|
||||
assert!(!err.is::<InvalidHeaderValue>());
|
||||
assert!(err.is::<status::InvalidStatusCode>());
|
||||
|
||||
let s = format!("{:?}", err);
|
||||
assert!(s.starts_with("ntex_http::Error"));
|
||||
} else {
|
||||
panic!("Bad status allowed!");
|
||||
}
|
||||
|
|
|
@ -758,6 +758,9 @@ mod tests {
|
|||
let hdr2 = HeaderValue::from(&hdr);
|
||||
assert_eq!(hdr, hdr2);
|
||||
|
||||
let hdr3 = HeaderValue::from_maybe_shared(Bytes::from_static(b"upgrade")).unwrap();
|
||||
assert_eq!(hdr, hdr3);
|
||||
|
||||
let hdr = http::header::HeaderValue::from_bytes(b"upgrade").unwrap();
|
||||
let hdr2 = HeaderValue::from(&hdr);
|
||||
assert_eq!(hdr2.as_bytes(), b"upgrade");
|
||||
|
@ -767,10 +770,29 @@ mod tests {
|
|||
|
||||
let hdr = HeaderValue::try_from("upgrade".to_string()).unwrap();
|
||||
assert_eq!(hdr.as_bytes(), b"upgrade");
|
||||
let hdr = HeaderValue::try_from(&("upgrade".to_string())).unwrap();
|
||||
assert_eq!(hdr.as_bytes(), b"upgrade");
|
||||
let hdr = HeaderValue::try_from(ByteString::from("upgrade")).unwrap();
|
||||
assert_eq!(hdr.as_bytes(), b"upgrade");
|
||||
let hdr = HeaderValue::try_from(&ByteString::from("upgrade")).unwrap();
|
||||
assert_eq!(hdr.as_bytes(), b"upgrade");
|
||||
let hdr2 = HeaderValue::try_from(&ByteString::from("upgrade2")).unwrap();
|
||||
|
||||
assert!(hdr == hdr);
|
||||
assert!(hdr == &hdr);
|
||||
assert!(hdr == "upgrade");
|
||||
assert!(hdr == "upgrade".to_string());
|
||||
assert!("upgrade" == hdr);
|
||||
assert!("upgrade" == &hdr);
|
||||
assert!("upgrade".to_string() == hdr);
|
||||
assert!(hdr < hdr2);
|
||||
assert!(hdr < &hdr2);
|
||||
assert!(&hdr < &hdr2);
|
||||
assert!(&hdr < "upgrade2");
|
||||
assert!(hdr < "upgrade2");
|
||||
assert!(hdr < "upgrade2".to_string());
|
||||
assert!("upgrade2" > hdr);
|
||||
assert!("upgrade2".to_string() > hdr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -251,7 +251,23 @@ mod tests {
|
|||
)
|
||||
.clone();
|
||||
|
||||
let srv = factory.create(&()).await.unwrap();
|
||||
let srv = factory.create(&()).await.unwrap().clone();
|
||||
let res = srv.call(10).await;
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 20);
|
||||
|
||||
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(Ok(())));
|
||||
|
||||
let res = lazy(|cx| srv.poll_shutdown(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(()));
|
||||
|
||||
let factory =
|
||||
crate::pipeline_factory(fn_service(|i: usize| Ready::<_, ()>::Ok(i * 2)))
|
||||
.apply(Rc::new(Tr(marker::PhantomData).clone()))
|
||||
.clone();
|
||||
|
||||
let srv = factory.create(&()).await.unwrap().clone();
|
||||
let res = srv.call(10).await;
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 20);
|
||||
|
|
|
@ -135,11 +135,11 @@ mod tests {
|
|||
|
||||
#[ntex_macros::rt_test2]
|
||||
async fn test_condition_poll() {
|
||||
let cond = Condition::new();
|
||||
let cond = Condition::default().clone();
|
||||
let waiter = cond.wait();
|
||||
assert_eq!(lazy(|cx| waiter.poll_ready(cx)).await, Poll::Pending);
|
||||
cond.notify();
|
||||
assert_eq!(lazy(|cx| waiter.poll_ready(cx)).await, Poll::Ready(()));
|
||||
waiter.ready().await;
|
||||
|
||||
let waiter2 = waiter.clone();
|
||||
assert_eq!(lazy(|cx| waiter.poll_ready(cx)).await, Poll::Pending);
|
||||
|
|
|
@ -116,9 +116,16 @@ mod tests {
|
|||
#[ntex_macros::rt_test2]
|
||||
async fn test_oneshot() {
|
||||
let (tx, rx) = channel();
|
||||
assert!(format!("{:?}", tx).contains("Sender"));
|
||||
assert!(format!("{:?}", rx).contains("Receiver"));
|
||||
|
||||
tx.send("test").unwrap();
|
||||
assert_eq!(rx.await.unwrap(), "test");
|
||||
|
||||
let (tx, rx) = channel();
|
||||
tx.send("test").unwrap();
|
||||
assert_eq!(rx.recv().await.unwrap(), "test");
|
||||
|
||||
let (tx, rx) = channel();
|
||||
assert!(!tx.is_canceled());
|
||||
drop(rx);
|
||||
|
|
|
@ -209,7 +209,13 @@ mod tests {
|
|||
#[ntex_macros::rt_test2]
|
||||
async fn test_pool() {
|
||||
let p = new();
|
||||
p.shrink_to_fit();
|
||||
assert!(format!("{:?}", p).contains("Pool"));
|
||||
|
||||
let (tx, rx) = p.channel();
|
||||
assert!(format!("{:?}", tx).contains("Sender"));
|
||||
assert!(format!("{:?}", rx).contains("Receiver"));
|
||||
|
||||
tx.send("test").unwrap();
|
||||
assert_eq!(rx.await.unwrap(), "test");
|
||||
assert!(format!("{}", Canceled).contains("canceled"));
|
||||
|
|
|
@ -49,15 +49,6 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.1.4", note = "Use stream_recv() fn instead")]
|
||||
pub async fn next<S>(stream: &mut S) -> Option<S::Item>
|
||||
where
|
||||
S: Stream + Unpin,
|
||||
{
|
||||
stream_recv(stream).await
|
||||
}
|
||||
|
||||
/// Creates a future that resolves to the next item in the stream.
|
||||
pub async fn stream_recv<S>(stream: &mut S) -> Option<S::Item>
|
||||
where
|
||||
|
@ -66,14 +57,6 @@ where
|
|||
poll_fn(|cx| Pin::new(&mut *stream).poll_next(cx)).await
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub async fn send<S, I>(sink: &mut S, item: I) -> Result<(), S::Error>
|
||||
where
|
||||
S: Sink<I> + Unpin,
|
||||
{
|
||||
sink_write(sink, item).await
|
||||
}
|
||||
|
||||
/// A future that completes after the given item has been fully processed
|
||||
/// into the sink, including flushing.
|
||||
pub async fn sink_write<S, I>(sink: &mut S, item: I) -> Result<(), S::Error>
|
||||
|
|
|
@ -310,7 +310,7 @@ mod tests {
|
|||
});
|
||||
|
||||
let srv = apply(
|
||||
Buffer::new(|| ()).buf_size(2),
|
||||
Buffer::new(|| ()).buf_size(2).clone(),
|
||||
fn_factory(|| async { Ok::<_, ()>(TestService(inner.clone())) }),
|
||||
);
|
||||
|
||||
|
|
|
@ -349,6 +349,7 @@ mod tests {
|
|||
async fn test_variant() {
|
||||
let factory = variant(fn_factory(|| async { Ok::<_, ()>(Srv1) }))
|
||||
.v2(fn_factory(|| async { Ok::<_, ()>(Srv2) }))
|
||||
.clone()
|
||||
.v3(fn_factory(|| async { Ok::<_, ()>(Srv2) }))
|
||||
.clone();
|
||||
let service = factory.create(&()).await.unwrap();
|
||||
|
|
|
@ -618,6 +618,15 @@ mod tests {
|
|||
Some(Bytes::from("test"))
|
||||
);
|
||||
assert!(poll_fn(|cx| b.poll_next_chunk(cx)).await.is_none(),);
|
||||
|
||||
let mut b = BytesMut::from("test");
|
||||
assert_eq!(b.size(), BodySize::Sized(4));
|
||||
assert_eq!(
|
||||
poll_fn(|cx| b.poll_next_chunk(cx)).await.unwrap().ok(),
|
||||
Some(Bytes::from("test"))
|
||||
);
|
||||
assert_eq!(b.size(), BodySize::Sized(0));
|
||||
assert!(poll_fn(|cx| b.poll_next_chunk(cx)).await.is_none(),);
|
||||
}
|
||||
|
||||
#[crate::rt_test]
|
||||
|
|
|
@ -1007,6 +1007,26 @@ mod tests {
|
|||
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(resp.body().get_ref(), b"test");
|
||||
|
||||
let builder = Response::build_from(ResponseBuilder::new(StatusCode::OK))
|
||||
.keep_alive()
|
||||
.if_true(true, |b| {
|
||||
b.header(CONTENT_TYPE, "ttt");
|
||||
})
|
||||
.if_some(Some(true), |_, b| {
|
||||
b.header(CONTENT_TYPE, "ttt2");
|
||||
})
|
||||
.take();
|
||||
let _ = builder.extensions();
|
||||
let resp: Response = builder.into();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
resp.headers().get_all(CONTENT_TYPE).collect::<Vec<_>>(),
|
||||
vec![
|
||||
HeaderValue::from_static("ttt"),
|
||||
HeaderValue::from_static("ttt2")
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue