|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using FluentAssertions; |
| 4 | +using Microsoft.OpenApi.Extensions; |
| 5 | +using Microsoft.OpenApi.Models; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Microsoft.OpenApi.Tests.Extensions; |
| 9 | + |
| 10 | +public class OpenApiServerExtensionsTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void ShouldSubstituteServerVariableWithProvidedValues() |
| 14 | + { |
| 15 | + var variable = new OpenApiServer |
| 16 | + { |
| 17 | + Url = "http://example.com/api/{version}", |
| 18 | + Description = string.Empty, |
| 19 | + Variables = new Dictionary<string, OpenApiServerVariable> |
| 20 | + { |
| 21 | + { "version", new OpenApiServerVariable { Default = "v1", Enum = ["v1", "v2"]} } |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + var url = variable.ReplaceServerUrlVariables(new Dictionary<string, string> {{"version", "v2"}}); |
| 26 | + |
| 27 | + url.Should().Be("http://example.com/api/v2"); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void ShouldSubstituteServerVariableWithDefaultValues() |
| 32 | + { |
| 33 | + var variable = new OpenApiServer |
| 34 | + { |
| 35 | + Url = "http://example.com/api/{version}", |
| 36 | + Description = string.Empty, |
| 37 | + Variables = new Dictionary<string, OpenApiServerVariable> |
| 38 | + { |
| 39 | + { "version", new OpenApiServerVariable { Default = "v1", Enum = ["v1", "v2"]} } |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + var url = variable.ReplaceServerUrlVariables(new Dictionary<string, string>(0)); |
| 44 | + |
| 45 | + url.Should().Be("http://example.com/api/v1"); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void ShouldFailIfNoValueIsAvailable() |
| 50 | + { |
| 51 | + var variable = new OpenApiServer |
| 52 | + { |
| 53 | + Url = "http://example.com/api/{version}", |
| 54 | + Description = string.Empty, |
| 55 | + Variables = new Dictionary<string, OpenApiServerVariable> |
| 56 | + { |
| 57 | + { "version", new OpenApiServerVariable { Enum = ["v1", "v2"]} } |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + Assert.Throws<ArgumentException>(() => |
| 62 | + { |
| 63 | + variable.ReplaceServerUrlVariables(new Dictionary<string, string>(0)); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void ShouldFailIfProvidedValueIsNotInEnum() |
| 69 | + { |
| 70 | + var variable = new OpenApiServer |
| 71 | + { |
| 72 | + Url = "http://example.com/api/{version}", |
| 73 | + Description = string.Empty, |
| 74 | + Variables = new Dictionary<string, OpenApiServerVariable> |
| 75 | + { |
| 76 | + { "version", new OpenApiServerVariable { Enum = ["v1", "v2"]} } |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + Assert.Throws<ArgumentException>(() => |
| 81 | + { |
| 82 | + variable.ReplaceServerUrlVariables(new Dictionary<string, string> {{"version", "v3"}}); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void ShouldFailIfEnumIsEmpty() |
| 88 | + { |
| 89 | + var variable = new OpenApiServer |
| 90 | + { |
| 91 | + Url = "http://example.com/api/{version}", |
| 92 | + Description = string.Empty, |
| 93 | + Variables = new Dictionary<string, OpenApiServerVariable> |
| 94 | + { |
| 95 | + { "version", new OpenApiServerVariable { Enum = []} } |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + Assert.Throws<ArgumentException>(() => |
| 100 | + { |
| 101 | + variable.ReplaceServerUrlVariables(new Dictionary<string, string> {{"version", "v1"}}); |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
0 commit comments