Skip to content

Commit d077f47

Browse files
authored
Merge pull request #6 from jtgeibel/new-body
Add support for changes to `conduit::Body`
2 parents b4da96b + b71595f commit d077f47

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
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: 34 additions & 5 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,
@@ -146,7 +175,7 @@ mod tests {
146175
assert_eq!(req.content_length(), None);
147176
assert_eq!(req.headers().len(), 0);
148177
let mut s = String::new();
149-
req.body().read_to_string(&mut s).ok().expect("No body");
178+
req.body().read_to_string(&mut s).expect("No body");
150179
assert_eq!(s, "".to_string());
151180
}
152181

@@ -158,7 +187,7 @@ mod tests {
158187
assert_eq!(req.method(), Method::POST);
159188
assert_eq!(req.path(), "/articles");
160189
let mut s = String::new();
161-
req.body().read_to_string(&mut s).ok().expect("No body");
190+
req.body().read_to_string(&mut s).expect("No body");
162191
assert_eq!(s, "Hello world".to_string());
163192
assert_eq!(req.content_length(), Some(11));
164193
}

0 commit comments

Comments
 (0)