1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
use async_h1::{
client::Encoder,
server::{ConnectionStatus, Server},
};
use async_std::io::{Read as AsyncRead, Write as AsyncWrite};
use http_types::{Request, Response, Result};
use std::{
fmt::{Debug, Display},
future::Future,
io,
pin::Pin,
sync::RwLock,
task::{Context, Poll, Waker},
};
use async_dup::Arc;
#[pin_project::pin_project]
pub struct TestServer<F, Fut> {
server: Server<TestIO, F, Fut>,
#[pin]
client: TestIO,
}
impl<F, Fut> TestServer<F, Fut>
where
F: Fn(Request) -> Fut,
Fut: Future<Output = Result<Response>>,
{
#[allow(dead_code)]
pub fn new(f: F) -> Self {
let (client, server) = TestIO::new();
Self {
server: Server::new(server, f),
client,
}
}
#[allow(dead_code)]
pub async fn accept_one(&mut self) -> http_types::Result<ConnectionStatus> {
self.server.accept_one().await
}
#[allow(dead_code)]
pub fn close(&mut self) {
self.client.close();
}
#[allow(dead_code)]
pub fn all_read(&self) -> bool {
self.client.all_read()
}
#[allow(dead_code)]
pub async fn write_request(&mut self, request: Request) -> io::Result<()> {
async_std::io::copy(&mut Encoder::new(request), self).await?;
Ok(())
}
}
impl<F, Fut> AsyncRead for TestServer<F, Fut>
where
F: Fn(Request) -> Fut,
Fut: Future<Output = Result<Response>>,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
self.project().client.poll_read(cx, buf)
}
}
impl<F, Fut> AsyncWrite for TestServer<F, Fut>
where
F: Fn(Request) -> Fut,
Fut: Future<Output = Result<Response>>,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.project().client.poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.project().client.poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.project().client.poll_close(cx)
}
}
/// a Test IO
#[derive(Default, Clone, Debug)]
pub struct TestIO {
pub read: Arc<CloseableCursor>,
pub write: Arc<CloseableCursor>,
}
#[derive(Default)]
struct CloseableCursorInner {
data: Vec<u8>,
cursor: usize,
waker: Option<Waker>,
closed: bool,
}
#[derive(Default)]
pub struct CloseableCursor(RwLock<CloseableCursorInner>);
impl CloseableCursor {
pub fn len(&self) -> usize {
self.0.read().unwrap().data.len()
}
pub fn cursor(&self) -> usize {
self.0.read().unwrap().cursor
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn current(&self) -> bool {
let inner = self.0.read().unwrap();
inner.data.len() == inner.cursor
}
pub fn close(&self) {
let mut inner = self.0.write().unwrap();
inner.closed = true;
if let Some(waker) = inner.waker.take() {
waker.wake();
}
}
}
impl Display for CloseableCursor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let inner = self.0.read().unwrap();
let s = std::str::from_utf8(&inner.data).unwrap_or("not utf8");
write!(f, "{}", s)
}
}
impl TestIO {
pub fn new() -> (TestIO, TestIO) {
let client = Arc::new(CloseableCursor::default());
let server = Arc::new(CloseableCursor::default());
(
TestIO {
read: client.clone(),
write: server.clone(),
},
TestIO {
read: server,
write: client,
},
)
}
pub fn all_read(&self) -> bool {
self.write.current()
}
pub fn close(&mut self) {
self.write.close();
}
}
impl Debug for CloseableCursor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let inner = self.0.read().unwrap();
f.debug_struct("CloseableCursor")
.field(
"data",
&std::str::from_utf8(&inner.data).unwrap_or("not utf8"),
)
.field("closed", &inner.closed)
.field("cursor", &inner.cursor)
.finish()
}
}
impl AsyncRead for CloseableCursor {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut &*self).poll_read(cx, buf)
}
}
impl AsyncRead for &CloseableCursor {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let mut inner = self.0.write().unwrap();
if inner.cursor < inner.data.len() {
let bytes_to_copy = buf.len().min(inner.data.len() - inner.cursor);
buf[..bytes_to_copy]
.copy_from_slice(&inner.data[inner.cursor..inner.cursor + bytes_to_copy]);
inner.cursor += bytes_to_copy;
Poll::Ready(Ok(bytes_to_copy))
} else if inner.closed {
Poll::Ready(Ok(0))
} else {
inner.waker = Some(cx.waker().clone());
Poll::Pending
}
}
}
impl AsyncWrite for &CloseableCursor {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let mut inner = self.0.write().unwrap();
if inner.closed {
Poll::Ready(Ok(0))
} else {
inner.data.extend_from_slice(buf);
if let Some(waker) = inner.waker.take() {
waker.wake();
}
Poll::Ready(Ok(buf.len()))
}
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.close();
Poll::Ready(Ok(()))
}
}
impl AsyncRead for TestIO {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut &*self.read).poll_read(cx, buf)
}
}
impl AsyncWrite for TestIO {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut &*self.write).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut &*self.write).poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut &*self.write).poll_close(cx)
}
}
impl std::io::Write for CloseableCursor {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write().unwrap().data.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
|