mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-19 15:31:39 +02:00
Fix some SonarCloud issues (#955)
* Fixed some SonarCloud issues
* if (value.Contains('\n'))
This commit is contained in:
@@ -320,14 +320,14 @@ internal class OpenApiPathsMapper
|
|||||||
var mappedHeaders = headers?.ToDictionary(
|
var mappedHeaders = headers?.ToDictionary(
|
||||||
item => item.Key,
|
item => item.Key,
|
||||||
_ => GetExampleMatcherModel(null, _settings.HeaderPatternToUse).Pattern!
|
_ => GetExampleMatcherModel(null, _settings.HeaderPatternToUse).Pattern!
|
||||||
);
|
) ?? new Dictionary<string, object>();
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(responseContentType))
|
if (!string.IsNullOrEmpty(responseContentType))
|
||||||
{
|
{
|
||||||
mappedHeaders.TryAdd(HeaderContentType, responseContentType!);
|
mappedHeaders.TryAdd(HeaderContentType, responseContentType!);
|
||||||
}
|
}
|
||||||
|
|
||||||
return mappedHeaders?.Keys.Any() == true ? mappedHeaders : null;
|
return mappedHeaders.Keys.Any() ? mappedHeaders : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IList<ParamModel>? MapQueryParameters(IEnumerable<OpenApiParameter> queryParameters)
|
private IList<ParamModel>? MapQueryParameters(IEnumerable<OpenApiParameter> queryParameters)
|
||||||
|
|||||||
@@ -47,9 +47,9 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IReadOnlyList<MappingModel> FromDocument(OpenApiDocument openApiDocument, WireMockOpenApiParserSettings? settings = null)
|
public IReadOnlyList<MappingModel> FromDocument(OpenApiDocument document, WireMockOpenApiParserSettings? settings = null)
|
||||||
{
|
{
|
||||||
return new OpenApiPathsMapper(settings ?? new WireMockOpenApiParserSettings()).ToMappingModels(openApiDocument.Paths, openApiDocument.Servers);
|
return new OpenApiPathsMapper(settings ?? new WireMockOpenApiParserSettings()).ToMappingModels(document.Paths, document.Servers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ internal static class CSharpFormatter
|
|||||||
return "\"\"";
|
return "\"\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value.Contains("\n"))
|
if (value.Contains('\n'))
|
||||||
{
|
{
|
||||||
var escapedValue = value?.Replace("\"", "\"\"") ?? string.Empty;
|
var escapedValue = value?.Replace("\"", "\"\"") ?? string.Empty;
|
||||||
return $"@\"{escapedValue}\"";
|
return $"@\"{escapedValue}\"";
|
||||||
|
|||||||
@@ -42,7 +42,14 @@ public class PactTests
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
server.SavePact(Path.Combine("../../../", "Pact", "files"), "pact-get.json");
|
var folder = Path.Combine("../../../", "Pact", "files");
|
||||||
|
var file = "pact-get.json";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
server.SavePact(folder, file);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
File.ReadAllBytes(Path.Combine(folder, file)).Length.Should().BeGreaterThan(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -201,6 +208,13 @@ public class PactTests
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
server.SavePact(Path.Combine("../../../", "Pact", "files"), "pact-multiple.json");
|
var folder = Path.Combine("../../../", "Pact", "files");
|
||||||
|
var file = "pact-multiple.json";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
server.SavePact(folder, file);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
File.ReadAllBytes(Path.Combine(folder, file)).Length.Should().BeGreaterThan(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,20 @@
|
|||||||
using Moq;
|
|
||||||
using NFluent;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Moq;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests;
|
||||||
|
|
||||||
|
public class WireMockServerAdminFilesTests
|
||||||
{
|
{
|
||||||
public class WireMockServerAdminFilesTests
|
|
||||||
{
|
|
||||||
private readonly HttpClient _client = new HttpClient();
|
private readonly HttpClient _client = new HttpClient();
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -42,8 +41,7 @@ namespace WireMock.Net.Tests
|
|||||||
var httpResponseMessage = await _client.PostAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.txt", multipartFormDataContent).ConfigureAwait(false);
|
var httpResponseMessage = await _client.PostAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.txt", multipartFormDataContent).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(httpResponseMessage.StatusCode).Equals(HttpStatusCode.OK);
|
httpResponseMessage.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
Check.That(server.LogEntries.Count().Equals(1));
|
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
filesystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Once);
|
filesystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Once);
|
||||||
@@ -77,7 +75,7 @@ namespace WireMock.Net.Tests
|
|||||||
var httpResponseMessage = await _client.PostAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.txt", multipartFormDataContent).ConfigureAwait(false);
|
var httpResponseMessage = await _client.PostAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.txt", multipartFormDataContent).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(httpResponseMessage.StatusCode).Equals(HttpStatusCode.OK);
|
httpResponseMessage.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
filesystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Once);
|
filesystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Once);
|
||||||
@@ -110,11 +108,10 @@ namespace WireMock.Net.Tests
|
|||||||
var httpResponseMessageGet = await _client.GetAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.txt").ConfigureAwait(false);
|
var httpResponseMessageGet = await _client.GetAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.txt").ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(httpResponseMessageGet.StatusCode).Equals(HttpStatusCode.OK);
|
httpResponseMessageGet.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
Check.That(httpResponseMessageGet.Content.ReadAsStringAsync().Result).Equals("Here's a string.");
|
var result = await httpResponseMessageGet.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||||
|
result.Should().Be("Here's a string.");
|
||||||
Check.That(server.LogEntries.Count().Equals(2));
|
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
filesystemHandlerMock.Verify(fs => fs.ReadFile(It.Is<string>(p => p == "filename.txt")), Times.Once);
|
filesystemHandlerMock.Verify(fs => fs.ReadFile(It.Is<string>(p => p == "filename.txt")), Times.Once);
|
||||||
@@ -146,9 +143,10 @@ namespace WireMock.Net.Tests
|
|||||||
var httpResponseMessageGet = await _client.GetAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.bin").ConfigureAwait(false);
|
var httpResponseMessageGet = await _client.GetAsync("http://localhost:" + server.Ports[0] + "/__admin/files/filename.bin").ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(httpResponseMessageGet.StatusCode).Equals(HttpStatusCode.OK);
|
httpResponseMessageGet.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
Check.That(httpResponseMessageGet.Content.ReadAsByteArrayAsync().Result).Equals(symbol);
|
|
||||||
Check.That(server.LogEntries.Count().Equals(2));
|
var result = await httpResponseMessageGet.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||||
|
result.Should().BeEquivalentTo(symbol);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
filesystemHandlerMock.Verify(fs => fs.ReadFile(It.Is<string>(p => p == "filename.bin")), Times.Once);
|
filesystemHandlerMock.Verify(fs => fs.ReadFile(It.Is<string>(p => p == "filename.bin")), Times.Once);
|
||||||
@@ -176,8 +174,7 @@ namespace WireMock.Net.Tests
|
|||||||
var httpResponseMessage = await _client.SendAsync(httpRequestMessage).ConfigureAwait(false);
|
var httpResponseMessage = await _client.SendAsync(httpRequestMessage).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(httpResponseMessage.StatusCode).Equals(HttpStatusCode.NoContent);
|
httpResponseMessage.StatusCode.Should().Be(HttpStatusCode.NoContent);
|
||||||
Check.That(server.LogEntries.Count().Equals(1));
|
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
filesystemHandlerMock.Verify(fs => fs.FileExists(It.IsAny<string>()), Times.Once);
|
filesystemHandlerMock.Verify(fs => fs.FileExists(It.IsAny<string>()), Times.Once);
|
||||||
@@ -204,12 +201,10 @@ namespace WireMock.Net.Tests
|
|||||||
var httpResponseMessage = await _client.SendAsync(httpRequestMessage).ConfigureAwait(false);
|
var httpResponseMessage = await _client.SendAsync(httpRequestMessage).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(httpResponseMessage.StatusCode).Equals(HttpStatusCode.NotFound);
|
httpResponseMessage.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||||
Check.That(server.LogEntries.Count().Equals(1));
|
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
filesystemHandlerMock.Verify(fs => fs.FileExists(It.IsAny<string>()), Times.Once);
|
filesystemHandlerMock.Verify(fs => fs.FileExists(It.IsAny<string>()), Times.Once);
|
||||||
filesystemHandlerMock.VerifyNoOtherCalls();
|
filesystemHandlerMock.VerifyNoOtherCalls();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user