Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions python/echo/function/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@ def __init__(self):

async def handle(self, scope, receive, send):
""" Handle all HTTP requests to this Function other than readiness
and liveness probes. Echoes the query string back to the caller."""
and liveness probes. Echoes the query string on GET or the request
body on POST."""

query_string = scope.get('query_string', b'').decode('utf-8')
logging.info(f"Echoing query string: {query_string}")
method = scope.get('method', 'GET')

if method == 'POST':
body = b''
while True:
message = await receive()
body += message.get('body', b'')
if not message.get('more_body', False):
break
response_body = body
logging.info(f"Echoing request body: {response_body.decode('utf-8')}")
else:
query_string = scope.get('query_string', b'').decode('utf-8')
response_body = query_string.encode()
logging.info(f"Echoing query string: {query_string}")

await send({
'type': 'http.response.start',
Expand All @@ -32,7 +46,7 @@ async def handle(self, scope, receive, send):
})
await send({
'type': 'http.response.body',
'body': query_string.encode(),
'body': response_body,
})

def start(self, cfg):
Expand Down
16 changes: 9 additions & 7 deletions rust/echo/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::config::HandlerConfig;
use actix_web::{http::Method, web::Data, HttpRequest, HttpResponse};
use actix_web::{http::Method, web, web::Data, HttpRequest, HttpResponse};
use log::info;

// Implement your function's logic here
pub async fn index(req: HttpRequest, config: Data<HandlerConfig>) -> HttpResponse {
pub async fn index(req: HttpRequest, body: web::Bytes, config: Data<HandlerConfig>) -> HttpResponse {
info!("{:#?}", req);
if req.method() == Method::GET {
// Echo back the query string if present, otherwise return greeting
Expand All @@ -14,7 +14,8 @@ pub async fn index(req: HttpRequest, config: Data<HandlerConfig>) -> HttpRespons
HttpResponse::Ok().body(format!("Hello {}!\n", config.name))
}
} else {
HttpResponse::Ok().body(format!("Thanks {}!\n", config.name))
// Echo back the request body
HttpResponse::Ok().body(body)
}
}

Expand All @@ -30,7 +31,7 @@ mod tests {
#[actix_rt::test]
async fn get() {
let req = TestRequest::get().to_http_request();
let resp = index(req, config()).await;
let resp = index(req, Bytes::new(), config()).await;
assert_eq!(resp.status(), http::StatusCode::OK);
assert_eq!(
&Bytes::from(format!("Hello {}!\n", "world")),
Expand All @@ -43,7 +44,7 @@ mod tests {
let req = TestRequest::get()
.uri("/?test=param&message=hello")
.to_http_request();
let resp = index(req, config()).await;
let resp = index(req, Bytes::new(), config()).await;
assert_eq!(resp.status(), http::StatusCode::OK);
assert_eq!(
&Bytes::from("test=param&message=hello"),
Expand All @@ -53,11 +54,12 @@ mod tests {

#[actix_rt::test]
async fn post() {
let payload = Bytes::from("{\"message\":\"Hello World\"}");
let req = TestRequest::post().to_http_request();
let resp = index(req, config()).await;
let resp = index(req, payload.clone(), config()).await;
assert!(resp.status().is_success());
assert_eq!(
&Bytes::from(format!("Thanks {}!\n", "world")),
&payload,
to_bytes(resp.into_body()).await.unwrap().as_ref()
);
}
Expand Down
Loading