From 5be3aef55300ef1c59ab1e0fee42ff8626e44a5e Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Mon, 11 Jun 2018 21:21:44 +0200 Subject: [PATCH] test all resp.into_xxx() --- README.md | 2 +- src/test/simple.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8f5361b..f35631c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - [x] Header handling - [x] Transfer-Encoding: chunked - [x] Ergonomic JSON handling -- [ ] Test harness for end-to-end tests +- [x] Test harness for end-to-end tests - [ ] Limit read length on Content-Size - [ ] Auth headers - [ ] Cookie jar in agent diff --git a/src/test/simple.rs b/src/test/simple.rs index 7631fb2..cf1e16e 100644 --- a/src/test/simple.rs +++ b/src/test/simple.rs @@ -1,6 +1,8 @@ -use super::super::*; +use std::io::Read; use test; +use super::super::*; + #[test] fn header_passing() { test::set_handler("/header_passing", |req, _url| { @@ -23,3 +25,30 @@ fn body_as_text() { let text = resp.into_string().unwrap(); assert_eq!(text, "Hello World!"); } + +#[test] +fn body_as_json() { + test::set_handler("/body_as_json", |_req, _url| { + test::make_stream( + 200, + "OK", + vec![], + "{\"hello\":\"world\"}".to_string().into_bytes(), + ) + }); + let resp = get("test://host/body_as_json").call(); + let json = resp.into_json().unwrap(); + assert_eq!(json["hello"], "world"); +} + +#[test] +fn body_as_reader() { + test::set_handler("/body_as_reader", |_req, _url| { + test::make_stream(200, "OK", vec![], "abcdefgh".to_string().into_bytes()) + }); + let resp = get("test://host/body_as_reader").call(); + let mut reader = resp.into_reader(); + let mut text = String::new(); + reader.read_to_string(&mut text).unwrap(); + assert_eq!(text, "abcdefgh"); +}