Skip to content

Commit 4034f33

Browse files
authored
Merge pull request #516 from yusuke-ota/fix_test_response
Uncomment and fix tests/response
2 parents 12c2e22 + 8fd000f commit 4034f33

File tree

1 file changed

+74
-54
lines changed

1 file changed

+74
-54
lines changed

tests/response.rs

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,74 @@
1-
// use async_std::task::block_on;
2-
// use tide::*;
3-
4-
// #[test]
5-
// fn test_status() {
6-
// let resp = "foo"
7-
// .with_status(http::status::StatusCode::NOT_FOUND)
8-
// .into_response();
9-
// assert_eq!(resp.status(), http::status::StatusCode::NOT_FOUND);
10-
// assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
11-
// }
12-
13-
// #[test]
14-
// fn byte_vec_content_type() {
15-
// let resp = String::from("foo").into_bytes().into_response();
16-
// assert_eq!(resp.headers()["Content-Type"], "application/octet-stream");
17-
// assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
18-
// }
19-
20-
// #[test]
21-
// fn string_content_type() {
22-
// let resp = String::from("foo").into_response();
23-
// assert_eq!(resp.headers()["Content-Type"], "text/plain; charset=utf-8");
24-
// assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
25-
// }
26-
27-
// #[test]
28-
// fn json_content_type() {
29-
// use std::collections::BTreeMap;
30-
31-
// let mut map = BTreeMap::new();
32-
// map.insert(Some("a"), 2);
33-
// map.insert(Some("b"), 4);
34-
// map.insert(None, 6);
35-
36-
// let resp = json(map);
37-
// assert_eq!(
38-
// resp.status(),
39-
// http::status::StatusCode::INTERNAL_SERVER_ERROR
40-
// );
41-
// assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"");
42-
43-
// let mut map = BTreeMap::new();
44-
// map.insert("a", 2);
45-
// map.insert("b", 4);
46-
// map.insert("c", 6);
47-
48-
// let resp = json(map);
49-
// assert_eq!(resp.status(), http::status::StatusCode::OK);
50-
// assert_eq!(
51-
// block_on(resp.into_body().into_vec()).unwrap(),
52-
// br##"{"a":2,"b":4,"c":6}"##
53-
// );
54-
// }
1+
use tide::*;
2+
3+
#[async_std::test]
4+
async fn test_status() {
5+
let mut resp = Response::new(StatusCode::NotFound).body_string("foo".to_string());
6+
assert_eq!(resp.status(), StatusCode::NotFound);
7+
let foo = resp.take_body().into_string().await.unwrap();
8+
assert_eq!(foo.as_bytes(), b"foo");
9+
}
10+
11+
#[async_std::test]
12+
async fn byte_vec_content_type() {
13+
use async_std::io::Cursor;
14+
use http_types::headers::HeaderName;
15+
use std::str::FromStr;
16+
17+
let mut resp = Response::new(StatusCode::Ok).body(Cursor::new("foo"));
18+
let header_values = resp
19+
.header(&HeaderName::from_str("Content-Type").unwrap())
20+
.unwrap();
21+
assert_eq!(header_values[0], mime::APPLICATION_OCTET_STREAM.to_string());
22+
let foo = resp.take_body().into_string().await.unwrap();
23+
assert_eq!(foo.as_bytes(), b"foo");
24+
}
25+
26+
#[async_std::test]
27+
async fn string_content_type() {
28+
use http_types::headers::HeaderName;
29+
use std::str::FromStr;
30+
31+
let mut resp = Response::new(StatusCode::Ok).body_string("foo".to_string());
32+
let header_values = resp
33+
.header(&HeaderName::from_str("Content-Type").unwrap())
34+
.unwrap();
35+
assert_eq!(header_values[0], mime::TEXT_PLAIN_UTF_8.to_string());
36+
let foo = resp.take_body().into_string().await.unwrap();
37+
assert_eq!(foo.as_bytes(), b"foo");
38+
}
39+
40+
#[async_std::test]
41+
async fn json_content_type() {
42+
use http_types::{Method, Url};
43+
use std::collections::BTreeMap;
44+
45+
let mut app = tide::new();
46+
app.at("/json_content_type").get(|_| async move {
47+
let mut map = BTreeMap::new();
48+
map.insert(Some("a"), 2);
49+
map.insert(Some("b"), 4);
50+
map.insert(None, 6);
51+
let resp = Response::new(StatusCode::Ok).body_json(&map)?;
52+
Ok(resp)
53+
});
54+
let req = http::Request::new(
55+
Method::Get,
56+
Url::parse("http://localhost/json_content_type").unwrap(),
57+
);
58+
let mut resp: http::Response = app.respond(req).await.unwrap();
59+
assert_eq!(resp.status(), StatusCode::InternalServerError);
60+
let body = resp.take_body().into_string().await.unwrap();
61+
assert_eq!(body.as_bytes(), b"");
62+
63+
let mut resp = async move {
64+
let mut map = BTreeMap::new();
65+
map.insert("a", 2);
66+
map.insert("b", 4);
67+
map.insert("c", 6);
68+
Response::new(StatusCode::Ok).body_json(&map).unwrap()
69+
}
70+
.await;
71+
assert_eq!(resp.status(), StatusCode::Ok);
72+
let body = resp.take_body().into_string().await.unwrap();
73+
assert_eq!(body.as_bytes(), br##"{"a":2,"b":4,"c":6}"##);
74+
}

0 commit comments

Comments
 (0)