Skip to content

Commit 09b7e20

Browse files
committed
- add v3 test for Encoding
1 parent 72959fd commit 09b7e20

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
<None Update="V3Tests\Samples\OpenApiDiscriminator\basicDiscriminator.yaml">
7171
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7272
</None>
73+
<None Update="V3Tests\Samples\OpenApiEncoding\advancedEncoding.yaml">
74+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
75+
</None>
76+
<None Update="V3Tests\Samples\OpenApiEncoding\basicEncoding.yaml">
77+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
78+
</None>
7379
<None Update="V3Tests\Samples\OpenApiExample\advancedExample.yaml">
7480
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7581
</None>
@@ -120,7 +126,6 @@
120126
<ItemGroup>
121127
<Folder Include="V3Tests\Samples\OpenApiSecurityScheme\" />
122128
<Folder Include="V3Tests\Samples\OpenApiSchema\" />
123-
<Folder Include="V3Tests\Samples\OpenApiEncoding\" />
124129
</ItemGroup>
125130

126131
</Project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.IO;
7+
using System.Linq;
8+
using FluentAssertions;
9+
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Readers.ParseNodes;
11+
using Microsoft.OpenApi.Readers.V3;
12+
using SharpYaml.Serialization;
13+
using Xunit;
14+
15+
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
16+
{
17+
[Collection("DefaultSettings")]
18+
public class OpenApiEncodingTests
19+
{
20+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiEncoding";
21+
22+
[Fact]
23+
public void ParseBasicEncodingShouldSucceed()
24+
{
25+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicEncoding.yaml")))
26+
{
27+
var yamlStream = new YamlStream();
28+
yamlStream.Load(new StreamReader(stream));
29+
var yamlNode = yamlStream.Documents.First().RootNode;
30+
31+
var context = new ParsingContext();
32+
var diagnostic = new OpenApiDiagnostic();
33+
34+
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
35+
36+
// Act
37+
var encoding = OpenApiV3Deserializer.LoadEncoding(node);
38+
39+
// Assert
40+
encoding.ShouldBeEquivalentTo(
41+
new OpenApiEncoding
42+
{
43+
ContentType = "application/xml; charset=utf-8"
44+
});
45+
}
46+
}
47+
48+
[Fact]
49+
public void ParseAdvancedEncodingShouldSucceed()
50+
{
51+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "advancedEncoding.yaml")))
52+
{
53+
var yamlStream = new YamlStream();
54+
yamlStream.Load(new StreamReader(stream));
55+
var yamlNode = yamlStream.Documents.First().RootNode;
56+
57+
var context = new ParsingContext();
58+
var diagnostic = new OpenApiDiagnostic();
59+
60+
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
61+
62+
// Act
63+
var encoding = OpenApiV3Deserializer.LoadEncoding(node);
64+
65+
// Assert
66+
encoding.ShouldBeEquivalentTo(
67+
new OpenApiEncoding
68+
{
69+
ContentType = "image/png, image/jpeg",
70+
Headers =
71+
{
72+
["X-Rate-Limit-Limit"] =
73+
new OpenApiHeader
74+
{
75+
Description = "The number of allowed requests in the current period",
76+
Schema = new OpenApiSchema
77+
{
78+
Type = "integer"
79+
}
80+
}
81+
}
82+
});
83+
}
84+
}
85+
}
86+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#encodingObject
2+
# only accept png/jpeg
3+
contentType: image/png, image/jpeg
4+
headers:
5+
X-Rate-Limit-Limit:
6+
description: The number of allowed requests in the current period
7+
schema:
8+
type: integer
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#encodingObject
2+
# require XML Content-Type in utf-8 encoding
3+
contentType: application/xml; charset=utf-8

0 commit comments

Comments
 (0)