Skip to content

Commit 0ed9017

Browse files
authored
Added generic tests for http and servicebus (#1043)
1 parent 3c23a24 commit 0ed9017

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
app = func.FunctionApp()
9+
10+
11+
@app.function_name(name="default_template")
12+
@app.generic_trigger(arg_name="req",
13+
type="httpTrigger",
14+
route="default_template")
15+
@app.generic_output_binding(arg_name="$return", type="http")
16+
def default_template(req: func.HttpRequest) -> func.HttpResponse:
17+
logging.info('Python HTTP trigger function processed a request.')
18+
19+
name = req.params.get('name')
20+
if not name:
21+
try:
22+
req_body = req.get_json()
23+
except ValueError:
24+
pass
25+
else:
26+
name = req_body.get('name')
27+
28+
if name:
29+
return func.HttpResponse(
30+
f"Hello, {name}. This HTTP triggered function "
31+
f"executed successfully.")
32+
else:
33+
return func.HttpResponse(
34+
"This HTTP triggered function executed successfully. "
35+
"Pass a name in the query string or in the request body for a"
36+
" personalized response.",
37+
status_code=200
38+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import json
2+
3+
import azure.functions as func
4+
5+
app = func.FunctionApp()
6+
7+
8+
@app.function_name(name="put_message")
9+
@app.generic_trigger(arg_name="req", type="httpTrigger", route="put_message")
10+
@app.generic_output_binding(arg_name="msg",
11+
type="serviceBus",
12+
connection="AzureWebJobsServiceBusConnectionString",
13+
queue_name="testqueue")
14+
@app.generic_output_binding(arg_name="$return", type="http")
15+
def put_message(req: func.HttpRequest, msg: func.Out[str]):
16+
msg.set(req.get_body().decode('utf-8'))
17+
return 'OK'
18+
19+
20+
@app.function_name(name="get_servicebus_triggered")
21+
@app.generic_trigger(arg_name="req", type="httpTrigger",
22+
route="get_servicebus_triggered")
23+
@app.generic_input_binding(arg_name="file",
24+
type="blob",
25+
path="python-worker-tests/test-servicebus-triggered.txt", # NoQA
26+
connection="AzureWebJobsStorage")
27+
@app.generic_output_binding(arg_name="$return", type="http")
28+
def get_servicebus_triggered(req: func.HttpRequest,
29+
file: func.InputStream) -> str:
30+
return func.HttpResponse(
31+
file.read().decode('utf-8'), mimetype='application/json')
32+
33+
34+
@app.generic_trigger(
35+
arg_name="msg",
36+
type="serviceBusTrigger",
37+
connection="AzureWebJobsServiceBusConnectionString",
38+
queue_name="testqueue")
39+
@app.generic_output_binding(arg_name="$return",
40+
path="python-worker-tests/test-servicebus-triggered.txt", # NoQA
41+
type="blob",
42+
connection="AzureWebJobsStorage")
43+
def servicebus_trigger(msg: func.ServiceBusMessage) -> str:
44+
result = json.dumps({
45+
'message_id': msg.message_id,
46+
'body': msg.get_body().decode('utf-8'),
47+
'content_type': msg.content_type,
48+
'delivery_count': msg.delivery_count,
49+
'expiration_time': (msg.expiration_time.isoformat() if
50+
msg.expiration_time else None),
51+
'label': msg.label,
52+
'partition_key': msg.partition_key,
53+
'reply_to': msg.reply_to,
54+
'reply_to_session_id': msg.reply_to_session_id,
55+
'scheduled_enqueue_time': (msg.scheduled_enqueue_time.isoformat() if
56+
msg.scheduled_enqueue_time else None),
57+
'session_id': msg.session_id,
58+
'time_to_live': msg.time_to_live,
59+
'to': msg.to,
60+
'user_properties': msg.user_properties,
61+
})
62+
63+
return result

tests/endtoend/test_http_functions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,12 @@ class TestHttpFunctionsStein(TestHttpFunctions):
114114
def get_script_dir(cls):
115115
return testutils.E2E_TESTS_FOLDER / 'http_functions' /\
116116
'http_functions_stein'
117+
118+
119+
class TestHttpFunctionsSteinGeneric(TestHttpFunctions):
120+
121+
@classmethod
122+
def get_script_dir(cls):
123+
return testutils.E2E_TESTS_FOLDER / 'http_functions' /\
124+
'http_functions_stein' /\
125+
'generic'

tests/endtoend/test_servicebus_functions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ class TestServiceBusFunctionsStein(TestServiceBusFunctions):
4444
def get_script_dir(cls):
4545
return testutils.E2E_TESTS_FOLDER / 'servicebus_functions' / \
4646
'servicebus_functions_stein'
47+
48+
49+
class TestServiceBusFunctionsSteinGeneric(TestServiceBusFunctions):
50+
51+
@classmethod
52+
def get_script_dir(cls):
53+
return testutils.E2E_TESTS_FOLDER / 'servicebus_functions' / \
54+
'servicebus_functions_stein' / \
55+
'generic'

0 commit comments

Comments
 (0)