mirror of https://github.com/knative/func.git
feat: introduce echo return for Python http template (#1078)
* Introduce echo return for Python http template * Updated tests and regenerated zz* file * Updated output to dict-only * Changed output type from Python dict literal to proper JSON
This commit is contained in:
parent
bb831c7c12
commit
5f8c01829e
|
@ -1,4 +1,47 @@
|
|||
from parliament import Context
|
||||
from flask import Request
|
||||
import json
|
||||
|
||||
|
||||
# parse request body, json data or URL query parameters
|
||||
def payload_print(req: Request) -> str:
|
||||
if req.method == "POST":
|
||||
if req.is_json:
|
||||
return json.dumps(req.json) + "\n"
|
||||
else:
|
||||
# MultiDict needs some iteration
|
||||
ret = "{"
|
||||
|
||||
for key in req.form.keys():
|
||||
ret += '"' + key + '": "'+ req.form[key] + '", '
|
||||
|
||||
return ret[:-2] + "}\n" if len(ret) > 2 else "{}"
|
||||
|
||||
elif req.method == "GET":
|
||||
# MultiDict needs some iteration
|
||||
ret = "{"
|
||||
|
||||
for key in req.args.keys():
|
||||
ret += '"' + key + '": "' + req.args[key] + '", '
|
||||
|
||||
return ret[:-2] + "}\n" if len(ret) > 2 else "{}"
|
||||
|
||||
|
||||
# pretty print the request to stdout instantaneously
|
||||
def pretty_print(req: Request) -> str:
|
||||
ret = str(req.method) + ' ' + str(req.url) + ' ' + str(req.host) + '\n'
|
||||
for (header, values) in req.headers:
|
||||
ret += " " + str(header) + ": " + values + '\n'
|
||||
|
||||
if req.method == "POST":
|
||||
ret += "Request body:\n"
|
||||
ret += " " + payload_print(req) + '\n'
|
||||
|
||||
elif req.method == "GET":
|
||||
ret += "URL Query String:\n"
|
||||
ret += " " + payload_print(req) + '\n'
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def main(context: Context):
|
||||
|
@ -9,5 +52,12 @@ def main(context: Context):
|
|||
"""
|
||||
|
||||
# Add your business logic here
|
||||
print("Received request")
|
||||
|
||||
return { "message": "Howdy!" }, 200
|
||||
if 'request' in context.keys():
|
||||
ret = pretty_print(context.request)
|
||||
print(ret, flush=True)
|
||||
return payload_print(context.request), 200
|
||||
else:
|
||||
print("Empty request", flush=True)
|
||||
return "{}", 200
|
||||
|
|
|
@ -4,9 +4,9 @@ func = __import__("func")
|
|||
|
||||
class TestFunc(unittest.TestCase):
|
||||
|
||||
def test_func(self):
|
||||
def test_func_empty_request(self):
|
||||
resp, code = func.main({})
|
||||
self.assertEqual(resp["message"], "Howdy!")
|
||||
self.assertEqual(resp, "{}")
|
||||
self.assertEqual(code, 200)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -78,8 +78,8 @@ var defaultFunctionsHttpValidators = []FunctionHttpResponsivenessValidator{
|
|||
expects: `OK`,
|
||||
},
|
||||
{runtime: "python",
|
||||
targetUrl: "%s",
|
||||
expects: `Howdy!`,
|
||||
targetUrl: "%s?message=hello",
|
||||
expects: `{"message": "hello"}`,
|
||||
},
|
||||
{runtime: "quarkus",
|
||||
targetUrl: "%s?message=hello",
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue