Skip to content

Commit b9f7d95

Browse files
committed
ci: fixes an issue where the unit tests would depend on a service experiencing an outage
1 parent 2cfa031 commit b9f7d95

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
using System.Globalization;
77
using System.IO;
88
using System.Linq;
9+
using System.Net;
10+
using System.Net.Http;
11+
using System.Net.Http.Headers;
912
using System.Text;
1013
using System.Text.Json.Nodes;
14+
using System.Threading;
1115
using System.Threading.Tasks;
1216
using FluentAssertions;
1317
using Microsoft.OpenApi.Extensions;
@@ -20,6 +24,8 @@
2024
using Microsoft.OpenApi.Validations;
2125
using Microsoft.OpenApi.Validations.Rules;
2226
using Microsoft.OpenApi.Writers;
27+
using Moq;
28+
using Moq.Protected;
2329
using Xunit;
2430

2531
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -28,8 +34,6 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
2834
public class OpenApiDocumentTests
2935
{
3036
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDocument/";
31-
private const string codacyApi = "https://api.codacy.com/api/api-docs/swagger.yaml";
32-
3337
private static async Task<T> CloneAsync<T>(T element) where T : class, IOpenApiSerializable
3438
{
3539
using var stream = new MemoryStream();
@@ -1493,8 +1497,24 @@ public async Task ParseDocumentWithExampleReferencesPasses()
14931497
[Fact]
14941498
public async Task ParseDocumentWithNonStandardMIMETypePasses()
14951499
{
1500+
var path = Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml");
1501+
using var stream = Resources.GetStream(path);
1502+
using var streamReader = new StreamReader(stream);
1503+
var contentAsString = await streamReader.ReadToEndAsync();
1504+
var mockMessageHandler = new Mock<HttpMessageHandler>();
1505+
mockMessageHandler.Protected()
1506+
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
1507+
.ReturnsAsync(new HttpResponseMessage {
1508+
StatusCode = HttpStatusCode.OK,
1509+
Content = new StringContent(contentAsString, new MediaTypeHeaderValue("text/x-yaml"))
1510+
});
1511+
var settings = new OpenApiReaderSettings
1512+
{
1513+
HttpClient = new HttpClient(mockMessageHandler.Object)
1514+
};
1515+
settings.AddYamlReader();
14961516
// Act & Assert: Ensure NotSupportedException is not thrown for non-standard MIME type: text/x-yaml
1497-
var result = await OpenApiDocument.LoadAsync(codacyApi, SettingsFixture.ReaderSettings);
1517+
var result = await OpenApiDocument.LoadAsync("https://localhost/doesntmatter/foo.bar", settings);
14981518
Assert.NotNull(result.Document);
14991519
}
15001520
}

0 commit comments

Comments
 (0)