actix_http/
test.rs

1//! Various testing helpers for use in internal and app tests.
2
3use std::{
4    cell::{Ref, RefCell, RefMut},
5    io::{self, Read, Write},
6    pin::Pin,
7    rc::Rc,
8    str::FromStr,
9    task::{Context, Poll},
10};
11
12use actix_codec::{AsyncRead, AsyncWrite, ReadBuf};
13use bytes::{Bytes, BytesMut};
14use http::{header, Method, Uri, Version};
15
16use crate::{
17    header::{HeaderMap, TryIntoHeaderPair},
18    payload::Payload,
19    Request,
20};
21
22/// Test `Request` builder.
23pub struct TestRequest(Option<Inner>);
24
25struct Inner {
26    version: Version,
27    method: Method,
28    uri: Uri,
29    headers: HeaderMap,
30    payload: Option<Payload>,
31}
32
33impl Default for TestRequest {
34    fn default() -> TestRequest {
35        TestRequest(Some(Inner {
36            method: Method::GET,
37            uri: Uri::from_str("/").unwrap(),
38            version: Version::HTTP_11,
39            headers: HeaderMap::new(),
40            payload: None,
41        }))
42    }
43}
44
45impl TestRequest {
46    /// Create a default TestRequest and then set its URI.
47    pub fn with_uri(path: &str) -> TestRequest {
48        TestRequest::default().uri(path).take()
49    }
50
51    /// Set HTTP version of this request.
52    pub fn version(&mut self, ver: Version) -> &mut Self {
53        parts(&mut self.0).version = ver;
54        self
55    }
56
57    /// Set HTTP method of this request.
58    pub fn method(&mut self, meth: Method) -> &mut Self {
59        parts(&mut self.0).method = meth;
60        self
61    }
62
63    /// Set URI of this request.
64    ///
65    /// # Panics
66    /// If provided URI is invalid.
67    pub fn uri(&mut self, path: &str) -> &mut Self {
68        parts(&mut self.0).uri = Uri::from_str(path).unwrap();
69        self
70    }
71
72    /// Insert a header, replacing any that were set with an equivalent field name.
73    pub fn insert_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self {
74        match header.try_into_pair() {
75            Ok((key, value)) => {
76                parts(&mut self.0).headers.insert(key, value);
77            }
78            Err(err) => {
79                panic!("Error inserting test header: {}.", err.into());
80            }
81        }
82
83        self
84    }
85
86    /// Append a header, keeping any that were set with an equivalent field name.
87    pub fn append_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self {
88        match header.try_into_pair() {
89            Ok((key, value)) => {
90                parts(&mut self.0).headers.append(key, value);
91            }
92            Err(err) => {
93                panic!("Error inserting test header: {}.", err.into());
94            }
95        }
96
97        self
98    }
99
100    /// Set request payload.
101    ///
102    /// This sets the `Content-Length` header with the size of `data`.
103    pub fn set_payload(&mut self, data: impl Into<Bytes>) -> &mut Self {
104        let mut payload = crate::h1::Payload::empty();
105        let bytes = data.into();
106        self.insert_header((header::CONTENT_LENGTH, bytes.len()));
107        payload.unread_data(bytes);
108        parts(&mut self.0).payload = Some(payload.into());
109        self
110    }
111
112    pub fn take(&mut self) -> TestRequest {
113        TestRequest(self.0.take())
114    }
115
116    /// Complete request creation and generate `Request` instance.
117    pub fn finish(&mut self) -> Request {
118        let inner = self.0.take().expect("cannot reuse test request builder");
119
120        let mut req = if let Some(pl) = inner.payload {
121            Request::with_payload(pl)
122        } else {
123            Request::with_payload(crate::h1::Payload::empty().into())
124        };
125
126        let head = req.head_mut();
127        head.uri = inner.uri;
128        head.method = inner.method;
129        head.version = inner.version;
130        head.headers = inner.headers;
131
132        req
133    }
134}
135
136#[inline]
137fn parts(parts: &mut Option<Inner>) -> &mut Inner {
138    parts.as_mut().expect("cannot reuse test request builder")
139}
140
141/// Async I/O test buffer.
142#[derive(Debug)]
143pub struct TestBuffer {
144    pub read_buf: Rc<RefCell<BytesMut>>,
145    pub write_buf: Rc<RefCell<BytesMut>>,
146    pub err: Option<Rc<io::Error>>,
147}
148
149impl TestBuffer {
150    /// Create new `TestBuffer` instance with initial read buffer.
151    pub fn new<T>(data: T) -> Self
152    where
153        T: Into<BytesMut>,
154    {
155        Self {
156            read_buf: Rc::new(RefCell::new(data.into())),
157            write_buf: Rc::new(RefCell::new(BytesMut::new())),
158            err: None,
159        }
160    }
161
162    // intentionally not using Clone trait
163    #[allow(dead_code)]
164    pub(crate) fn clone(&self) -> Self {
165        Self {
166            read_buf: Rc::clone(&self.read_buf),
167            write_buf: Rc::clone(&self.write_buf),
168            err: self.err.clone(),
169        }
170    }
171
172    /// Create new empty `TestBuffer` instance.
173    pub fn empty() -> Self {
174        Self::new("")
175    }
176
177    #[allow(dead_code)]
178    pub(crate) fn read_buf_slice(&self) -> Ref<'_, [u8]> {
179        Ref::map(self.read_buf.borrow(), |b| b.as_ref())
180    }
181
182    #[allow(dead_code)]
183    pub(crate) fn read_buf_slice_mut(&self) -> RefMut<'_, [u8]> {
184        RefMut::map(self.read_buf.borrow_mut(), |b| b.as_mut())
185    }
186
187    #[allow(dead_code)]
188    pub(crate) fn write_buf_slice(&self) -> Ref<'_, [u8]> {
189        Ref::map(self.write_buf.borrow(), |b| b.as_ref())
190    }
191
192    #[allow(dead_code)]
193    pub(crate) fn write_buf_slice_mut(&self) -> RefMut<'_, [u8]> {
194        RefMut::map(self.write_buf.borrow_mut(), |b| b.as_mut())
195    }
196
197    #[allow(dead_code)]
198    pub(crate) fn take_write_buf(&self) -> Bytes {
199        self.write_buf.borrow_mut().split().freeze()
200    }
201
202    /// Add data to read buffer.
203    pub fn extend_read_buf<T: AsRef<[u8]>>(&mut self, data: T) {
204        self.read_buf.borrow_mut().extend_from_slice(data.as_ref())
205    }
206}
207
208impl io::Read for TestBuffer {
209    fn read(&mut self, dst: &mut [u8]) -> Result<usize, io::Error> {
210        if self.read_buf.borrow().is_empty() {
211            if self.err.is_some() {
212                Err(Rc::try_unwrap(self.err.take().unwrap()).unwrap())
213            } else {
214                Err(io::Error::new(io::ErrorKind::WouldBlock, ""))
215            }
216        } else {
217            let size = std::cmp::min(self.read_buf.borrow().len(), dst.len());
218            let b = self.read_buf.borrow_mut().split_to(size);
219            dst[..size].copy_from_slice(&b);
220            Ok(size)
221        }
222    }
223}
224
225impl io::Write for TestBuffer {
226    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
227        self.write_buf.borrow_mut().extend(buf);
228        Ok(buf.len())
229    }
230
231    fn flush(&mut self) -> io::Result<()> {
232        Ok(())
233    }
234}
235
236impl AsyncRead for TestBuffer {
237    fn poll_read(
238        self: Pin<&mut Self>,
239        _: &mut Context<'_>,
240        buf: &mut ReadBuf<'_>,
241    ) -> Poll<io::Result<()>> {
242        let dst = buf.initialize_unfilled();
243        let res = self.get_mut().read(dst).map(|n| buf.advance(n));
244        Poll::Ready(res)
245    }
246}
247
248impl AsyncWrite for TestBuffer {
249    fn poll_write(
250        self: Pin<&mut Self>,
251        _: &mut Context<'_>,
252        buf: &[u8],
253    ) -> Poll<io::Result<usize>> {
254        Poll::Ready(self.get_mut().write(buf))
255    }
256
257    fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
258        Poll::Ready(Ok(()))
259    }
260
261    fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
262        Poll::Ready(Ok(()))
263    }
264}
265
266/// Async I/O test buffer with ability to incrementally add to the read buffer.
267#[derive(Clone)]
268pub struct TestSeqBuffer(Rc<RefCell<TestSeqInner>>);
269
270impl TestSeqBuffer {
271    /// Create new `TestBuffer` instance with initial read buffer.
272    pub fn new<T>(data: T) -> Self
273    where
274        T: Into<BytesMut>,
275    {
276        Self(Rc::new(RefCell::new(TestSeqInner {
277            read_buf: data.into(),
278            read_closed: false,
279            write_buf: BytesMut::new(),
280            err: None,
281        })))
282    }
283
284    /// Create new empty `TestBuffer` instance.
285    pub fn empty() -> Self {
286        Self::new(BytesMut::new())
287    }
288
289    pub fn read_buf(&self) -> Ref<'_, BytesMut> {
290        Ref::map(self.0.borrow(), |inner| &inner.read_buf)
291    }
292
293    pub fn write_buf(&self) -> Ref<'_, BytesMut> {
294        Ref::map(self.0.borrow(), |inner| &inner.write_buf)
295    }
296
297    pub fn take_write_buf(&self) -> Bytes {
298        self.0.borrow_mut().write_buf.split().freeze()
299    }
300
301    pub fn err(&self) -> Ref<'_, Option<io::Error>> {
302        Ref::map(self.0.borrow(), |inner| &inner.err)
303    }
304
305    /// Add data to read buffer.
306    ///
307    /// # Panics
308    ///
309    /// Panics if called after [`TestSeqBuffer::close_read`] has been called
310    pub fn extend_read_buf<T: AsRef<[u8]>>(&mut self, data: T) {
311        let mut inner = self.0.borrow_mut();
312        if inner.read_closed {
313            panic!("Tried to extend the read buffer after calling close_read");
314        }
315
316        inner.read_buf.extend_from_slice(data.as_ref())
317    }
318
319    /// Closes the [`AsyncRead`]/[`Read`] part of this test buffer.
320    ///
321    /// The current data in the buffer will still be returned by a call to read/poll_read, however,
322    /// after the buffer is empty, it will return `Ok(0)` to signify the EOF condition
323    pub fn close_read(&self) {
324        self.0.borrow_mut().read_closed = true;
325    }
326}
327
328pub struct TestSeqInner {
329    read_buf: BytesMut,
330    read_closed: bool,
331    write_buf: BytesMut,
332    err: Option<io::Error>,
333}
334
335impl io::Read for TestSeqBuffer {
336    fn read(&mut self, dst: &mut [u8]) -> Result<usize, io::Error> {
337        let mut inner = self.0.borrow_mut();
338
339        if inner.read_buf.is_empty() {
340            if let Some(err) = inner.err.take() {
341                Err(err)
342            } else if inner.read_closed {
343                Ok(0)
344            } else {
345                Err(io::Error::new(io::ErrorKind::WouldBlock, ""))
346            }
347        } else {
348            let size = std::cmp::min(inner.read_buf.len(), dst.len());
349            let b = inner.read_buf.split_to(size);
350            dst[..size].copy_from_slice(&b);
351            Ok(size)
352        }
353    }
354}
355
356impl io::Write for TestSeqBuffer {
357    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
358        self.0.borrow_mut().write_buf.extend(buf);
359        Ok(buf.len())
360    }
361
362    fn flush(&mut self) -> io::Result<()> {
363        Ok(())
364    }
365}
366
367impl AsyncRead for TestSeqBuffer {
368    fn poll_read(
369        self: Pin<&mut Self>,
370        _: &mut Context<'_>,
371        buf: &mut ReadBuf<'_>,
372    ) -> Poll<io::Result<()>> {
373        let dst = buf.initialize_unfilled();
374        let r = self.get_mut().read(dst);
375        match r {
376            Ok(n) => {
377                buf.advance(n);
378                Poll::Ready(Ok(()))
379            }
380            Err(err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
381            Err(err) => Poll::Ready(Err(err)),
382        }
383    }
384}
385
386impl AsyncWrite for TestSeqBuffer {
387    fn poll_write(
388        self: Pin<&mut Self>,
389        _: &mut Context<'_>,
390        buf: &[u8],
391    ) -> Poll<io::Result<usize>> {
392        Poll::Ready(self.get_mut().write(buf))
393    }
394
395    fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
396        Poll::Ready(Ok(()))
397    }
398
399    fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
400        Poll::Ready(Ok(()))
401    }
402}