|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 9 | +using Microsoft.Azure.WebJobs.Script.WebHost.Extensions; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Extensions |
| 14 | +{ |
| 15 | + public class FunctionMetadataExtensionsTests |
| 16 | + { |
| 17 | + private readonly string _testRootScriptPath; |
| 18 | + |
| 19 | + public FunctionMetadataExtensionsTests() |
| 20 | + { |
| 21 | + _testRootScriptPath = Path.GetTempPath(); |
| 22 | + } |
| 23 | + |
| 24 | + [Theory] |
| 25 | + [InlineData("invalid")] |
| 26 | + [InlineData("{ bindings: [ {} ] }")] |
| 27 | + [InlineData("{ bindings: 'not an array' }")] |
| 28 | + public async Task ToFunctionTrigger_InvalidConfig_ReturnsNull(string functionConfig) |
| 29 | + { |
| 30 | + var functionMetadata = new FunctionMetadata |
| 31 | + { |
| 32 | + Name = "TestFunction" |
| 33 | + }; |
| 34 | + var options = new ScriptJobHostOptions |
| 35 | + { |
| 36 | + RootScriptPath = _testRootScriptPath |
| 37 | + }; |
| 38 | + var functionPath = Path.Combine(_testRootScriptPath, functionMetadata.Name); |
| 39 | + var functionMetadataFilePath = Path.Combine(functionPath, ScriptConstants.FunctionMetadataFileName); |
| 40 | + FileUtility.EnsureDirectoryExists(functionPath); |
| 41 | + File.WriteAllText(functionMetadataFilePath, functionConfig); |
| 42 | + |
| 43 | + var result = await functionMetadata.ToFunctionTrigger(options); |
| 44 | + Assert.Null(result); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void GetFunctionInvokeUrlTemplate_ReturnsExpectedResult() |
| 49 | + { |
| 50 | + string baseUrl = "https://localhost"; |
| 51 | + var functionMetadata = new FunctionMetadata |
| 52 | + { |
| 53 | + Name = "TestFunction" |
| 54 | + }; |
| 55 | + var httpTriggerBinding = new BindingMetadata |
| 56 | + { |
| 57 | + Name = "req", |
| 58 | + Type = "httpTrigger", |
| 59 | + Direction = BindingDirection.In, |
| 60 | + Raw = new JObject() |
| 61 | + }; |
| 62 | + functionMetadata.Bindings.Add(httpTriggerBinding); |
| 63 | + var uri = FunctionMetadataExtensions.GetFunctionInvokeUrlTemplate(baseUrl, functionMetadata, "api"); |
| 64 | + Assert.Equal("https://localhost/api/testfunction", uri.ToString()); |
| 65 | + |
| 66 | + // with empty route prefix |
| 67 | + uri = FunctionMetadataExtensions.GetFunctionInvokeUrlTemplate(baseUrl, functionMetadata, string.Empty); |
| 68 | + Assert.Equal("https://localhost/testfunction", uri.ToString()); |
| 69 | + |
| 70 | + // with a custom route |
| 71 | + httpTriggerBinding.Raw.Add("route", "catalog/products/{category:alpha?}/{id:int?}"); |
| 72 | + uri = FunctionMetadataExtensions.GetFunctionInvokeUrlTemplate(baseUrl, functionMetadata, "api"); |
| 73 | + Assert.Equal("https://localhost/api/catalog/products/{category:alpha?}/{id:int?}", uri.ToString()); |
| 74 | + |
| 75 | + // with empty route prefix |
| 76 | + uri = FunctionMetadataExtensions.GetFunctionInvokeUrlTemplate(baseUrl, functionMetadata, string.Empty); |
| 77 | + Assert.Equal("https://localhost/catalog/products/{category:alpha?}/{id:int?}", uri.ToString()); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments