Skip to content

Commit b71595f

Browse files
committed
Add support for changes to conduit::Body
1 parent fe70417 commit b71595f

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22

33
name = "conduit-test"
4-
version = "0.9.0-alpha.1"
4+
version = "0.9.0-alpha.2"
55
authors = ["[email protected]",
66
"Alex Crichton <[email protected]>"]
77
description = "Testing utilities for conduit-based stacks"
88
repository = "https://github.com/conduit-rust/conduit-test"
99
license = "MIT"
1010

1111
[dependencies]
12-
conduit = "0.9.0-alpha.1"
12+
conduit = { git = "https://github.com/jtgeibel/conduit", rev = "0deff5b" } # "0.9.0-alpha.2"

src/lib.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
#![warn(rust_2018_idioms)]
22
extern crate conduit;
33

4-
use std::io::prelude::*;
5-
use std::io::Cursor;
4+
use std::borrow::Cow;
5+
use std::io::{Cursor, Read};
66
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
77

88
use conduit::{
99
header::{HeaderValue, IntoHeaderName},
10-
Extensions, HeaderMap, Host, Method, Scheme, TypeMap, Version,
10+
Body, Extensions, HeaderMap, Host, Method, Response, Scheme, TypeMap, Version,
1111
};
1212

13+
pub trait ResponseExt {
14+
fn into_cow(self) -> Cow<'static, [u8]>;
15+
}
16+
17+
impl ResponseExt for Response<Body> {
18+
/// Convert the request into a copy-on-write body
19+
///
20+
/// # Blocking
21+
///
22+
/// This function may block if the value is a `Body::File`.
23+
///
24+
/// # Panics
25+
///
26+
/// This function panics if there is an error reading a `Body::File`.
27+
fn into_cow(self) -> Cow<'static, [u8]> {
28+
use conduit::Body::*;
29+
30+
match self.into_body() {
31+
Static(slice) => slice.into(),
32+
Owned(vec) => vec.into(),
33+
File(mut file) => {
34+
let mut vec = Vec::new();
35+
std::io::copy(&mut file, &mut vec).unwrap();
36+
vec.into()
37+
}
38+
}
39+
}
40+
}
41+
1342
pub struct MockRequest {
1443
path: String,
1544
method: Method,

0 commit comments

Comments
 (0)