-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ToolTests.cs
98 lines (81 loc) · 4.52 KB
/
ToolTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.IO;
using System.Text.Json;
using Swashbuckle.AspNetCore.TestSupport.Utilities;
using Xunit;
namespace Swashbuckle.AspNetCore.Cli.Test
{
public class ToolTests
{
[Fact]
public void Throws_When_Startup_Assembly_Does_Not_Exist()
{
var args = new string[] { "tofile", "--output", "swagger.json", "--serializeasv2", "./does_not_exist.dll", "v1" };
Assert.Throws<FileNotFoundException>(() => Program.Main(args));
}
[Fact]
public void Can_Generate_Swagger_Json()
{
using var temporaryDirectory = new TemporaryDirectory();
var args = new string[] { "tofile", "--output", $"{temporaryDirectory.Path}/swagger.json", "--serializeasv2", Path.Combine(Directory.GetCurrentDirectory(), "Basic.dll"), "v1" };
Assert.Equal(0, Program.Main(args));
using var document = JsonDocument.Parse(File.ReadAllText(Path.Combine(temporaryDirectory.Path, "swagger.json")));
// verify one of the endpoints
var paths = document.RootElement.GetProperty("paths");
var productsPath = paths.GetProperty("/products");
Assert.True(productsPath.TryGetProperty("post", out _));
}
[Fact(Skip = "Disabled because it makes CI unstable")]
public void Overwrites_Existing_File()
{
using var temporaryDirectory = new TemporaryDirectory();
var path = Path.Combine(temporaryDirectory.Path, "swagger.json");
var dummyContent = new string('x', 100_000);
File.WriteAllText(path, dummyContent);
var args = new string[] { "tofile", "--output", path, Path.Combine(Directory.GetCurrentDirectory(), "Basic.dll"), "v1" };
Assert.Equal(0, Program.Main(args));
var readContent = File.ReadAllText(path);
Assert.True(readContent.Length < dummyContent.Length);
using var document = JsonDocument.Parse(readContent);
// verify one of the endpoints
var paths = document.RootElement.GetProperty("paths");
var productsPath = paths.GetProperty("/products");
Assert.True(productsPath.TryGetProperty("post", out _));
}
[Fact]
public void CustomDocumentSerializer_Writes_Custom_V2_Document()
{
using var temporaryDirectory = new TemporaryDirectory();
var args = new string[] { "tofile", "--output", $"{temporaryDirectory.Path}/swagger.json", "--serializeasv2", Path.Combine(Directory.GetCurrentDirectory(), "CustomDocumentSerializer.dll"), "v1" };
Assert.Equal(0, Program.Main(args));
using var document = JsonDocument.Parse(File.ReadAllText(Path.Combine(temporaryDirectory.Path, "swagger.json")));
// verify that the custom serializer wrote the swagger info
var swaggerInfo = document.RootElement.GetProperty("swagger").GetString();
Assert.Equal("DocumentSerializerTest2.0", swaggerInfo);
}
[Fact]
public void CustomDocumentSerializer_Writes_Custom_V3_Document()
{
using var temporaryDirectory = new TemporaryDirectory();
var args = new string[] { "tofile", "--output", $"{temporaryDirectory.Path}/swagger.json", Path.Combine(Directory.GetCurrentDirectory(), "CustomDocumentSerializer.dll"), "v1" };
Assert.Equal(0, Program.Main(args));
using var document = JsonDocument.Parse(File.ReadAllText(Path.Combine(temporaryDirectory.Path, "swagger.json")));
// verify that the custom serializer wrote the swagger info
var swaggerInfo = document.RootElement.GetProperty("swagger").GetString();
Assert.Equal("DocumentSerializerTest3.0", swaggerInfo);
}
#if NET6_0_OR_GREATER
[Fact]
public void Can_Generate_Swagger_Json_ForTopLevelApp()
{
using var temporaryDirectory = new TemporaryDirectory();
var args = new string[] { "tofile", "--output", $"{temporaryDirectory.Path}/swagger.json", "--serializeasv2", Path.Combine(Directory.GetCurrentDirectory(), "MinimalApp.dll"), "v1" };
Assert.Equal(0, Program.Main(args));
using var document = JsonDocument.Parse(File.ReadAllText(Path.Combine(temporaryDirectory.Path, "swagger.json")));
// verify one of the endpoints
var paths = document.RootElement.GetProperty("paths");
var path = paths.GetProperty("/WeatherForecast");
Assert.True(path.TryGetProperty("get", out _));
}
#endif
}
}