Skip to content

Commit 2a2ccbf

Browse files
gavin-aguiarGavin Aguiar
andauthored
Added validation for route name for http triggers (#1066)
* Validating route name * Added unit tests * Refactored unit tests * Added check for function type * Addressed comments * changed function name Co-authored-by: Gavin Aguiar <[email protected]>
1 parent 00a907b commit 2a2ccbf

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

azure_functions_worker/functions.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ def get_return_binding(binding_name: str,
7878

7979
return return_binding_name
8080

81+
@staticmethod
82+
def validate_binding_route(func_name: str, binding: BindingInfo,
83+
func_type: str):
84+
if hasattr(binding, 'route') and binding.route.startswith(
85+
'/') and func_type == 'function':
86+
raise FunctionLoadError(
87+
func_name,
88+
f'Invalid route name: {binding.route}. Route name cannot begin'
89+
f' with a /')
90+
8191
@staticmethod
8292
def validate_binding_direction(binding_name: str,
8393
binding_direction: str,
@@ -93,6 +103,14 @@ def validate_binding_direction(binding_name: str,
93103
func_name,
94104
'"$return" binding must have direction set to "out"')
95105

106+
def validate_binding(self, func_name: str, binding: BindingInfo,
107+
func_type: str):
108+
self.validate_binding_route(func_name, binding, func_type)
109+
110+
self.validate_binding_direction(binding.name,
111+
binding.direction,
112+
func_name)
113+
96114
@staticmethod
97115
def is_context_required(params, bound_params: dict,
98116
annotations: dict,
@@ -357,6 +375,7 @@ def add_indexed_function(self, function_id: str,
357375
function):
358376
func = function.get_user_function()
359377
func_name = function.get_function_name()
378+
func_type = function.http_type
360379
return_binding_name: typing.Optional[str] = None
361380
has_explicit_return = False
362381
has_implicit_return = False
@@ -368,9 +387,7 @@ def add_indexed_function(self, function_id: str,
368387

369388
bound_params = {}
370389
for binding in function.get_bindings():
371-
self.validate_binding_direction(binding.name,
372-
binding.direction,
373-
func_name)
390+
self.validate_binding(func_name, binding, func_type)
374391

375392
has_explicit_return, has_implicit_return = \
376393
self.get_explicit_and_implicit_return(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import unittest
5+
6+
from azure.functions import Function
7+
from azure.functions.decorators.blob import BlobInput
8+
from azure.functions.decorators.http import HttpTrigger
9+
10+
from azure_functions_worker import functions
11+
from azure_functions_worker.functions import FunctionLoadError
12+
13+
14+
class TestFunctionsRegistry(unittest.TestCase):
15+
16+
def setUp(self) -> None:
17+
def dummy():
18+
return "test"
19+
20+
self.dummy = dummy
21+
self.func = Function(self.dummy, "test.py")
22+
self.function_registry = functions.Registry()
23+
24+
def test_add_indexed_function_invalid_route(self):
25+
trigger1 = HttpTrigger(name="req1", route="/")
26+
self.func.add_trigger(trigger=trigger1)
27+
28+
with self.assertRaises(FunctionLoadError) as ex:
29+
self.function_registry.add_indexed_function(function_id='123',
30+
function=self.func)
31+
32+
self.assertEqual(str(ex.exception),
33+
'cannot load the dummy function: Invalid route name: '
34+
'/. Route name cannot begin with a /')
35+
36+
def test_add_indexed_function_invalid_direction(self):
37+
trigger1 = HttpTrigger(name="req1", route="test")
38+
binding = BlobInput(name="$return", path="testpath",
39+
connection="testconnection")
40+
self.func.add_trigger(trigger=trigger1)
41+
self.func.add_binding(binding=binding)
42+
43+
with self.assertRaises(FunctionLoadError) as ex:
44+
self.function_registry.add_indexed_function(function_id='123',
45+
function=self.func)
46+
47+
self.assertEqual(str(ex.exception),
48+
'cannot load the dummy function: \"$return\" '
49+
'binding must have direction set to \"out\"')

0 commit comments

Comments
 (0)