|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using FluentAssertions; |
| 8 | +using Microsoft.OpenApi.Reader; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Microsoft.OpenApi.Readers.Tests.V31Tests |
| 12 | +{ |
| 13 | + [Collection("DefaultSettings")] |
| 14 | + public class OpenApiHeaderTests |
| 15 | + { |
| 16 | + private const string SampleFolderPath = "V31Tests/Samples/OpenApiHeader/"; |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public async Task ParseBasicHeaderShouldSucceed() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicHeader.yaml")); |
| 23 | + |
| 24 | + // Act |
| 25 | + var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings); |
| 26 | + |
| 27 | + // Assert |
| 28 | + Assert.Equivalent( |
| 29 | + new OpenApiHeader |
| 30 | + { |
| 31 | + Description = "The number of allowed requests in the current period", |
| 32 | + Schema = new OpenApiSchema() |
| 33 | + { |
| 34 | + Type = JsonSchemaType.Integer |
| 35 | + } |
| 36 | + }, header); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task ParseHeaderWithContentShouldSucceed() |
| 41 | + { |
| 42 | + // Arrange |
| 43 | + using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithContent.yaml")); |
| 44 | + |
| 45 | + // Act |
| 46 | + var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings); |
| 47 | + |
| 48 | + // Assert |
| 49 | + Assert.Equivalent( |
| 50 | + new OpenApiHeader |
| 51 | + { |
| 52 | + Description = "A complex header with content", |
| 53 | + Content = new Dictionary<string, OpenApiMediaType>() |
| 54 | + { |
| 55 | + ["application/json"] = new() |
| 56 | + { |
| 57 | + Schema = new OpenApiSchema() |
| 58 | + { |
| 59 | + Type = JsonSchemaType.Object, |
| 60 | + Properties = new Dictionary<string, IOpenApiSchema>() |
| 61 | + { |
| 62 | + ["timestamp"] = new OpenApiSchema() |
| 63 | + { |
| 64 | + Type = JsonSchemaType.String, |
| 65 | + Format = "date-time" |
| 66 | + }, |
| 67 | + ["value"] = new OpenApiSchema() |
| 68 | + { |
| 69 | + Type = JsonSchemaType.Integer |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }, header); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task ParseHeaderWithMultipleContentTypesShouldSucceed() |
| 80 | + { |
| 81 | + // Arrange |
| 82 | + using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithMultipleContentTypes.yaml")); |
| 83 | + |
| 84 | + // Act |
| 85 | + var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings); |
| 86 | + |
| 87 | + // Assert |
| 88 | + Assert.Equivalent( |
| 89 | + new OpenApiHeader |
| 90 | + { |
| 91 | + Description = "A header that accepts multiple content types", |
| 92 | + Content = new Dictionary<string, OpenApiMediaType>() |
| 93 | + { |
| 94 | + ["application/json"] = new() |
| 95 | + { |
| 96 | + Schema = new OpenApiSchema() |
| 97 | + { |
| 98 | + Type = JsonSchemaType.Object, |
| 99 | + Properties = new Dictionary<string, IOpenApiSchema>() |
| 100 | + { |
| 101 | + ["data"] = new OpenApiSchema() |
| 102 | + { |
| 103 | + Type = JsonSchemaType.String |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + ["text/plain"] = new() |
| 109 | + { |
| 110 | + Schema = new OpenApiSchema() |
| 111 | + { |
| 112 | + Type = JsonSchemaType.String |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }, header); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public async Task ParseHeaderWithStyleAndContentShouldPreferContent() |
| 121 | + { |
| 122 | + // Arrange |
| 123 | + using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithStyleAndContent.yaml")); |
| 124 | + |
| 125 | + // Act |
| 126 | + var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings); |
| 127 | + |
| 128 | + // Assert |
| 129 | + // Both content and style can be present, content takes precedence for serialization behavior |
| 130 | + Assert.NotNull(header.Content); |
| 131 | + Assert.Single(header.Content); |
| 132 | + Assert.True(header.Content.ContainsKey("application/json")); |
| 133 | + Assert.Equal(ParameterStyle.Simple, header.Style); // Style can still be present |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments