Rename test function as_write_vec -> into_written_bytes

Tests use `Response::as_write_vec` to inspect the outgoing HTTP/1.1
request line and headers. The current version has two problems:

1. Called `as_write_vec` when it actually returns a `&[u8]`.
2. Inspects/uses the `Response::stream` without consuming `Response`.

The first problem is trivial, but the second is subtle. Currently all
calls on `Response` that works with the internal `Response::stream`
consumes `self` (`into_string`, `into_reader`).

`Response` is by itself `Send + Sync`, and must be so because the
nested Stream is `Read + Write + Send + Sync`. However for
implementors of `TLSStream`, it would be nice to relax the `Sync`
requirement.

Assumption: If all fields in Response are `Sync` except
`Response::stream`, but any access to `stream` consumes `Response`, we
can consider the entire `Response` `Sync`.

This assumption can help us relax the `TlsStream` `Sync` requirement
in a later PR.
This commit is contained in:
Martin Algesten
2022-01-29 11:06:29 +01:00
parent 6e5041044b
commit 219b5edf9e
5 changed files with 27 additions and 22 deletions

View File

@@ -37,8 +37,10 @@ pub(crate) struct Stream {
trait Inner: Read + Write {
fn is_poolable(&self) -> bool;
fn socket(&self) -> Option<&TcpStream>;
fn as_write_vec(&self) -> &[u8] {
panic!("as_write_vec on non Test stream");
/// The bytes written to the stream as a Vec<u8>. This is used for tests only.
fn written_bytes(&self) -> Vec<u8> {
panic!("written_bytes on non Test stream");
}
}
@@ -76,8 +78,10 @@ impl Inner for TestStream {
fn socket(&self) -> Option<&TcpStream> {
None
}
fn as_write_vec(&self) -> &[u8] {
&self.1
/// For tests only
fn written_bytes(&self) -> Vec<u8> {
self.1.clone()
}
}
@@ -274,8 +278,8 @@ impl Stream {
}
#[cfg(test)]
pub fn as_write_vec(&self) -> &[u8] {
self.inner.get_ref().as_write_vec()
pub fn written_bytes(&self) -> Vec<u8> {
self.inner.get_ref().written_bytes()
}
}