Skip to content

Commit

Permalink
Fixed error when methods is not specified (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Oct 30, 2023
1 parent aa6ba5c commit 13026cb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AppServiceProxy.Tests/AppServiceProxy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.2" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
29 changes: 29 additions & 0 deletions AppServiceProxy.Tests/ProxiesJsonReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,33 @@ public void ResponseOverrides()
Assert.Equal("OK", proxies[0].ResponseOverrides!.StatusReason);
Assert.Equal("text/plain", proxies[0].ResponseOverrides!.Headers["Content-Type"]);
}

[Fact]
public void Optional_Methods()
{
const string json = @"
{
""$schema"": ""http://json.schemastore.org/proxies"",
""proxies"": {
""proxy1"": {
""matchCondition"": {
""route"": ""/api/{test}""
},
""backendUri"": ""https://<AnotherApp>.azurewebsites.net/api/<FunctionName>""
}
}
}
";

var proxies = ProxiesJsonReader.ParseJson(json);

Assert.Single(proxies);
Assert.Equal("proxy1", proxies[0].Name);
Assert.False(proxies[0].Disabled);
Assert.Null(proxies[0].MatchCondition.Methods);
Assert.Equal("/api/{test}", proxies[0].MatchCondition.Route);
Assert.Equal("https://<AnotherApp>.azurewebsites.net/api/<FunctionName>", proxies[0].BackendUri);
Assert.Null(proxies[0].RequestOverrides);
Assert.Null(proxies[0].ResponseOverrides);
}
}
37 changes: 37 additions & 0 deletions AppServiceProxy.Tests/ProxiesJsonTransformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,41 @@ public void EnvironmentVariable()

Assert.Equal("https://example.com", clusters[0].Destinations![$"Cluster_{proxies[0].Name}/destination"].Address);
}

[Fact]
public void Optional_Methods()
{
const string json = @"
{
""$schema"": ""http://json.schemastore.org/proxies"",
""proxies"": {
""proxy1"": {
""matchCondition"": {
""route"": ""/api/{test}""
},
""backendUri"": ""https://<AnotherApp>.azurewebsites.net/backend/{test}""
}
}
}
";

var proxies = ProxiesJsonReader.ParseJson(json);

var (routes, clusters) = ProxiesJsonTransform.Apply(proxies);

Assert.Single(routes);
Assert.Single(clusters);

Assert.Equal($"Route_{proxies[0].Name}", routes[0].RouteId);
Assert.Equal($"Cluster_{proxies[0].Name}", routes[0].ClusterId);
Assert.Equal($"Cluster_{proxies[0].Name}", clusters[0].ClusterId);
Assert.Equal(proxies[0].MatchCondition.Methods, routes[0].Match.Methods);
Assert.Equal(proxies[0].MatchCondition.Route, routes[0].Match.Path);

Assert.NotNull(routes[0].Transforms);
Assert.Single(routes[0].Transforms!);

Assert.Equal("/backend/{test}", routes[0].Transforms![0]["PathPattern"]);
Assert.Equal("https://<AnotherApp>.azurewebsites.net", clusters[0].Destinations![$"Cluster_{proxies[0].Name}/destination"].Address);
}
}
2 changes: 1 addition & 1 deletion AppServiceProxy/Configuration/ProxiesJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class ProxyConfig

internal class MatchConditionConfig
{
public IReadOnlyList<string> Methods { get; init; } = null!;
public IReadOnlyList<string>? Methods { get; init; } = null!;

public string Route { get; init; } = null!;
}
Expand Down
2 changes: 1 addition & 1 deletion AppServiceProxy/Configuration/ProxiesJsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private static ProxyConfig ParseProxyConfig(JsonProperty proxy)

var matchConditionConfig = new MatchConditionConfig
{
Methods = matchCondition.GetProperty("methods").EnumerateArray().Select(x => x.GetString()!).ToArray(),
Methods = matchCondition.TryGetProperty("methods", out var methods) ? methods.EnumerateArray().Select(x => x.GetString()!).ToArray() : null,
Route = matchCondition.GetProperty("route").GetString()!
};

Expand Down

0 comments on commit 13026cb

Please sign in to comment.