@@ -29,11 +29,13 @@ public void MapOpenApi_ReturnsEndpointConventionBuilder()
29
29
Assert . IsAssignableFrom < IEndpointConventionBuilder > ( returnedBuilder ) ;
30
30
}
31
31
32
- [ Fact ]
33
- public void MapOpenApi_SupportsCustomizingPath ( )
32
+ [ Theory ]
33
+ [ InlineData ( "/custom/{documentName}/openapi.json" ) ]
34
+ [ InlineData ( "/custom/{documentName}/openapi.yaml" ) ]
35
+ [ InlineData ( "/custom/{documentName}/openapi.yml" ) ]
36
+ public void MapOpenApi_SupportsCustomizingPath ( string expectedPath )
34
37
{
35
38
// Arrange
36
- var expectedPath = "/custom/{documentName}/openapi.json" ;
37
39
var serviceProvider = CreateServiceProvider ( ) ;
38
40
var builder = new DefaultEndpointRouteBuilder ( new ApplicationBuilder ( serviceProvider ) ) ;
39
41
@@ -72,13 +74,17 @@ public async Task MapOpenApi_ReturnsRenderedDocument()
72
74
} ) ;
73
75
}
74
76
75
- [ Fact ]
76
- public async Task MapOpenApi_ReturnsDefaultDocumentIfNoNameProvided ( )
77
+ [ Theory ]
78
+ [ InlineData ( "/openapi.json" , "application/json;charset=utf-8" , false ) ]
79
+ [ InlineData ( "/openapi.toml" , "application/json;charset=utf-8" , false ) ]
80
+ [ InlineData ( "/openapi.yaml" , "text/plain+yaml;charset=utf-8" , true ) ]
81
+ [ InlineData ( "/openapi.yml" , "text/plain+yaml;charset=utf-8" , true ) ]
82
+ public async Task MapOpenApi_ReturnsDefaultDocumentIfNoNameProvided ( string expectedPath , string expectedContentType , bool isYaml )
77
83
{
78
84
// Arrange
79
85
var serviceProvider = CreateServiceProvider ( ) ;
80
86
var builder = new DefaultEndpointRouteBuilder ( new ApplicationBuilder ( serviceProvider ) ) ;
81
- builder . MapOpenApi ( "/openapi.json" ) ;
87
+ builder . MapOpenApi ( expectedPath ) ;
82
88
var context = new DefaultHttpContext ( ) ;
83
89
var responseBodyStream = new MemoryStream ( ) ;
84
90
context . Response . Body = responseBodyStream ;
@@ -91,6 +97,11 @@ public async Task MapOpenApi_ReturnsDefaultDocumentIfNoNameProvided()
91
97
92
98
// Assert
93
99
Assert . Equal ( StatusCodes . Status200OK , context . Response . StatusCode ) ;
100
+ Assert . Equal ( expectedContentType , context . Response . ContentType ) ;
101
+ var responseString = Encoding . UTF8 . GetString ( responseBodyStream . ToArray ( ) ) ;
102
+ // String check to validate that generated document starts with YAML syntax
103
+ Assert . Equal ( isYaml , responseString . StartsWith ( "openapi: 3.0.1" , StringComparison . OrdinalIgnoreCase ) ) ;
104
+ responseBodyStream . Position = 0 ;
94
105
ValidateOpenApiDocument ( responseBodyStream , document =>
95
106
{
96
107
Assert . Equal ( "OpenApiEndpointRouteBuilderExtensionsTests | v1" , document . Info . Title ) ;
@@ -121,16 +132,19 @@ public async Task MapOpenApi_Returns404ForUnresolvedDocument()
121
132
Assert . Equal ( "No OpenAPI document with the name 'v2' was found." , Encoding . UTF8 . GetString ( responseBodyStream . ToArray ( ) ) ) ;
122
133
}
123
134
124
- [ Fact ]
125
- public async Task MapOpenApi_ReturnsDocumentIfNameProvidedInQuery ( )
135
+ [ Theory ]
136
+ [ InlineData ( "/openapi.json" , "application/json;charset=utf-8" , false ) ]
137
+ [ InlineData ( "/openapi.yaml" , "text/plain+yaml;charset=utf-8" , true ) ]
138
+ [ InlineData ( "/openapi.yml" , "text/plain+yaml;charset=utf-8" , true ) ]
139
+ public async Task MapOpenApi_ReturnsDocumentIfNameProvidedInQuery ( string expectedPath , string expectedContentType , bool isYaml )
126
140
{
127
141
// Arrange
128
142
var documentName = "v2" ;
129
143
var hostEnvironment = new HostEnvironment ( ) { ApplicationName = nameof ( OpenApiEndpointRouteBuilderExtensionsTests ) } ;
130
144
var serviceProviderIsService = new ServiceProviderIsService ( ) ;
131
145
var serviceProvider = CreateServiceProvider ( documentName ) ;
132
146
var builder = new DefaultEndpointRouteBuilder ( new ApplicationBuilder ( serviceProvider ) ) ;
133
- builder . MapOpenApi ( "/openapi.json" ) ;
147
+ builder . MapOpenApi ( expectedPath ) ;
134
148
var context = new DefaultHttpContext ( ) ;
135
149
var responseBodyStream = new MemoryStream ( ) ;
136
150
context . Response . Body = responseBodyStream ;
@@ -144,6 +158,11 @@ public async Task MapOpenApi_ReturnsDocumentIfNameProvidedInQuery()
144
158
145
159
// Assert
146
160
Assert . Equal ( StatusCodes . Status200OK , context . Response . StatusCode ) ;
161
+ Assert . Equal ( expectedContentType , context . Response . ContentType ) ;
162
+ var responseString = Encoding . UTF8 . GetString ( responseBodyStream . ToArray ( ) ) ;
163
+ // String check to validate that generated document starts with YAML syntax
164
+ Assert . Equal ( isYaml , responseString . StartsWith ( "openapi: 3.0.1" , StringComparison . OrdinalIgnoreCase ) ) ;
165
+ responseBodyStream . Position = 0 ;
147
166
ValidateOpenApiDocument ( responseBodyStream , document =>
148
167
{
149
168
Assert . Equal ( $ "OpenApiEndpointRouteBuilderExtensionsTests | { documentName } ", document . Info . Title ) ;
0 commit comments