|
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 async_std::io::prelude::*; |
| 2 | +use tide::*; |
| 3 | + |
| 4 | +#[async_std::test] |
| 5 | +async fn test_status() { |
| 6 | + let mut resp = Response::new(StatusCode::NotFound).body_string("foo".to_string()); |
| 7 | + assert_eq!(resp.status(), StatusCode::NotFound); |
| 8 | + |
| 9 | + let mut foo = String::new(); |
| 10 | + let _bytes = resp.take_body().read_to_string(&mut foo).await; |
| 11 | + assert_eq!(foo.as_bytes(), b"foo"); |
| 12 | +} |
| 13 | + |
| 14 | +#[async_std::test] |
| 15 | +async fn byte_vec_content_type() { |
| 16 | + use async_std::io::Cursor; |
| 17 | + use http_types::headers::{HeaderName, HeaderValue}; |
| 18 | + use std::str::FromStr; |
| 19 | + |
| 20 | + let mut resp = Response::new(StatusCode::Ok).body(Cursor::new("foo")); |
| 21 | + |
| 22 | + let mut header_values: Vec<HeaderValue> = Vec::new(); |
| 23 | + let content_type = HeaderName::from_str("Content-Type").unwrap(); |
| 24 | + if let Some(x) = resp.remove_header(&content_type) { |
| 25 | + header_values = x; |
| 26 | + } |
| 27 | + assert_eq!(header_values[0], "application/octet-stream"); |
| 28 | + |
| 29 | + let mut foo = String::new(); |
| 30 | + let _bytes = resp.take_body().read_to_string(&mut foo).await; |
| 31 | + assert_eq!(foo.as_bytes(), b"foo"); |
| 32 | +} |
| 33 | + |
| 34 | +#[async_std::test] |
| 35 | +async fn string_content_type() { |
| 36 | + use http_types::headers::{HeaderName, HeaderValue}; |
| 37 | + use std::str::FromStr; |
| 38 | + |
| 39 | + let mut resp = Response::new(StatusCode::Ok).body_string("foo".to_string()); |
| 40 | + |
| 41 | + let mut header_values: Vec<HeaderValue> = Vec::new(); |
| 42 | + let content_type = HeaderName::from_str("Content-Type").unwrap(); |
| 43 | + if let Some(x) = resp.remove_header(&content_type) { |
| 44 | + header_values = x; |
| 45 | + } |
| 46 | + assert_eq!(header_values[0], "text/plain; charset=utf-8"); |
| 47 | + |
| 48 | + let mut foo = String::new(); |
| 49 | + let _bytes = resp.take_body().read_to_string(&mut foo).await; |
| 50 | + assert_eq!(foo.as_bytes(), b"foo"); |
| 51 | +} |
| 52 | + |
| 53 | +#[async_std::test] |
| 54 | +async fn json_content_type() { |
| 55 | + use http_types::Method; |
| 56 | + use std::collections::BTreeMap; |
| 57 | + |
| 58 | + let mut app = tide::new(); |
| 59 | + app.at("/json_content_type").get(|_| async move { |
| 60 | + let mut map = BTreeMap::new(); |
| 61 | + map.insert(Some("a"), 2); |
| 62 | + map.insert(Some("b"), 4); |
| 63 | + map.insert(None, 6); |
| 64 | + println!("{:?}", serde_json::to_vec(&map)); |
| 65 | + |
| 66 | + let resp = Response::new(StatusCode::Ok).body_json(&map)?; |
| 67 | + Ok(resp) |
| 68 | + }); |
| 69 | + |
| 70 | + let req = http::Request::new( |
| 71 | + Method::Get, |
| 72 | + "http://localhost/json_content_type".parse().unwrap(), |
| 73 | + ); |
| 74 | + let mut resp: http::Response = app.respond(req).await.unwrap(); |
| 75 | + assert_eq!(resp.status(), StatusCode::InternalServerError); |
| 76 | + |
| 77 | + let mut body = String::new(); |
| 78 | + let _bytes = resp.take_body().read_to_string(&mut body).await; |
| 79 | + assert_eq!(body.as_bytes(), b""); |
| 80 | + |
| 81 | + let mut map = BTreeMap::new(); |
| 82 | + map.insert("a", 2); |
| 83 | + map.insert("b", 4); |
| 84 | + map.insert("c", 6); |
| 85 | + |
| 86 | + let mut resp = Response::new(StatusCode::Ok).body_json(&map).unwrap(); |
| 87 | + assert_eq!(resp.status(), StatusCode::Ok); |
| 88 | + |
| 89 | + let mut body = String::new(); |
| 90 | + let _bytes = resp.take_body().read_to_string(&mut body).await; |
| 91 | + assert_eq!(body.as_bytes(), br##"{"a":2,"b":4,"c":6}"##); |
| 92 | +} |
0 commit comments