1
1
#![ feature( async_await) ]
2
2
#![ deny( warnings) ]
3
- extern crate hyper;
4
- extern crate pretty_env_logger;
5
- extern crate serde_json;
6
3
7
4
use hyper:: { Body , Chunk , Client , Method , Request , Response , Server , StatusCode , header} ;
8
5
use hyper:: client:: HttpConnector ;
9
- use hyper:: service:: { service_fn , make_service_fn } ;
6
+ use hyper:: service:: { make_service_fn , service_fn } ;
10
7
use futures_util:: { TryStreamExt } ;
11
8
12
9
type GenericError = Box < dyn std:: error:: Error + Send + Sync > ;
10
+ type Result < T > = std:: result:: Result < T , GenericError > ;
13
11
14
- static NOTFOUND : & [ u8 ] = b"Not Found" ;
15
- static URL : & str = "http://127.0.0.1:1337/json_api" ;
16
12
static INDEX : & [ u8 ] = b"<a href=\" test.html\" >test.html</a>" ;
13
+ static INTERNAL_SERVER_ERROR : & [ u8 ] = b"Internal Server Error" ;
14
+ static NOTFOUND : & [ u8 ] = b"Not Found" ;
17
15
static POST_DATA : & str = r#"{"original": "data"}"# ;
16
+ static URL : & str = "http://127.0.0.1:1337/json_api" ;
17
+
18
+ async fn client_request_response (
19
+ client : & Client < HttpConnector >
20
+ ) -> Result < Response < Body > > {
21
+ let req = Request :: builder ( )
22
+ . method ( Method :: POST )
23
+ . uri ( URL )
24
+ . header ( header:: CONTENT_TYPE , "application/json" )
25
+ . body ( POST_DATA . into ( ) )
26
+ . unwrap ( ) ;
18
27
19
- async fn client_request_response ( client : & Client < HttpConnector > )
20
- -> Result < Response < Body > , GenericError >
21
- {
22
- let req = Request :: builder ( )
23
- . method ( Method :: POST )
24
- . uri ( URL )
25
- . header ( header:: CONTENT_TYPE , "application/json" )
26
- . body ( POST_DATA . into ( ) )
27
- . unwrap ( ) ;
28
-
29
- let web_res = client. request ( req) . await ?;
28
+ let web_res = client. request ( req) . await ?;
30
29
// Compare the JSON we sent (before) with what we received (after):
31
30
let body = Body :: wrap_stream ( web_res. into_body ( ) . map_ok ( |b| {
32
31
Chunk :: from ( format ! ( "<b>POST request body</b>: {}<br><b>Response</b>: {}" ,
@@ -37,9 +36,7 @@ async fn client_request_response(client: &Client<HttpConnector>)
37
36
Ok ( Response :: new ( body) )
38
37
}
39
38
40
- async fn api_post_response ( req : Request < Body > )
41
- -> Result < Response < Body > , GenericError >
42
- {
39
+ async fn api_post_response ( req : Request < Body > ) -> Result < Response < Body > > {
43
40
// A web api to run against
44
41
let entire_body = req. into_body ( ) . try_concat ( ) . await ?;
45
42
// TODO: Replace all unwraps with proper error handling
@@ -54,7 +51,7 @@ async fn api_post_response(req: Request<Body>)
54
51
Ok ( response)
55
52
}
56
53
57
- async fn api_get_response ( ) -> Result < Response < Body > , GenericError > {
54
+ async fn api_get_response ( ) -> Result < Response < Body > > {
58
55
let data = vec ! [ "foo" , "bar" ] ;
59
56
let res = match serde_json:: to_string ( & data) {
60
57
Ok ( json) => {
@@ -66,20 +63,21 @@ async fn api_get_response() -> Result<Response<Body>, GenericError> {
66
63
Err ( _) => {
67
64
Response :: builder ( )
68
65
. status ( StatusCode :: INTERNAL_SERVER_ERROR )
69
- . body ( Body :: from ( "Internal Server Error" ) )
66
+ . body ( INTERNAL_SERVER_ERROR . into ( ) )
70
67
. unwrap ( )
71
68
}
72
69
} ;
73
70
Ok ( res)
74
71
}
75
72
76
- async fn response_examples ( req : Request < Body > , client : Client < HttpConnector > )
77
- -> Result < Response < Body > , GenericError >
78
- {
73
+ async fn response_examples (
74
+ req : Request < Body > ,
75
+ client : Client < HttpConnector >
76
+ ) -> Result < Response < Body > > {
79
77
match ( req. method ( ) , req. uri ( ) . path ( ) ) {
80
- ( & Method :: GET , "/" ) | ( & Method :: GET , "/index.html" ) => {
81
- let body = Body :: from ( INDEX ) ;
82
- Ok ( Response :: new ( body ) )
78
+ ( & Method :: GET , "/" ) |
79
+ ( & Method :: GET , "/index.html" ) => {
80
+ Ok ( Response :: new ( INDEX . into ( ) ) )
83
81
} ,
84
82
( & Method :: GET , "/test.html" ) => {
85
83
client_request_response ( & client) . await
@@ -92,17 +90,16 @@ async fn response_examples(req: Request<Body>, client: Client<HttpConnector>)
92
90
}
93
91
_ => {
94
92
// Return 404 not found response.
95
- let body = Body :: from ( NOTFOUND ) ;
96
93
Ok ( Response :: builder ( )
97
- . status ( StatusCode :: NOT_FOUND )
98
- . body ( body )
99
- . unwrap ( ) )
94
+ . status ( StatusCode :: NOT_FOUND )
95
+ . body ( NOTFOUND . into ( ) )
96
+ . unwrap ( ) )
100
97
}
101
98
}
102
99
}
103
100
104
101
#[ hyper:: rt:: main]
105
- async fn main ( ) -> Result < ( ) , GenericError > {
102
+ async fn main ( ) -> Result < ( ) > {
106
103
pretty_env_logger:: init ( ) ;
107
104
108
105
let addr = "127.0.0.1:1337" . parse ( ) . unwrap ( ) ;
0 commit comments