|
1 |
| -use crate::{ |
2 |
| - requests::{IntoResponse, NextEventResponse}, |
3 |
| - Error, |
4 |
| -}; |
5 |
| -use http::{ |
6 |
| - uri::{PathAndQuery, Scheme}, |
7 |
| - HeaderValue, Method, Request, Response, StatusCode, Uri, |
8 |
| -}; |
9 |
| -use hyper::{client::HttpConnector, server::conn::Http, service::service_fn, Body}; |
10 |
| -use serde_json::json; |
11 |
| -use std::convert::TryFrom; |
12 |
| -use tokio::{ |
13 |
| - io::{AsyncRead, AsyncWrite}, |
14 |
| - select, |
15 |
| - sync::oneshot, |
16 |
| -}; |
17 |
| -use tracing::{error, info, instrument}; |
18 |
| - |
19 |
| -#[instrument] |
20 |
| -async fn hello(req: Request<Body>) -> Result<Response<Body>, Error> { |
21 |
| - Ok(Response::new(Body::from("hello"))) |
22 |
| -} |
23 |
| - |
24 |
| -async fn handle_incoming(req: Request<Body>) -> Result<Response<Body>, Error> { |
25 |
| - let path: Vec<&str> = req |
26 |
| - .uri() |
27 |
| - .path_and_query() |
28 |
| - .unwrap() |
29 |
| - .as_str() |
30 |
| - .split("/") |
31 |
| - .collect::<Vec<&str>>(); |
32 |
| - match &path[1..] { |
33 |
| - ["2018-06-01", "runtime", "invocation", "next"] => next_event(&req).await, |
34 |
| - ["2018-06-01", "runtime", "invocation", id, "response"] => complete_event(&req, id).await, |
35 |
| - ["2018-06-01", "runtime", "invocation", id, "error"] => event_err(&req, id).await, |
36 |
| - ["2018-06-01", "runtime", "init", "error"] => unimplemented!(), |
37 |
| - _ => unimplemented!(), |
38 |
| - } |
39 |
| -} |
40 |
| - |
41 |
| -#[instrument(skip(io, rx))] |
42 |
| -async fn handle<I>(io: I, rx: oneshot::Receiver<()>) -> Result<(), hyper::error::Error> |
43 |
| -where |
44 |
| - I: AsyncRead + AsyncWrite + Unpin + 'static, |
45 |
| -{ |
46 |
| - let conn = Http::new().serve_connection(io, service_fn(handle_incoming)); |
47 |
| - select! { |
48 |
| - _ = rx => { |
49 |
| - info!("Received cancelation signal"); |
50 |
| - return Ok(()) |
51 |
| - } |
52 |
| - res = conn => { |
53 |
| - match res { |
54 |
| - Ok(()) => return Ok(()), |
55 |
| - Err(e) => { |
56 |
| - error!(message = "Got error serving connection", e = %e); |
57 |
| - return Err(e); |
58 |
| - } |
59 |
| - } |
60 |
| - } |
61 |
| - } |
62 |
| -} |
| 1 | +use crate::Error; |
| 2 | +use http::{uri::Scheme, Request, Response, Uri}; |
| 3 | +use hyper::{client::HttpConnector, Body}; |
63 | 4 |
|
64 | 5 | #[derive(Debug)]
|
65 | 6 | pub(crate) struct Client<C = HttpConnector> {
|
@@ -105,83 +46,139 @@ where
|
105 | 46 | }
|
106 | 47 | }
|
107 | 48 |
|
108 |
| -async fn next_event(req: &Request<Body>) -> Result<Response<Body>, Error> { |
109 |
| - let path = "/2018-06-01/runtime/invocation/next"; |
110 |
| - assert_eq!(req.method(), Method::GET); |
111 |
| - assert_eq!(req.uri().path_and_query().unwrap(), &PathAndQuery::from_static(path)); |
112 |
| - let body = json!({"message": "hello"}); |
113 |
| - |
114 |
| - let rsp = NextEventResponse { |
115 |
| - request_id: "8476a536-e9f4-11e8-9739-2dfe598c3fcd", |
116 |
| - deadline: 1_542_409_706_888, |
117 |
| - arn: "arn:aws:lambda:us-east-2:123456789012:function:custom-runtime", |
118 |
| - trace_id: "Root=1-5bef4de7-ad49b0e87f6ef6c87fc2e700;Parent=9a9197af755a6419", |
119 |
| - body: serde_json::to_vec(&body)?, |
| 49 | +#[cfg(test)] |
| 50 | +mod endpoint_tests { |
| 51 | + use crate::{ |
| 52 | + requests::{ |
| 53 | + EventCompletionRequest, EventErrorRequest, IntoRequest, IntoResponse, NextEventRequest, NextEventResponse, |
| 54 | + }, |
| 55 | + simulated::Connector, |
| 56 | + types::Diagnostic, |
| 57 | + Error, |
120 | 58 | };
|
121 |
| - rsp.into_rsp().map_err(|e| e.into()) |
122 |
| -} |
| 59 | + use http::{ |
| 60 | + uri::{PathAndQuery, Scheme}, |
| 61 | + HeaderValue, Method, Request, Response, StatusCode, Uri, |
| 62 | + }; |
| 63 | + use hyper::{server::conn::Http, service::service_fn, Body}; |
| 64 | + use serde_json::json; |
| 65 | + use std::convert::TryFrom; |
| 66 | + use tokio::{ |
| 67 | + io::{AsyncRead, AsyncWrite}, |
| 68 | + select, sync, |
| 69 | + sync::oneshot, |
| 70 | + }; |
| 71 | + use tracing::{error, info, instrument}; |
123 | 72 |
|
124 |
| -async fn complete_event(req: &Request<Body>, id: &str) -> Result<Response<Body>, Error> { |
125 |
| - assert_eq!(Method::POST, req.method()); |
126 |
| - let rsp = Response::builder() |
127 |
| - .status(StatusCode::ACCEPTED) |
128 |
| - .body(Body::empty()) |
129 |
| - .expect("Unable to construct response"); |
| 73 | + #[instrument] |
| 74 | + async fn hello(req: Request<Body>) -> Result<Response<Body>, Error> { |
| 75 | + Ok(Response::new(Body::from("hello"))) |
| 76 | + } |
130 | 77 |
|
131 |
| - let expected = format!("/2018-06-01/runtime/invocation/{}/response", id); |
132 |
| - assert_eq!(expected, req.uri().path()); |
| 78 | + async fn handle_incoming(req: Request<Body>) -> Result<Response<Body>, Error> { |
| 79 | + let path: Vec<&str> = req |
| 80 | + .uri() |
| 81 | + .path_and_query() |
| 82 | + .unwrap() |
| 83 | + .as_str() |
| 84 | + .split("/") |
| 85 | + .collect::<Vec<&str>>(); |
| 86 | + match &path[1..] { |
| 87 | + ["2018-06-01", "runtime", "invocation", "next"] => next_event(&req).await, |
| 88 | + ["2018-06-01", "runtime", "invocation", id, "response"] => complete_event(&req, id).await, |
| 89 | + ["2018-06-01", "runtime", "invocation", id, "error"] => event_err(&req, id).await, |
| 90 | + ["2018-06-01", "runtime", "init", "error"] => unimplemented!(), |
| 91 | + _ => unimplemented!(), |
| 92 | + } |
| 93 | + } |
133 | 94 |
|
134 |
| - Ok(rsp) |
135 |
| -} |
| 95 | + #[instrument(skip(io, rx))] |
| 96 | + async fn handle<I>(io: I, rx: oneshot::Receiver<()>) -> Result<(), hyper::error::Error> |
| 97 | + where |
| 98 | + I: AsyncRead + AsyncWrite + Unpin + 'static, |
| 99 | + { |
| 100 | + let conn = Http::new().serve_connection(io, service_fn(handle_incoming)); |
| 101 | + select! { |
| 102 | + _ = rx => { |
| 103 | + info!("Received cancelation signal"); |
| 104 | + return Ok(()) |
| 105 | + } |
| 106 | + res = conn => { |
| 107 | + match res { |
| 108 | + Ok(()) => return Ok(()), |
| 109 | + Err(e) => { |
| 110 | + error!(message = "Got error serving connection", e = %e); |
| 111 | + return Err(e); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
136 | 117 |
|
137 |
| -async fn event_err(req: &Request<Body>, id: &str) -> Result<Response<Body>, Error> { |
138 |
| - let expected = format!("/2018-06-01/runtime/invocation/{}/error", id); |
139 |
| - assert_eq!(expected, req.uri().path()); |
| 118 | + async fn next_event(req: &Request<Body>) -> Result<Response<Body>, Error> { |
| 119 | + let path = "/2018-06-01/runtime/invocation/next"; |
| 120 | + assert_eq!(req.method(), Method::GET); |
| 121 | + assert_eq!(req.uri().path_and_query().unwrap(), &PathAndQuery::from_static(path)); |
| 122 | + let body = json!({"message": "hello"}); |
| 123 | + |
| 124 | + let rsp = NextEventResponse { |
| 125 | + request_id: "8476a536-e9f4-11e8-9739-2dfe598c3fcd", |
| 126 | + deadline: 1_542_409_706_888, |
| 127 | + arn: "arn:aws:lambda:us-east-2:123456789012:function:custom-runtime", |
| 128 | + trace_id: "Root=1-5bef4de7-ad49b0e87f6ef6c87fc2e700;Parent=9a9197af755a6419", |
| 129 | + body: serde_json::to_vec(&body)?, |
| 130 | + }; |
| 131 | + rsp.into_rsp().map_err(|e| e.into()) |
| 132 | + } |
140 | 133 |
|
141 |
| - assert_eq!(req.method(), Method::POST); |
142 |
| - let header = "lambda-runtime-function-error-type"; |
143 |
| - let expected = "unhandled"; |
144 |
| - assert_eq!(req.headers()[header], HeaderValue::try_from(expected)?); |
| 134 | + async fn complete_event(req: &Request<Body>, id: &str) -> Result<Response<Body>, Error> { |
| 135 | + assert_eq!(Method::POST, req.method()); |
| 136 | + let rsp = Response::builder() |
| 137 | + .status(StatusCode::ACCEPTED) |
| 138 | + .body(Body::empty()) |
| 139 | + .expect("Unable to construct response"); |
145 | 140 |
|
146 |
| - let rsp = Response::builder().status(StatusCode::ACCEPTED).body(Body::empty())?; |
147 |
| - Ok(rsp) |
148 |
| -} |
| 141 | + let expected = format!("/2018-06-01/runtime/invocation/{}/response", id); |
| 142 | + assert_eq!(expected, req.uri().path()); |
149 | 143 |
|
150 |
| -fn set_origin<B>(base: Uri, req: Request<B>) -> Result<Request<B>, Error> { |
151 |
| - let (mut parts, body) = req.into_parts(); |
152 |
| - let (scheme, authority) = { |
153 |
| - let scheme = base.scheme().unwrap_or(&Scheme::HTTP); |
154 |
| - let authority = base.authority().expect("Authority not found"); |
155 |
| - (scheme, authority) |
156 |
| - }; |
157 |
| - let path = parts.uri.path_and_query().expect("PathAndQuery not found"); |
| 144 | + Ok(rsp) |
| 145 | + } |
158 | 146 |
|
159 |
| - let uri = Uri::builder() |
160 |
| - .scheme(scheme.clone()) |
161 |
| - .authority(authority.clone()) |
162 |
| - .path_and_query(path.clone()) |
163 |
| - .build() |
164 |
| - .expect("Unable to build URI"); |
| 147 | + async fn event_err(req: &Request<Body>, id: &str) -> Result<Response<Body>, Error> { |
| 148 | + let expected = format!("/2018-06-01/runtime/invocation/{}/error", id); |
| 149 | + assert_eq!(expected, req.uri().path()); |
165 | 150 |
|
166 |
| - parts.uri = uri; |
167 |
| - Ok(Request::from_parts(parts, body)) |
168 |
| -} |
| 151 | + assert_eq!(req.method(), Method::POST); |
| 152 | + let header = "lambda-runtime-function-error-type"; |
| 153 | + let expected = "unhandled"; |
| 154 | + assert_eq!(req.headers()[header], HeaderValue::try_from(expected)?); |
169 | 155 |
|
170 |
| -#[cfg(test)] |
171 |
| -mod endpoint_tests { |
172 |
| - use super::{handle, set_origin}; |
173 |
| - use crate::{ |
174 |
| - requests::{EventCompletionRequest, EventErrorRequest, IntoRequest, NextEventRequest}, |
175 |
| - simulated::Connector, |
176 |
| - types::Diagnostic, |
177 |
| - Error, |
178 |
| - }; |
179 |
| - use http::{HeaderValue, StatusCode, Uri}; |
180 |
| - use std::convert::TryFrom; |
181 |
| - use tokio::sync; |
| 156 | + let rsp = Response::builder().status(StatusCode::ACCEPTED).body(Body::empty())?; |
| 157 | + Ok(rsp) |
| 158 | + } |
| 159 | + |
| 160 | + fn set_origin<B>(base: Uri, req: Request<B>) -> Result<Request<B>, Error> { |
| 161 | + let (mut parts, body) = req.into_parts(); |
| 162 | + let (scheme, authority) = { |
| 163 | + let scheme = base.scheme().unwrap_or(&Scheme::HTTP); |
| 164 | + let authority = base.authority().expect("Authority not found"); |
| 165 | + (scheme, authority) |
| 166 | + }; |
| 167 | + let path = parts.uri.path_and_query().expect("PathAndQuery not found"); |
| 168 | + |
| 169 | + let uri = Uri::builder() |
| 170 | + .scheme(scheme.clone()) |
| 171 | + .authority(authority.clone()) |
| 172 | + .path_and_query(path.clone()) |
| 173 | + .build() |
| 174 | + .expect("Unable to build URI"); |
| 175 | + |
| 176 | + parts.uri = uri; |
| 177 | + Ok(Request::from_parts(parts, body)) |
| 178 | + } |
182 | 179 |
|
183 | 180 | #[tokio::test]
|
184 |
| - async fn next_event() -> Result<(), Error> { |
| 181 | + async fn test_next_event() -> Result<(), Error> { |
185 | 182 | let (client, server) = crate::simulated::chan();
|
186 | 183 | let base = Uri::from_static("http://localhost:9001");
|
187 | 184 |
|
|
0 commit comments